wierd output on input validation

I decided to try and put some input validation in a loop that would add elements to a vector until the user inputted an EOF flag.

everything seems to work fine when i plug in numbers normally, but when i enter a bad input, (line 30 - 36), like a character the program seems to just pause like it's going to take another input, but doesn't seem to be going back into normal program flow, or even start looping infinitly like i thought it might if the input handling failed.

did i misunderstand one of the member functions or am i not resetting the stream buffer correctly?

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
  vector<int> searchVector;

    cout << "Enter any number of integers or press control-z to stop." << endl;

    int nCount = 1, num = 0;

    while( true )
    {

        cout << "Number " << nCount << ": ";
        cin >> num;

        if( cin.good() )
        {

        searchVector.push_back(num);

        }
        else
        {
            if( cin.eof() )
            {
                cin.clear();

                cin.ignore(100, 'n');

                break;
            }
            else
            {
                cout << "Bad input. To exit enter control-z." << endl;

                cin.clear();
                cin.ignore(100, 'n');

            }

        }

        nCount++;

    }
Last edited on
Your ignore() statement should be using the end of line character ('\n') not the 'n' character.

gotta love simple mistakes like that lol. thanks
Topic archived. No new replies allowed.