Be a Supporter!

help with my first C++ project

  • 497 Views
  • 5 Replies
New Topic Respond to this Topic
Musician
Musician
  • Member since: May. 19, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
help with my first C++ project 2007-04-30 21:55:59 Reply

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
Response to help with my first C++ project 2007-04-30 23:15:21 Reply

#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;
}


BBS Signature
StrixVariaXIX
StrixVariaXIX
  • Member since: Nov. 18, 2003
  • Offline.
Forum Stats
Member
Level 30
Gamer
Response to help with my first C++ project 2007-04-30 23:40:23 Reply

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
Response to help with my first C++ project 2007-05-01 12:23:19 Reply

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

BBS Signature
Musician
Musician
  • Member since: May. 19, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to help with my first C++ project 2007-05-01 20:45:49 Reply

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
Response to help with my first C++ project 2007-05-02 16:22:26 Reply

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.


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature