PROBLEM...
Write a program that prompts that user to select from a menu of operators, then ask the user to input two numbers and perform the selected operation. The program should exit only if the user choose the exit option.
The following are the menu of operations:
[ + ] Add two numbers
[ - ] Subtract two numbers
[ * ] Multiply two numbers
[ / ] Divide two numbers
[ Q ] Exit
Sample run
-----------------------------------------------------
-----------MENU-----------
[ + ] Add two numbers
[ - ] Subtract two numbers
[ * ] Multiply two numbers
[ / ] Divide two numbers
[ Q ] Exit
--------------------------
Enter your choice : +
Enter two numbers : 10 5
The sum of 10 and 5 is 15
-----------MENU-----------
[ + ] Add two numbers
[ - ] Subtract two numbers
[ * ] Multiply two numbers
[ / ] Divide two numbers
[ Q ] Exit
--------------------------
Enter your choice : -
Enter two numbers : 10 5
The difference of 10 and 5 is 5
-----------MENU-----------
[ + ] Add two numbers
[ - ] Subtract two numbers
[ * ] Multiply two numbers
[ / ] Divide two numbers
[ Q ] Exit
--------------------------
Enter your choice : /
Enter two numbers : 10 5
The quotient of 10 and 5 is 1.5
-----------MENU-----------
[ + ] Add two numbers
[ - ] Subtract two numbers
[ * ] Multiply two numbers
[ / ] Divide two numbers
[ Q ] Exit
--------------------------
Enter your choice : Q
You have good start. First I would work on the menu; get that to display the pay you want it, get the choice from the user into a variable. I would put the menu in a function that returns the menuChoice back to main where I would use a switch statement to process the menuChoice. Once you have that part working continue on with a function for each operation. Quit can be handled in main.
Work on some code and if you have a problem post the code here and state what the problem is.
the last time this OP did exactly the same - 4 problems posted without any effort and after some initial pushback someone was kind (stupid?) enough to do his/her work for him/her. i just hope better sense will prevail this time ... http://www.cplusplus.com/forum/beginner/211769/
You do not output the result. Simply change e.g. case '+':
cout << "The sum of " << n1 << "and" << n2 << " is " << n1 + n2 << endl;
You can do this for any case. Note that you don't need sum, difference, product, quotient.
Further note that you don't calculate the quotient for case '/'.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
In line 1 you should initialize all of your variables e.g.,int n1{ 0 }, n2{ 0 } ...
You display your menu and ask for a choice, but never ask the user for any input.
Line 12 operators is nor defined and if it was would not likely have any value at this point. Not only would your switch fail, but it may not even compile;
That is because you ask the user to enter and then enter two numbers. So you would need to test for the input "operators" being not equal to "Q" to enter the two numbers otherwise it will skip entering the two numbers and go to the switch to quit the program.
You also need a case for "Q" and your default should display a message that you did something wrong and your current default message would work better in the case for "Q".
BTW you code looks much better.
Hope that helps,
Andy
P.S. in the case for divide you should check that "n2" is not equal to zero otherwise you will get a divide by zero error at some point and that will not look good.
operators = toupper(operators) // <--- Does nothing if not a lowercase letter.
if (operators != 'Q')
{
cout << "Enter two numbers : ";
cin >> n1 >> n2;
}
If the if condition is not true it just continues on the the switch statement.
Some other small things I noticed. These will not cause any problems with the running of the program, but they are not needed: