help with my first C++ project
- Musician
-
Musician
- Member since: May. 19, 2005
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
Hey I've got a problem. I set up this text RPG script that i'm going to use for a game but there's a minor glitch with it. you see I have this integer variable called choice. and when you have to make a decision you make an input into choice and it will select the right output depending on which number you choose. Problem is, if you enter anything other than an integer, the whole game goes into an infinite loop and continues pasting the choices over and over.
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Welcome to...\n";
cout << "HARRISEN QUEST\n";
cout << "a game developed by Jonathan McEntee\n\n";
cout << "Press any Key to continue\n";
cin.ignore(1);
while (choice != 5){
cout << "\nYou awaken to find that X has occured\n";
cout << "Option 1: ...\n";
cout << "Option 2: ...\n";
cout << "Option 3: ...\n";
cout << "Option 4: ...\n";
cout << "Option 5: Exit Game\n";
cout << ">";
cin >> choice;
switch(choice){
case 1:
{
cout << "\ntest1\n\n";
break;
}
case 2:
{
cout << "\ntest2\n\n";
break;
}
case 3:
{
cout << "\ntest3\n\n";
break;
}
case 4:
{
cout << "\ntest4\n\n";
break;
}
}
}
return 1;
}
I have no country to fight for; my country is the earth; I am a citizen of the world
-- Eugene Debs
- authorblues
-
authorblues
- Member since: Jun. 21, 2005
- Offline.
-
- Forum Stats
- Member
- Level 12
- Blank Slate
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Welcome to...\n";
cout << "HARRISEN QUEST\n";
cout << "a game developed by Jonathan McEntee\n\n";
cout << "Press any Key to continue\n";
cin.ignore(1);
do
{
cout << "\nYou awaken to find that X has occured\n";
cout << "Option 1: ...\n";
cout << "Option 2: ...\n";
cout << "Option 3: ...\n";
cout << "Option 4: ...\n";
cout << "Option 5: Exit Game\n";
cout << ">";
cin >> choice;
switch(choice){
case 1:
cout << "\ntest1\n\n";
break;
case 2:
cout << "\ntest2\n\n";
break;
case 3:
cout << "\ntest3\n\n";
break;
case 4:
cout << "\ntest4\n\n";
break;
}
} while (choice != 5);
return EXIT_SUCCESS;
}
- StrixVariaXIX
-
StrixVariaXIX
- Member since: Nov. 18, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Gamer
You have to store "choice" in a char instead of an int if you want to do error checking like this. Entering a char when an int is expected gives the error you described every time. Once you change the variable to a char, check for '1' and '2' (with the single quotes exactly as I showed it) instead of 1 or 2. This should fix it.
- amaterasu
-
amaterasu
- Member since: Mar. 7, 2004
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
Or you can just have logic to test the input, and prints an error if the wrong input is entered.
if((choice < '1') || (choice > '4'))
{
cout << "Wrong choice! Please try again.\n";
continue;
}
Characters are just numeric values. The input from cin is the numeric value of the character you entered. You don't really have to cast it to a char type. The above logic will work just fine.
This works because it evaluates choice as a numeric value. The value for '1' is 31, and '4' is 34, in hex. The conditional makes sure that the input is between 31 and 34, therefor makes sure that it is '1' '2' '3' or '4'.
continue causes the loop to advance to the next iteration, skipping any other code.
beep
- Musician
-
Musician
- Member since: May. 19, 2005
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
hey thanks I actually ended up changing the choice variable to a string (included the string library).
I have no country to fight for; my country is the earth; I am a citizen of the world
-- Eugene Debs
- Jon-86
-
Jon-86
- Member since: Jan. 30, 2007
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
Why not just put a 'default' in your case statments?
Then if they dont enter an available option you just show them an error message.

