Asking User If they want to run program again
May 23, 2021 at 2:05am UTC
I have this calculator program and i need to ask the user if they want to run it again, right now when i say no it will stop, but when i say yes it just ends, how can i fix this?
Also when the operation input is not valid, the program treats it as r = false, says goodbye and ends when it is supposed to run the program again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include <iostream>
using namespace std;
void calculator(bool r);
void run_again(bool r);
int main()
{
bool r;
do {
calculator(r);
run_again(r);
}while (r == true );
return 0;
}
void calculator(bool r){
float variable1;
float variable2;
char operation;
float answer;
cout << "Enter First Number: " ;
cin >> variable1;
cout << "Enter Second Number: " ;
cin >> variable2;
cout << "What would you like to do with these numbers? " << endl;
cout << "Options: +, -, *, /, M (to get max value between 2 numbers), m(to get min value): " ;
cin >> operation;
switch (operation){
case '+' :
answer = variable1 + variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '-' :
answer = variable1 - variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '*' :
answer = variable1 * variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '/' :
answer = variable1 / variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case 'M' :
answer = max(variable1, variable2);
cout << "Answer is: " ;
cout << answer << endl;
break ;
case 'm' :
answer = min(variable1, variable2);
cout << "Answer is: " ;
cout << answer << endl;
break ;
default :
cout << "Not An Option" ;
r = true ;
break ;
}
}
void run_again(bool r)
{
string s;
cout<<"Do you want to run again (yes or no):" <<endl;
cin >> s;
if (s == "yes" ) {
r = true ;
}
else {
cout << "Goodbye" ;
r = false ;
}
}
Last edited on May 23, 2021 at 2:52am UTC
May 23, 2021 at 2:57am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include <iostream>
using namespace std;
void calculator();
bool run_again();
int main()
{
bool r{true };
do {
calculator();
r = run_again();
}while (r == true );
return 0;
}
void calculator(){
float variable1;
float variable2;
char operation;
float answer;
cout << "Enter First Number: " ;
cin >> variable1;
cout << "Enter Second Number: " ;
cin >> variable2;
cout << "What would you like to do with these numbers? " << endl;
cout << "Options: +, -, *, /, M (to get max value between 2 numbers), m(to get min value): " ;
cin >> operation;
switch (operation){
case '+' :
answer = variable1 + variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '-' :
answer = variable1 - variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '*' :
answer = variable1 * variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case '/' :
answer = variable1 / variable2;
cout << "Answer is: " ;
cout << answer << endl;
break ;
case 'M' :
answer = max(variable1, variable2);
cout << "Answer is: " ;
cout << answer << endl;
break ;
case 'm' :
answer = min(variable1, variable2);
cout << "Answer is: " ;
cout << answer << endl;
break ;
default :
cout << "Not An Option" ;
break ;
}
}
bool run_again()
{
string s;
bool r;
cout<<"Do you want to run again (yes or no):" <<endl;
cin >> s;
if (s == "yes" ) {
r = true ;
}
else {
r = false ;
}
return r;
}
May 23, 2021 at 3:06am UTC
Thanks, but this didnt solve the issue of rerunning the program when the input is not an option in the swtich statement
May 23, 2021 at 4:39am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool run_again()
{
string prompt{"Do you want to run again (yes or no): " };
string s;
while (
cout << prompt &&
cin >> s &&
( s != "yes" && s != "no" )
)
{
cout << s << ' ' << "Wrong answer\n" ;
}
if (s == "yes" ){ return true ;}
else return false ;
}
Enter First Number: 1
Enter Second Number: 2
What would you like to do with these numbers?
Options: +, -, *, /, M (to get max value between 2 numbers), m(to get min value): +
Answer is: 3
Do you want to run again (yes or no): w
w Wrong answer
y
y Wrong answer
yes
Enter First Number: 2
Enter Second Number: 3
What would you like to do with these numbers?
Options: +, -, *, /, M (to get max value between 2 numbers), m(to get min value): +
Answer is: 5
Do you want to run again (yes or no): mmm
mmm Wrong answer
m
m Wrong answer
no
Program ended with exit code: 0
Topic archived. No new replies allowed.