Switch statement infinite loop validation bug

I have written similar code before and I can not figure out what is wrong this time. All variables are declared as int and assigned to 0 prior to this section.( with exception to "people as 1")

The issue: When the user inputs a letter I get an infinite loop running the default case. I tried clearing the cin buffer and assigning the selecter variable back to 0 and neither option fixed my loop. Any insight would be appreciated.

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
for(; selecter!=-1; people++)
    {
    cout<<"Please input person number "<<people<<"'s favorite  drink.\n";
    cout<<"1 for Coffee.\n";
    cout<<"2 for Tea.\n";
    cout<<"3 for Coke.\n";
    cout<<"4 for Orange Juice.\n";
    cout<<"-1 to end survey with totals.\n";
    cout<<"Selection: ";
    cin>>selecter;
    switch (selecter)
        {
            case 1:
                coffee++ ;
                break;

            case 2:
                tea++ ;
                break;

            case 3:
                coke++ ;
                break;

            case 4:
                oj++ ;
                break;

            case -1:
                people--;//Dummy to end loop. returns "people" back to original number.
                break;

            default:
                cout<<"That is an invalid entry, please try again."<<endl;
                people--;
                break;
        }

        cout<<endl;
    }
Hi,

Try this:

1
2
3
4
5
6
7
8
9
bool Quit = false;

while(!Quit) {
   switch

  case -1:
     Quit = true;

}


The menu should be a function.
Tried that and the behavior has not changed here is the complete program:

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
#include <iostream>

using namespace std;

int main()
{
int coffee=0;
int tea=0;
int coke=0;
int oj=0;
int selecter=0;
int people=1;
bool quit=false;

while(quit==false)
    {
    cout<<"Please input person number "<<people<<"'s favorite  drink.\n";
    cout<<"1 for Coffee.\n";
    cout<<"2 for Tea.\n";
    cout<<"3 for Coke.\n";
    cout<<"4 for Orange Juice.\n";
    cout<<"-1 to end survey with totals.\n";
    cout<<"Selection: ";
    cin>>selecter;
    switch (selecter)
        {
            case 1:
                coffee++ ;
                break;

            case 2:
                tea++ ;
                break;

            case 3:
                coke++ ;
                break;

            case 4:
                oj++ ;
                break;

            case -1:
                people--;
                quit=true;
                break;

            default:
                cout<<"That is an invalid entry, please try again."<<endl;
               // selecter=0;
                people--;
                break;
        }
        people++;
        cout<<endl;
    }
    people--;
    cout<<people<<" have been surveyed.\n";
    cout<<"The results:\n";
    cout<<"Coffee: "<<coffee<<endl;
    cout<<"Tea: "<<tea<<endl;
    cout<<"Coke: "<<coke<<endl;
    cout<<"Orange Juice: "<<oj<<endl;

}


(have tried with and without the selecter variable commented out in the default case.)
Last edited on
> When the user inputs a letter I get an infinite loop running the default case.

When the attempted input fails, the stream is put into a failed state;
the stream must be put back into a good state before any further i/o can be performed.
In addition, the character(s) that caused input failure remain in the input buffer; these must be removed.

Something like this perhaps:
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
#include <iostream>

int main()
{
    int coffee = 0, tea = 0, juice = 0, coke = 0 ;

    int selection = 0 ;
    while( selection != -1 )
    {
        const int person_number = coffee+tea+juice+coke + 1 ; // starts at one
        std::cout << "please input person #" << person_number << "'s favourite drink\n"
                  << "1 Coffee  2 Tea  3 Coke  4 Juice   "
                  << "enter -1 to end survey\nselection? " ;

        selection = 0 ;

        // if the user entered an int, switch on the selection
        if( std::cin >> selection ) switch(selection)
        {
            case 1 : ++coffee ; break ;
            case 2 : ++tea ; break ;
            case 3 : ++coke ; break ;
            case 4 : ++juice ; break ;
            case -1 : break ; // end of input: do nothing
            default: std::cout << "invalid number, please try again.\n" ;
        }

        else // input failure: eg. the user entered a alpha character
        {
            std::cout << "bad input: please enter an integer\n" ; // inform the user
            
            // http://en.cppreference.com/w/cpp/io/basic_ios/clear 
            std::cin.clear() ; // put the stream back into a good state
            
            // http://en.cppreference.com/w/cpp/io/basic_istream/ignore 
            std::cin.ignore( 1000, '\n' ) ; // and remove the bad input remaining in the input buffer
        }
    }

    const int npersons = coffee+tea+juice+coke ;

    // print results. eg.
    std::cout << "#persons: " << npersons << '\n'
              << "  coffee: " << coffee << '\n' ;
              // etc.
}
I had tried adding the cin.clear() line before and thought that was all I needed forgot about cin.ignore( 1000, '\n' ) Still taking my first few steps into c++ and my understanding of the buffer is still a bit lacking. Guess I have a topic to bring to class next week. The code runs correctly now. Thank you very much.
Topic archived. No new replies allowed.