Hi, I ran your code (added the missing closing brace), and it seems to loop just fine, and terminates correctly when you enter 4.
I think your problem is that the code in your different cases doesn't actually display the result of the calculation to the user.
You need to print out the values of num1+num2, or num1*num2, etc.
#include<iostream>
usingnamespace std;
int main()
{
int num1,num2,menu;
cout<<"please enter num1";
cin>>num1;
cout<<"please enter num2";
cin>>num2;
cout<<"menu of operation\n:";
cout<<"1.Addition\n:";
cout<<"2.Multiplication\n";
cout<<"3.Subtraction\n";
cout<<"4.Quit\n";
do
{
cout<<"Enter the menu:";
cin>>menu;
switch (menu)
{
int result;
case 1:
result = num1+num2;
cout<<"addition of two numbers: " << result << endl;
break;
case 2:
result = (num1*num2);
cout<<"multiplication of two numbers: " << result << endl;
break;
case 3:
result = (num1-num2);
cout<<"subtraction of two numbers: " << result << endl;
break;
default:
cout<<"invalid menu\n:";
}
}
while (menu!=4);
cout<<"THANK YOU\n";
}
(PS: My browser was acting REALLY slow, so I never saw jonnin's response before posting mine)
--> the fact that it's a do-while means that it will always enter the loop at least once
Lines 7, 10: Your entry of the numbers is before your loop. Your program will operate on the same numbers every time.
Line 27, 32, 37: These statements don't do anything.
Line 42: If the user enters 4 to quit, you're going to display "Invalid menu".
Line 48: You're missing a final }
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/
Hint: You can edit your post, highlight your code and press the <> formatting button.
#include<iostream>
usingnamespace std;
int main()
{
int num1,num2,menu;
int ans; // Need somewhere to put the result
cout<<"menu of operation\n:";
cout<<"1.Addition\n:";
cout<<"2.Multiplication\n";
cout<<"3.Subtraction\n";
cout<<"4.Quit\n";
do
{ cout<<"please enter num1";
cin>>num1;
cout<<"please enter num2";
cin>>num2;
cout<<"Enter the menu:";
cin>>menu;
switch (menu)
{
case 1:
ans = (num1+num2); // Store the result of the calculation somewhere
cout<<"addition of two numbers: " << ans << endl;
break;
case 2:
ans = (num1*num2);
cout<<"multiplication of two numbers: " << ans << endl;
break;
case 3:
ans = (num1-num2);
cout<<"subtraction of two numbers: " << ans << endl;
break;
default:
cout<<"invalid menu\n:";
}
}
while (menu!=4);
cout<<"THANK YOU\n";
system ("pause");
return 0;
}
Your loop and your switch statement are two different statements. The switch statement is inside the loop. You can easily replace the switch with if/else without changing the loop.
#include<iostream>
usingnamespace std;
int main()
{
int num1,num2,menu;
int ans;
cout<<"please enter num1";
cin>>num1;
cout<<"please enter num2";
cin>>num2;
cout<<"menu of operation\n:";
cout<<"1.Addition\n:";
cout<<"2.Multiplication\n";
cout<<"3.Subtraction\n";
cout<<"4.Quit\n";
do
{
cout<<"Enter the menu:"; //when i display 2,its display 2 types of operation
cin>>menu;
if (menu==1)
{
ans = (num1+num2);
cout<<"addition of two numbers: " << ans << endl;
}
if (menu==2)
{
ans = (num1*num2);
cout<<"multiplication of two numbers: " << ans << endl;
}
if (menu==3)
{
ans = (num1-num2);
cout<<"subtraction of two numbers: " << ans << endl;
}
if (menu>4)
{
cout<<"invalid menu\n:";
}
}while (menu!=4);
cout<<"THANK YOU\n";
system ("pause");
return 0;
}