Case label not within a switch statement


Case label not within a switch statement ,where am i wrong can help me please??
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 //My Calculator
#include <iostream>


using namespace std;
int main(){
    string name1;
    int choice = 0;
    int sum;
    int totalsum = 0;
    int sum2;
    int totalsum2 = 0;
    int sum3;
    int totalsum3 = 0;
    int sum4;
    int totalsum4 = 0;

    cout << "Hi user whats your name: "<< name1 << endl;
    cin >> name1;
    cout << name1 << " you want to:\n1.Add\n2.Subtract\n3.Multiply\n4.Division"<< endl;
    cin >> choice;

    switch(choice)
        case 1:
            cout <<"What you want to add: ";
            cin >> sum;
            while(0<sum){
                totalsum = totalsum + sum;

                cout<<"What is your next number to add or -1 to quit: ";
                cin >>sum;
break;

            }

    cout <<"Your total is : "<<totalsum << endl;

\
        case (2):
            cout <<"What you want to subtract: ";
            cin >> sum2;
            while(0<sum2){
                totalsum2 = totalsum2 - sum2;

                cout<<"What is your next number to subtract or -1 to quit: ";
                cin >>sum2;

break;
            }

        case (3):
            cout <<"What you want to multiply: ";
            cin >> sum3;
            while(0<sum3){
                totalsum3 = totalsum3 * sum3;

                cout<<"What is your next number to multiply or -1 to quit: ";
                cin >>sum3;

break;
            }

        case (4):
            cout <<"What you want to divide: ";
            cin >> sum4;
            while(0<sum4){
                totalsum4 = totalsum4 / sum4;

                cout<<"What is your next number to divide or -1 to quit: ";
                cin >>sum4;
break;
                }
        default:
            cout <<"That is 4 choice there."<< endl;


            }


    }

Last edited on
You're breaking out of the while loops, not the case statement.

Try this instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
switch(choice)
{
    case 1:
        cout << "What do you want to add: ";
        cin >> sum;
        while(0 < sum)
        {
            // ...
        }
        break;  // <---- break statement goes outside while loop!

    // ...
}

one more error :break statement not within loop or switch.



1
2
3
4
5
6
7
8
    cout <<"Your total is : "<<totalsum << endl;
    break;

                case (2):
            cout <<"What you want to subtract: ";
            cin >> sum2;
            while(0<sum2){
                totalsum2 = totalsum2 - sum2;
You need to enclose the contents of the switch statement in braces, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
    switch(choice)
    {
    case 1:
        // Do stuff
        break;

    case 2:
        // Do stuff
        break;

    // etc
    }
    
still cant after i put if/else statement T.T
still cant after i put if/else statement T.T

I have no idea what that means. Can you try and be a bit clearer? What is it that you still can't do? What if/else statement?
You say do stuff means this?



1
2
3
4
5
6
7
    if(totalsum<0){
        cout <<"What are you calculating?" << endl;
    }
    else{
        cout <<"The answer is "<< totalsum << endl;
    }
    break;
"Do stuff" means whatever it is that you want to happen in that particular case. I'm not writing the code for you - I'm demonstrating the correct syntax for a switch/case statement.

but how can i do stuff in it ??? add what inside, can help ??
You write code! You were doing it in the code you originally posted, so why are you having trouble understanding it now?
Topic archived. No new replies allowed.