Looping when user input is not the specified number.

Hi everyone, this is my first post on here so apologies if it hasn't been done right.

I need help with this particular loop in my quiz game. Basically everything in the game works fine now apart from this. So what I would like to do is: If the user types in anything other than the specified numbers the loop should loop around until they do, as this input is needed to continue in the game properly.

The loop works if any incorrect number is entered properly, but whenever a character or letter is inputted, the program just outputs the same line of "Please type either 1, 2, 3 or 4." over and over.

Is there a special type i can set the input "house" to so that it accepts both int and char? Also I have tried using char and unsigned char instead and that doesn't seem to work, it just causes more issues.

Also, this is just the function in the quiz that needs fixing.

My code is below, thanks in advance!:

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
  void welcome()
{

	cout << "Welcome To The Harry Potter Quiz\n\n" << endl;

	cout << "Please enter your name: " << endl;

	cin >> name;

	cout << "\n\nHello " << name << ", please pick your house by typing the corresponding number and pressing enter:" << endl;

	cout << "\n1. Gryffindor\n2. Ravenclaw\n3. Slytheirn\n4. Hufflepuff\n" << endl;

	bool houseLoop = true;

	do
	{
		cin >> house;

		if (house == 1)
		{
			cout << "\nOkay " << name << ", You are now in Gryffindor. Good choice!" << endl;
			houseSelect = "Gryffindor";
			houseLoop = false;
			break;
		}
		else if (house == 2)
		{
			cout << "\nOkay " << name << ", You are now in Ravenclaw. Good choice!" << endl;
			houseSelect = "Ravenclaw";
			houseLoop = false;
			break;
		}
		else if (house == 3)
		{
			cout << "\nOkay " << name << ", You are now in Slytheirn. Good choice!" << endl;
			houseSelect = "Slytheirn";
			houseLoop = false;
			break;
		}
		else if (house == 4)
		{
			cout << "\nOkay " << name << ", You are now in Hufflepuff. Good choice!" << endl;
			houseSelect = "Hufflepuff";
			houseLoop = false;
			break;
		}
		else
		{
			cout << "\nPlease type either 1, 2, 3 or 4.\n";
			houseLoop = true;
		}
	} while (houseLoop = true);

	cout << "\n\nPress enter to continue";
	_getch();
}
If you only need to deal with single-digit numbers (0 to 9 inclusive) then char would do. But you would need to change the code to match:
1
2
3
4
    char house;
    cin >> house;

    if (house == '1')


Note the use of single quotes, '1' is a char, the same type as the variable house.
You haven't shown the declaration for house. I'm assuming it's an int.

If the user enters a non-numeric character, cin will set it's fail bit if you're trying to cin an int.. In order to proceed, you have to clear the fail bit and remove the undesired characters from the cin buffer.
1
2
  cin.clear ();  // Clear the fail bit
  cin.ignore (1000);  // Remove any extraneous characters from the cin buffer 



Thanks everyone, I have used the solution on Chervil and it's working great now!

Thanks again!
Topic archived. No new replies allowed.