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();
}
|