c++ yes/no do while loop
Mar 30, 2018 at 4:17am UTC
Okay, so I'm trying to make a program that can display an arithmetic options menu that simulates a simple calculator and make the cycle continue until the user wants to stop.
I got down the arithmetic part but not the cycle thing. And I mean a yes or no do while thing here. I'm trying to get it to work but just doesn't want to. Can someone give me some pointers?
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
#include <iostream>
using namespace std;
int main ()
{
char input;
float num1, num2;
char ans;
cout << "Please enter the first number: " << endl;
cin >> num1;
cout << "Please enter the second number: " << endl;
cin >> num2;
cout << "Now, please choose an arithmetic operator (Arithmetic operators are /, *, + and -): " << endl;
cin >> input;
do {
if ( input == '/' )
{
cout << num1 << "/" << num2 << "=" << num1 / num2 << endl;
}
else if ( input == '*' )
{
cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
}
else if ( input == '+' )
{
cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
}
else if ( input == '-' )
{
cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
}
else
{
cout << "Sorry, you entered an invalid operator.\n" ;
}
cout<< "Do you want to continue (Y/N)?" << endl;
cout<< "You must type a 'Y' or an 'N' : " ;
cin >> ans;
}
while ((ans == 'Y' ) || (ans == 'y' ));
return 0;
}
Last edited on Mar 30, 2018 at 4:18am UTC
Mar 30, 2018 at 7:12am UTC
use a boolean for the do-while loop
1 2 3 4 5 6 7 8
bool again=true ;
do
{
//your code
cout<<"Do you want to continue? press 0 if not.
cin>>again;
}
while(again==true);
that will be better
Last edited on Mar 30, 2018 at 7:12am UTC
Mar 30, 2018 at 7:13am UTC
DigiLei wrote:Can someone give me some pointers ?
That's a terribly dangerous request in this forum!
Just move your
do {
statement, which is currently on line 19, to immediately before line 11.
It then works fine.
Last edited on Mar 30, 2018 at 7:26am UTC
Mar 30, 2018 at 12:42pm UTC
Hello DigiLei,
As lastchance pointed out this is the dangerous part.
Something yo could with the while condition:
while (std::toupper(ans) != 'N' );
this will allow anything other than 'N' to be evaluated to true and you would actually have to type 'N' to quit.
Another alternative is:
1 2
std::cin >>ans;
ans=std::toupper(ans); // <--- Requires header file "<cctype>".
Then the while condition would only have to check for an uppercase letter.
Right now I do not see anything wrong with what you have until I give it a test run.
Moving the "do {" as lastchance suggested made a big difference. Other than adding some "\n"s and a couple of spaces the program works.
Hope that helps,
Andy
Mar 30, 2018 at 12:50pm UTC
Two unrelated suggestions:
1. Indent your code to match the actual block structure. Trust me, if you don't do this, you'll mess up your code at some point.
2. When you select from among different values of the same variable, use a switch statement instead of an if/then/else ladder. It makes the code more clear that way.
With these suggestions and those from lastchance:
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
#include <iostream>
using namespace std;
int
main()
{
char input;
float num1, num2;
char ans;
do {
cout << "Please enter the first number: " << endl;
cin >> num1;
cout << "Please enter the second number: " << endl;
cin >> num2;
cout <<
"Now, please choose an arithmetic operator (Arithmetic operators are /, *, + and -): "
<< endl;
cin >> input;
switch (input) {
case '/' :
cout << num1 << "/" << num2 << "=" << num1 / num2 << endl;
break ;
case '*' :
cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
break ;
case '+' :
cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
break ;
case '-' :
cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
break ;
default :
cout << "Sorry, you entered an invalid operator.\n" ;
break ;
}
cout << "Do you want to continue (Y/N)?" << endl;
cout << "You must type a 'Y' or an 'N' : " ;
cin >> ans;
}
while ((ans == 'Y' ) || (ans == 'y' ));
return 0;
}
Topic archived. No new replies allowed.