Input validation - inputting character to an int variable

I'm writing a trivia game program that gives a menu of options. The way I organized it, the user inputs a value (read into the variable int user_input), and the program does something based on that. The problem comes when the user inputs a character - the program just enters an infinite loop. What can I do to stop this?

Here's the code (well, some of it. I cut out from the beginning and the end, but I'm assuming just about everything but the while() loop is irrelevant.)

(As I said, user_input is an int, not a char, as it probably should be. But I'd rather not rewrite a big chunk of my 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
		cout << "\n5 - Use 50/50 Lifeline" << endl;
		cout << "6 - Use 'Ask an Expert' Lifeine" << endl;
		cout << "0 - Quit game" << endl;

		cout << "Choice: ";
		cin >> user_input;

		if(user_input == 0)
		{
			end_game();
		}

		while((user_input < 1) || (user_input > 6))
		{
			cout << "Invalid input - Please enter a number between 1 and 6 or 0." << endl;
			cin >> user_input;
		}


		if(user_input == 5)
		{
			lifeline_fifty();
			question_menu(user_input);
		}
Add && cin.good() to the while block.
Thanks for the reply, but

while((user_input < 1) || (user_input > 6) && cin.good())

still infinitely runs if I input a character.
Try this:
while( ((user_input < 1) || (user_input > 6)) && cin.good())
Have a read through this, this helped me a lot when I had these problems!
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Last edited on
closed account (zb0S216C)
What if the user enters a character? Maybe consider swapping your current input buffer's type to a char. Here's what I mean:
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
                char user_input( '\0' );
                // ...
                cout << "\n5 - Use 50/50 Lifeline" << endl;
		cout << "6 - Use 'Ask an Expert' Lifeine" << endl;
		cout << "0 - Quit game" << endl;

		cout << "Choice: ";
		cin >> user_input;

		if( user_input == '0' )
		{
			end_game();
		}

		while((user_input < '1') || (user_input > '6'))
		{
			cout << "Invalid input - Please enter a number between 1 and 6 or 0." << endl;
			cin >> user_input;
		}


		if(user_input == '5')
		{
			lifeline_fifty();
			question_menu(user_input);
		}

Using a char as your input buffer type will grant you the ability to control passed characters, not just numbers.
That's how I usually do it:

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

bool GetInt(int & n)
{
    string str;
    getline(cin,str);

    stringstream buffer(str);
    buffer >> n;

    if (!buffer)
    {
        cout << "non numerical data!" << endl;
        return false;
    }

    if (!buffer.eof())
    {
        cout << "buffer not consumed!" << endl;
        return false;
    }

    return true;
}

int main()
{
    int my_int;

    while (true)
    {
        cout << "enter an int (0 to quit): ";

        if (!GetInt(my_int))
        {
            cout << "you did not enter an int..." << endl;
        }
        else
        {
            cout << "you entered an int!" << endl;

            if (my_int==0)
            {
                cout << "ok, bye!" << endl;
                break;
            }
        }
    }

    cout << "\n(hit enter to quit...)" << endl;
    cin.get();

    return 0;
}

enter an int (0 to quit): 123
you entered an int!
enter an int (0 to quit): asdf
non numerical data!
you did not enter an int...
enter an int (0 to quit): 123asdf
buffer not consumed!
you did not enter an int...
enter an int (0 to quit): 0
you entered an int!
ok, bye!

(hit enter to quit...)

I have a similar problem. The way I thought to solve it was to use a string temp and then check that that was an integer then convert and assign that string to the correct int variable. However I'm having trouble with checking and converting the string.

Since you are using single digits I would think about switching to char as mentioned above to give you better control.
Last edited on
Topic archived. No new replies allowed.