why it didnt loop like i hope?

i try 3 different apps but it didnt looping,it can run but it didnt loop ,is it because wrong structure ??? if it is ,any suggestion should i try ??

#include<iostream>
using namespace 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)
{
case 1:
(num1+num2);
cout<<"addition of two numbers:";
break;

case 2:
(num1*num2);
cout<<"multiplication of two numbers:";
break;

case 3:
(num1-num2);
cout<<"subtraction of two numbers:";
break;

default:
cout<<"invalid menu\n:";

}
}
while (menu!=4);
cout<<"THANK YOU\n";
Last edited on
try initialize menu to something not 4 so it is forced to always enter the loop.

num*num; is legal but does nothing. you will want result = num*num and a cout << stuff << result<< type statement once you have the loop working.

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.

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
50

#include<iostream>
using namespace 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
Last edited on
is looping suitable for any control structure???,for example i wanna use nested if??
Last edited on
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.

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
50
#include<iostream>
using namespace 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;
}


Last edited on
if i dont wanna use switch as main,how to add loop in other control structure like if else or nested if,NOT ANWERS JUST A KEYPOINT
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.
i try making and if-else,tell me what do you think?? ,and unfortunately the looping are not gonna stop,

#include<iostream>
using namespace std;
int main()
{
int num1,num2,menu;
int ans;

cout<<"please enter num1";
cin>>num1;

cout<<"please enter num2";
cin>>num2;

cout<<"Enter the menu:";
cin>>menu;

cout<<"menu of operation\n:";
cout<<"1.Addition\n:";
cout<<"2.Multiplication\n";
cout<<"3.Subtraction\n";
cout<<"4.Quit\n";

do
{


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
cout<<"invalid menu\n:";

}
while (menu!=4);
cout<<"THANK YOU\n";
system ("pause");
return 0;
}
Last edited on
You do not read menu in your loop so it won't end.

also


if
cout<<"invalid menu\n:";

is pure gibberish. It looks like an incomplete thought.
Last edited on
guys i make some mistake ,its on menu thing ,when i press menu 2 its display 2 types of operation how do i correct this

#include<iostream>
using namespace 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;
}
closed account (48T7M4Gy)
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
50
51
52
53
#include<iostream>

using namespace 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;
}
errr ...thanks ??
errr ...thanks ??
Does it mean you can't see the differences between your code and kemort's one?
Your code:
1
2
3
4
5
{
if (menu==1);
ans = (num1+num2);
cout<<"addition of two numbers: " << ans << endl;
}


kemort's code:
1
2
3
4
5
if (menu==1)
{
    ans = (num1+num2);
    cout<<"addition of two numbers: " << ans << endl;
}


Perhaps it would have been simpler for you if you'd used the code-tags too :)
i gave up,i use case instead of if else ,sorry
closed account (48T7M4Gy)
All you had were the braces {}'s ... in the wrong places.

A switch control is a better choice but I dared not mention it at the time.
Topic archived. No new replies allowed.