GoodEve guys!

hello guys.... how to do this

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

Thank you for using the calculator program.
Last edited on
Hi @nico144,

What do you have so far?

Hello nico144,

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.

Hope that helps,

Andy
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/
What' the problem in my code ???
int n1, n2, sum, difference, product, quotient;
cout << "-----------MENU-----------" << endl;
cout << "[ + ] Add two numbers" << endl;
cout << "[ - ] Subtract two numbers" << endl;
cout << "[ * ] Multiply two numbers" << endl;
cout << "[ / ] Divide two numbers" << endl;
cout << "[ Q ] Exit" << endl;
cout << "---------------------------" << endl;
cout << "Enter your choice : ";

switch (operators)
case '+':
cout << "The sum of " << n1 << "and" << n2 << " is" << endl;
sum = n1 + n2;
break;
case '-':
cout << "The difference of " << n1 << "and" << n2 << " is" << difference << endl;
difference = n1 - n2;
break;
case '*':
cout << "The product of " << n1 << "and" << n2 << " is" << product << endl;
product = n1 * n2;
break;
case '/':
cout << "The quotient of " << n1 << "and" << n2 << " is" << quotient << endl;
break;
default:
cout << "Thank you for using the calculator program." << endl;
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 '/'.
Hello nico144,

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;

Last topic on the page http://www.cplusplus.com/doc/tutorial/control/

The switch needs a set of "{}" to work properly.

coder777 has a good suggestion that you can use.

Hope that helps,

Andy
Always error in switch (operators)

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 <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	int n1, n2, sum, difference, product, quotient;
	cout << "-----------MENU-----------" << endl;
	cout << "[ + ] Add two numbers" << endl;
	cout << "[ - ] Subtract two numbers" << endl;
	cout << "[ * ] Multiply two numbers" << endl;
	cout << "[ / ] Divide two numbers" << endl;
	cout << "[ Q ] Exit" << endl;
	cout << "---------------------------" << endl;
	cout << "Enter your choice : ";
	
	switch (operators){
		case '+':
	   	 cout << "The sum of " << n1 << "and" << n2 << " is" << n1 + n2 << endl;
	   	 sum = n1 + n2;
	   	 break;
	   case '-':
	   	 cout << "The difference of " << n1 << "and" << n2 << " is" << n1 - n2 << endl;
	   	 difference = n1 - n2;
	   	 break;
	   case '*':
	   	 cout << "The product of " << n1 << "and" << n2 << " is" << n1 * n2 << endl;
	   	 product = n1 * n2;
	   	 break;
	   case '/':
	   	 cout << "The quotient of " << n1 << "and" << n2 << " is" << n1 / n2 << endl;
	   	 break;
	}
	   
	  default:
	  	cout << "Thank you for using the calculator program." << endl;
	
	
	
	
   
     
	system("PAUSE");
	return EXIT_SUCCESS;
}
operators must be defined [as char]

default: must be inside the switch() block.

To get the operator you need a cin, another cout for "Enter two numbers : " and cin for n1/n2.
last question. when i type Q to exit it requires a two numbers ??? im almost done with my codes.

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
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
	char operators;
	int n1, n2;
	cout << "-----------MENU-----------" << endl;
	cout << "[ + ] Add two numbers" << endl;
	cout << "[ - ] Subtract two numbers" << endl;
	cout << "[ * ] Multiply two numbers" << endl;
	cout << "[ / ] Divide two numbers" << endl;
	cout << "[ Q ] Exit" << endl;
	cout << "---------------------------" << endl;
	cout << "Enter your choice : ";
	cin >> operators;
	cout << "Enter two numbers : ";
	cin >> n1 >> n2;
	
	switch (operators){
		case '+':
	   	 cout << "The sum of " << n1 << "and" << n2 << " is " << n1 + n2 << endl;
	   	 break;
	   case '-':
	   	 cout << "The difference of " << n1 << "and" << n2 << " is " << n1 - n2 << endl;
	   	 break;
	   case '*':
	   	 cout << "The product of " << n1 << "and " << n2 << " is" << n1 * n2 << endl;
	   	 break;
	   case '/':
	   	 cout << "The quotient of " << n1 << "and  " << n2 << " is " << n1 / n2 << endl;
	   	 break;
	  default:
	  	cout << "Thank you for using the calculator program." << endl;
	  }
	
	
   
     
	system("PAUSE");
	return EXIT_SUCCESS;
}
Hello nico144,

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.
Last edited on
Hi Handy Andy!!!

Can you show me the code? being not equal to "Q" ? and n2 is not equal to zero? how to do that ??? thankyooou
Check for 'Q' right after line 20.
Use if:

1
2
3
4
5
	if(operators != 'Q')
{
	cout << "Enter two numbers : ";
	cin >> n1 >> n2;
}


n2 is similar. You may cout an error in that case.
Hello nico144,

After line 20 something like this:

1
2
3
4
5
6
7
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:

1
2
3
#include <cstdlib>
#include<iomanip>
#include<cmath> 


There is nothing in your code that these files would be used for.

And int main(int argc, char *argv[]) "argc and argv" are never used in the program. All you need is int main()

Hope that helps,

Andy
Last edited on
Thanks a lot guys now i finally finish the problem thankyou :) :) :) :)
Hello nico144,

If you are done with this give the topic a green check so everyone knows its finished.

Andy
Topic archived. No new replies allowed.