looping! :S


How to apply while loops when I want to repeat something?

problem:
Write a C++ calculator that takes two numbers and a letter indicating an
operation to be performed. The user enters the letter and the program performs the operation depending on the letter as the following menu:
A: Add (the two numbers)
S: Subtract
M: Multiply
D: Divide

The program should repeat the menu unless the user enters ‘B’ (for “Back”) in which case the program will (again) prompt the user for two numbers and a number system. The program should not exit unless the user presses the character ‘Q’ (for “Quit”).

I solved it but I don't know how to apply loops! :/ help me
my program:
#include<iostream>

using namespace std;

int main()
{

int x1,x2;
char x;
char a= 'a';
char s = 's';
char m = 'm';
char d = 'd';
char q='q';
int add,subt,multip,divide;

cout << "Enter the two numbers: " << endl;
cin >> x1 >> x2;

cout << "Enter the letter : " << endl;
cin >> x;

cout << "A: Add (the two numbers)" << endl
<< "S: Subtract" << endl
<< "M: Multiply" << endl
<< "D: Divide" << endl
<< "Q: Quit (exit the program)"<< endl;

if (x==a)
{ add = x1+x2;

cout << add << endl;
}
else
if (x==s)
{ subt = x1-x2;
cout << subt<< endl;
}
else if (x==m)
{
multip= x1*x2;
cout << multip << endl;
}

else if (x==d)
{
divide = x1/x2;
cout << divide << endl;
}
else if (x==q)
cout << "Quit" << endl;
return 0;

}

Hi, Have a look at the section on while loops and post any questions if you have trouble.

http://www.cplusplus.com/doc/tutorial/control/


in this problem there is no expression to apply loops , only the menu !
I'm confused , please help me !!
I solved it but I don't know how to apply loops! :/ help me

Take a look at the link I posted, just scroll down to the section titled Iteration structures (loops) it shows you examples on how to use a while loop.
If you're confused about how it works after that, post back on what you're having problems understanding.

Hint : your while condition would need to compare the user input to 'Q'
Last edited on
Topic archived. No new replies allowed.