Why is this entering an infinite loop?

Super super beginner here! Trying out loops etc. See my code below. If the user enters a character, the program enters an infinite loop. Why? I know it's not the correct variable type, but surely it'd just put the ASCII code into the int variable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

  #include <iostream>

int main()
{
    using namespace std;
    int userSelection = 0;
    bool validSelection = true;
    
   do
    {
        cout << "1. New Project" << endl << "2. Open Exisiting Project" << endl << "Make a Selection: ";
        cin >> userSelection;
        if (userSelection != 1 && userSelection != 2)
            validSelection = false;
            cout << "Invalid Selection" << endl;
    } while (validSelection == false);
    cout << "Good choice";
    return 0;
}

On line 15 you change validSelection to false, but you never change it back.
Hence the loop ends immediate or not at all.
http://stackoverflow.com/questions/5864540/a-simple-question-about-cin

Your cin is meant to accept ints, not strings or chars. Going against that puts cin into a fail state. Because of that your cin is not accepting any more prompts until that's fixed.
Between lines 11 and 12 add validSelection = true;
At the end of line 14 add {
Between lines 16 and 17 add }
closed account (3qX21hU5)
As Olysold said when you enter a char or string it put the stream (cin) into a error state because you variable is meant to accept int (whole numbers) only.

You will need to correct the streams state in order to clear the infinite loop. This is actually a quite common error checking procedure. Here is how it can be done.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    if (userSelection != 1 && userSelection != 2)
        {
            validSelection = false;
            cout << "Invalid Selection. Please enter it again." << endl;
            
            // Clears the streams error state.
            cin.clear();
            
            // Cleans up all the extra characters in the stream
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            
            // Get the user input again.
            cin >> userSelection;
        }


That is how you can change it.
Thanks for the replies. It's not the fact of not setting the validSelection back to true (because then the infinite loop would still stop at prompting the user at cin. But this code enters an infinite loop and won't stop outputting unless you kill the program!)
So the others guys (Olysold & Zereo) must be right. The fix is a little past my understand at this point, so I'll take it not to put ASCII into an int!
And yeah I noticed the error in my if statement by not using { } but that also wouldn't affect my problem.
Thanks again
Menu operations are usually best handled with a switch IMO, include a quit case & make sure there is a default clause to handle bad input. Put the whole thing in a while loop which has bool value as an end condition, so that it loops on the bad input.

Sorry Zereo :), but I really dislike things like this:

if (userSelection != 1 && userSelection != 2)

IMO, they just look ugly & are not scalable - a switch is much easier to read & understand.

@mogglespears if you google some of my previous posts - you will see lots of examples of how this can be done.

Hope all goes well.
closed account (3qX21hU5)
Lol no problem at all IdeasMan wasn't my code to start out with ;p was just using the OP code.
Last edited on
@TheIdeasMan thanks
Topic archived. No new replies allowed.