endless loop

The code below works fine with numeric input but gets in an endless loop with alphabetical input. Why?

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
void getUserInput(vector<int> &ui)
{
    int entry=0;
    bool isEntryValid=true;
    system("cls");

    do
    {
        if(isEntryValid==false)
        {
            cout<<"Invalid entry!"<<endl;
        }

        cout<<"Enter numeric input: <1-100>"<<endl;
        cin>>entry;

        if(entry < 1  || entry > 100 )
        {
            isEntryValid=false;
            system("cls");
        }
        else
        {
            isEntryValid=true;
        }

    }
    while (isEntryValid==false);
    ui.push_back(entry);

}

Last edited on
but gets in an endless loop with alphabetical input. Why?

Because the insertion operator>> knows that a number can't contain a non-digit value and if it encounters a non-digit value it places the stream into a failure state. When the stream is in a failure state no further processing will occur with that stream until the error flags are cleared and the offending data is removed from the stream.

if you want to handle the errors, you can read everything as a string, then check to see if the string is a valid value and if so, convert & move it to the target, if not, request a new value until it passes.

different compilers may handle this differently. Mine just assigns zero to entry when junk is put in.
Hello pavik,

Welcome to the forum.

In addition to what jlb says something you could do at like 16 is:

1
2
3
4
5
6
7
8
9
10
while (!cin)
{
    std::cout<<" An error message"<<std::endl;  // <--- Message of your choice.

    cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    std::cout<<"Enter numeric input: <1-100>"<<endl;
    std::cin >> entry;
}


This will catch any non numeric entry and repeat until a valid number is entered.

"cin >> entry" is a formatted input which means since "entry" is defined as an int it expects a number and nothing else.

Hope that helps,

Andy
Thx for the quick reply. Meanwhile I also found two other topics in connection with my question.

http://www.cplusplus.com/forum/articles/6046/
http://www.cplusplus.com/articles/D9j2Nwbp/

I'll check all the given hints.
I modified my code in two ways. First of all converting string to number (http://www.cplusplus.com/forum/articles/6046/) Then in another solution I used Handy Andy's hint. Both are much better than my original one (what else could be :-) but neighter of them are perfect.

When user enters a junk input - leading number - (i.e 9asd) the codes handles inputs like this as a valid entry (i.e. 9)

Why, and what modifications needs to get rid of this phenomenon?
Last edited on
why is because it is valid .. stoi etc type functions can pull out a partial valid response of 9 and ignore the bad stuff. Sometimes that is useful, sometimes not. You can read online help / manuals on functions like that to see what they do with various input, such as http://en.cppreference.com/w/cpp/string/basic_string/stol

a really lazy way to do what you want is a double conversion.
1) convert string to int
2) convert that int back to a string
3) is result of 2 == to original input? if yes, valid, else not... you may need to trim whitespace or something for this to work.
Last edited on
Registered users can post here. Sign in or register to post.