help using while and switch statement

Hello,

I am creating a code for class. The assignment is to make a menu and asking the user to pick 1 of 3 options. once they pick the option my code jumps into a while statement. My problem is once they pick an option, like 1, my code will not go to case 1 but instead jumps back to the beginning of the code. is there a syntax error in my code? Any help is appreciated. Thanks!

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
  int startNum;

    while (startNum != 3) //first loop
    {
        int magicNum, magicNum2;
        int tensDigit, onesDigit; // variables used for case 1

        cout << "Marcos Ortiz\n";
        cout << "100627948\n";
        cout << " \n";

        cout << "Pick one of the three options\n";
        cout << "1. Almost Always 99\n";
        cout << "2. Always 3\n";
        cout << "3. Exit\n";
        cin >> startNum;

        switch (startNum)
        {

            case'1':        //first option

                cout << "Pick a number from 0 up to 99\n";
                cin >> magicNum;

                if (magicNum < 10)
                {
                    magicNum = magicNum * 10;
                    break;

                }
                cout << magicNum << endl; //keep track of input value

              





        }
    }
startNum is an int, on line 21 case'1': you write '1' which is a char, since startNum is an int, this case will never be called. try case 1: instead. Also don't forget to break at the end of your case.

Hope this helps,

Regards,

Hugo.
Last edited on
wow. Such a small mistake xD. Thank you !
My pleasure! ;)
Topic archived. No new replies allowed.