Why does my do-while loop continue to loop?

I've set up dozens of do-while loops and they work flawless. But today I must be completely dim. I just can't make my loop stop looping when I type in '0' in my consol.

This is the code I've got:
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
    char choice;    // My variable

    do
    {

        cout << "\tMeny choice      : 1\n"  ;
        cout << "\tMeny choice      : 2\n"  ;
        cout << "\tMeny choice      : 3\n"  ;
                   ..............
        cout << "\tQuit             : 0\n"  ;
        cout << "\t__________________\n\n"  ;
        cout << "\tMake a choice: "         ;


        cin >> choice;    // Give the value of user input to variable 'choice' 

        switch( choice )    // Use a switch to test the value
        {
                case 1      :   // Execute this block
                                break;
                case 2      :   // Execute this block
                                break;
                case 3      :   // Execute this block
                                break;
                .......

        } //END switch

    } //END do and check the variable choice condition

    while( choice !=0 );  // If the variable is not set to '0', keep looping 


What am I missing?

Thanks in advance
Choice should probably be an integer.
choice is a char variable but you are comparing it to int 0. You should compare it to '0'. Similarly the cases in the switch are wrong, they also assume that choice is an int. Probably better to make choice an int.
You can either write such a way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        cin >> choice;    // Give the value of user input to variable 'choice' 

        switch( choice )    // Use a switch to test the value
        {
                case '1'      :   // Execute this block
                                break;
                case '2'      :   // Execute this block
                                break;
                case '3'      :   // Execute this block
                                break;
                .......

        } //END switch

    } //END do and check the variable choice condition

    while( choice != '0' );  // If the variable is not set to '0', keep looping   


Or another way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        cin >> choice;    // Give the value of user input to variable 'choice' 

        choice -= '0';

        switch( choice )    // Use a switch to test the value
        {
                case 1      :   // Execute this block
                                break;
                case 2      :   // Execute this block
                                break;
                case 3      :   // Execute this block
                                break;
                .......

        } //END switch

    } //END do and check the variable choice condition

    while( choice !=0 );  // If the variable is not set to '0', keep looping  
Last edited on
or simply:
 
int choice;
I want the user to be able to use letters and numbers, an 'int' will make it to complicated. I just forgot the ' ' around my numbers. Not the first time ;)

But an other problem just accured... When I puts in several letters/numbers in the consol, the loop keeps repeating the same amount of times.

For example: When I type '112233' in the consol and press enter, case one gets executed twise, then case two gets executed two times and then block three gets executed twise...

If I have a submeny in one of my cases, and someone type in '11' in my headmeny, will the consol enter the submeny case one under case one?

To clearify:
1
2
3
4
5
6
7
8
9
10
    switch( choice )
    {
        case 1: // submeny switch 1
                             // submeny switch1-case 1: something

        case 2: // submeny switch 2
                            // submeny switch2-case 1: something else
       .......
    }


If the user enters '11' by mistake, will this put him/her in the // submeny switch1-case 1: something without showing the meny inside case 1: // submeny switch 1 ?
Last edited on
Someone? Please...
You should clear the input buffer. For example

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

But before using this construction you should include header <limits>
@vlad

Thank you!
Always vlad =) You're the best
Topic archived. No new replies allowed.