1. You will have two functions in your code (apart from main() ). One function w
ID: 3698880 • Letter: 1
Question
1. You will have two functions in your code (apart from main() ). One function will take care of the login and the other function will be for calculations. 2. The function prototypes for the new functions are: int login(); void calc(int,int,int); 3. The login()asks the user to enter a username and password. Once the program has the username and password, it will reverse the password and compare it to the username. If the reversed password matches the username, it will return a value of 1(it matches) else it will return a 0 (doesn not match) to the main(). For example: a. let username be: Alice and password be: ecilA Then the function returns 1 (true) b. let username be: Alice and password be: ceAli (or anything else different than ecilA) Then the function returns 0 (true) 4. The other function, will be called calc() and will take 3 values as parameters – first will be the integer variable that holds the user’s choice from 1-6, the second would be the first number that the user enters, the third would be the second number the user enters(it will be 0 for factorial case). This function will have a switch case and based on the user’s choice will perform a mathematical operation and print the answer. 5. The main() will first have a function call for login and then will check the return value of login(). If it is a successful login, it will print the menu and ask the user to enter a choice. If the user enters choice 6 then it will ask the user to enter only one number and then call the function calc() else it will ask the user to enter two numbers and then call the function calc(). The main() will have the do-while loop to enable the user to repeat any calculation again
Explanation / Answer
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
int login(){
string username ;
string password;
cout<<"Enter user name : ";
getline(cin, username);
cout<<" Enter password : ";
getline(cin, password);
int length1 = username.length();
int length2 = password.length();
if(length1 != length2)
return 0;
else{
int i;
for(i=0;i < length1 ;i++){
if(username[i] != password[length1-i-1])
return 0;
}
}
return 1;
}
void calc( int choice, int number1, int number2){
if( choice == 1)
cout<<" Addition result = "<<number1+number2;
else if(choice == 2){
cout<<" Subtraction result = ";
if(number2 < number1) cout<<number1-number2;
else cout<<number2-number1;
}
else if(choice == 3)
cout<<" Multiplication result = "<<number1*number2;
else if(choice == 4){
cout<<" Division result = ";
if(number1 > number2) cout<<number1/number2;
else cout<<number2/number1;
}
else if(choice == 5){
cout<<" Power result = "<<pow(number1,number2);
}
else{
cout<<" Factorial result = ";
int i = 1;
int f = 1;
for(i =1; i<=number1; i++)
f = f * i;
cout<<f;
}
}
int main() {
int choice;
char ch;
int login_result = login();
if(login_result = 1) {
do{
cout<<" Menu";
cout<<" 1. Addition";
cout<<" 2. Subtraction";
cout<<" 3. Multiplication";
cout<<" 4. Division";
cout<<" 5. Power";
cout<<" 6. Factorial";
cout<<" Enter you choice";
cin>>choice;
int number1,number2;
if(choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5){
cout<<" Enter first number";
cin>>number1;
cout<<" Enter second number";
cin>>number2;
calc(choice,number1,number2);
}
else if(choice == 6){
cout<<" Enter the number";
cin>>number1;
calc(choice,number1,0);
}
else{
cout<<" Wrong choice ";
}
cout<<" Want to enter again ? (Y or N)";
cin>>ch;
}while(ch == 'Y' || ch == 'y');
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.