In: Computer Science
I did the previous steps and now im at 6 and 7. I can not figure it out. It is a calculator code, and this part we need to make it password protected. The user is asked to enter in a username and password, and if the password is the backwards of the username you get into the calulator code (that i already have finished below). Use strings and arrays to hold the username and password (maybe use something like gets() and fgets()) and use a do while loop to reverse the password and use strcmp() to compair the two words, if they are the same the calculator will unlock showing my calculator menu, else something like "invalid password" appears on the screen, and asking the user if they desire to try again. Use <stdio.h> and <string.h>
6. Now I want to make sure that nobody uses my calculator without authorization. So a user will have to enter a username and password, which if correct will show the welcome page and menu to the user else it will print the message like “Incorrect password. Authorization denied”. The correct password will be the reverse of the username. So after the user enters the username and password, you will check if the reverse of the password is the username. You will use a while loop to reverse your password. And you will you strcmp() to compare the username the user entered to the reverse of the password.
7. Below are the snapshots of the desired output screen. Figure 1: The above shows the program asking for username and password. We have entered a name in the username and the reverse of that name as the password. Figure 2: If the logging is successful you should print a message “Successful login” and then the menu should be displayed.
#include<stdio.h>
int main() {
char c;
do {
printf("Welcome to my
calculator\nPlease look at the menu below for your
calculations\n");
printf("1. Addition\n2.
Subtraction\n3. Multiplication\n4. Division\n5. Modulus \n6.
Factorial \n");
int n, x, y, f, i;
unsigned long long a = 1;
printf("Please press your choice
from 1-6: ");
scanf_s("%d", &n);
getchar();
switch (n) {
case 1:
printf("Enter
first number: ");
scanf_s("%d",
&x);
getchar();
printf("Enter
second number: ");
scanf_s("%d",
&y);
getchar();
printf("%d + %d
= %d \n", x, y, x + y);
break;
case 2:
printf("Enter
first number: ");
scanf_s("%d",
&x);
getchar();
printf("Enter
second number: ");
scanf_s("%d",
&y);
getchar();
printf("%d - %d
= %d \n", x, y, x - y);
break;
case 3:
printf("Enter
first number: ");
scanf_s("%d",
&x);
getchar();
printf("Enter
second number: ");
scanf_s("%d",
&y);
getchar();
printf("%d * %d
= %d \n", x, y, x*y);
break;
case 4:
printf("Enter
first number: ");
scanf_s("%d",
&x);
getchar();
printf("Enter
second number: ");
scanf_s("%d",
&y);
getchar();
if (y == 0)
{
printf("Division by 0 is not allowed\n");
}
else {
printf("%d / %d = %d \n", x, y, x / y);
}
break;
case 5:
printf("Enter
first number: ");
scanf_s("%d",
&x);
getchar();
printf("Enter
second number: ");
scanf_s("%d",
&y);
getchar();
if (y == 0)
{
printf("Division by 0 is not allowed\n");
}
else {
printf("The Modulus of %d and %d is: %d \n", x,
y, x%y);
}
break;
case 6:
printf("Enter
your number: ");
scanf_s("%d",
&f);
getchar();
if (f <
0)
printf("Error! Factorial of a negative number
does not exist\n");
else {
for (i = 1; i <= f; i++) {
a *= i;
}
printf("Factorial of %d = \n", f, a);
}
break;
default:
printf("Error!!
Enter a valid option 1-6\n");
}
printf("Do you want to Continue?
(Y/N): ");
scanf_s(" %c", &c);
getchar();
} while (c != 'N');
return 0;
}
#include <stdio.h>
#include <string.h>
//Function definition
char* reverse(char uname[]);
int main() {
//Declaring character arrays
char uname[100],pwd[100];
//declaring variable
char c;
//getting the firstname entered by the user
printf("Enter Username :");
gets(uname);//inputs value to array
//Getting the password entered by the user
printf("Enter Password :");
gets(pwd);//inputs value to array
//calling the function by passing the string as argument
reverse(uname);
if(strcmp(uname,pwd) == 0 )
{
do {
printf("Successful login\n");
printf("\nWelcome to my calculator\nPlease look at the menu below
for your calculations\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4.
Division\n5. Modulus \n6. Factorial \n");
int n, x, y, f, i;
unsigned long long a = 1;
printf("Please press your choice from 1-6: ");
scanf_s("%d", &n);
getchar();
switch (n) {
case 1:
printf("Enter first number: ");
scanf_s("%d", &x);
getchar();
printf("Enter second number: ");
scanf_s("%d", &y);
getchar();
printf("%d + %d = %d \n", x, y, x + y);
break;
case 2:
printf("Enter first number: ");
scanf_s("%d", &x);
getchar();
printf("Enter second number: ");
scanf_s("%d", &y);
getchar();
printf("%d - %d = %d \n", x, y, x - y);
break;
case 3:
printf("Enter first number: ");
scanf_s("%d", &x);
getchar();
printf("Enter second number: ");
scanf_s("%d", &y);
getchar();
printf("%d * %d = %d \n", x, y, x*y);
break;
case 4:
printf("Enter first number: ");
scanf_s("%d", &x);
getchar();
printf("Enter second number: ");
scanf_s("%d", &y);
getchar();
if (y == 0) {
printf("Division by 0 is not allowed\n");
}
else {
printf("%d / %d = %d \n", x, y, x / y);
}
break;
case 5:
printf("Enter first number: ");
scanf_s("%d", &x);
getchar();
printf("Enter second number: ");
scanf_s("%d", &y);
getchar();
if (y == 0) {
printf("Division by 0 is not allowed\n");
}
else {
printf("The Modulus of %d and %d is: %d \n", x, y, x%y);
}
break;
case 6:
printf("Enter your number: ");
scanf_s("%d", &f);
getchar();
if (f < 0)
printf("Error! Factorial of a negative number does not
exist\n");
else {
for (i = 1; i <= f; i++) {
a *= i;
}
printf("Factorial of %d = %li\n", f, a);
}
break;
default:
printf("Error!! Enter a valid option 1-6\n");
}
printf("Do you want to Continue? (Y/N): ");
scanf_s(" %c", &c);
getchar();
} while (c != 'N');
}
else
{
printf("**Incorrect password.
Authorization denied**\n");
}
return 0;
}
//function which reverses the character array
char* reverse(char uname[])
{
int m = 0, n= 0,temp;
//Assigning length of the string minus 1 to the variable n
n = strlen(uname) - 1;
//This loop will reverse the string
while (m < n)
{
temp = uname[m];
uname[m] = uname[n];
uname[n] = temp;
m++;
n--;
}
return uname;
}
______________________
output:
_________Thank You