Original Post
I am going through the book called "Beginning C++ through games development", i'm up to the end of chapter 8, trying to do the exercises and have come across a niggling problem I keep coming across in nearly all of the examples that ask for user input. The problem is that when a player enters something other than what the user input is asking for, the program enters an infinite loop. The user is supposed to enter an integer value when prompted. This works perfectly fine IF the user enters anything from 0 to 4. However the program enters an infinite loop if the user inputs a value that is NOT AN INTEGER. I have tried to reset the value of 'choice' to a value that is not between 0 and 4, but IS an integer at the end of the loop and/or the beginning, however the compiler just avoids this all together, and repeats the DO loop forever. I also ran the debugger in microsoft visual studio, and I noticed that after the player enters a choice that is a letter or something other than a number between 0 and 4, the value of choice becomes some long negative integer (i'm guessing due to the compiler converting characters and strings to integer?), it then skips over the switch statement (because the value does not equal anything from the switch statement), outputs the message saying the value is not valid, repeats the main loop again, but instead of getting user input, it seems to retain the previous value rather than stop at the user input and allow for a new input. So after all that long winded text, is this a simple problem for a beginner, or is it a little more complex than I think? I swear i've fixed something like this before, but this has me baffled. And what would I need to do to fix this. Also, is it possible to use a tag other than [code] which shows colour coding and so on? Just like in microsoft visual studio. [Yes; the other tag is [source], and I have made the necessary edits for you. --Zahlman] Thanks in advance, here is the following code i'm talking about. [Edited by - Zahlman on February 22, 2010 2:18:31 PM]
//Critter Caretaker
//Simulates caring for a virtual pet
#include<iostream>
using namespace std;
class Critter
{
public:
Critter(int hunger = 0, int boredom = 0);
void talk();
void eat(int food = 4);
void play(int fun = 4);
void stats();
private:
int m_hunger;
int m_boredom;
int GetMood() const;
void PassTime(int time = 1);
};
Critter::Critter(int hunger, int boredom):
m_hunger(hunger),
m_boredom(boredom)
{}
inline int Critter::GetMood() const
{
return (m_hunger + m_boredom);
}
void Critter::PassTime(int time)
{
m_hunger += time;
m_boredom += time;
}
void Critter::talk()
{
cout << "I'm a critter and I feel ";
int mood = GetMood();
if (mood > 15)
cout << "mad.\n";
else if (mood > 10)
cout << "frustrated.\n";
else if (mood > 5)
cout << "okay\n";
else
cout << "HAPPY! :D \n";
PassTime();
}
void Critter::eat(int food)
{
cout << "Brruuppp\n";
m_hunger -= food;
if (m_hunger < 0)
m_hunger = 0;
PassTime();
}
void Critter::play(int fun)
{
cout << "WHEEEE! THIS IS FUN!\n";
m_boredom -= fun;
if (m_boredom < 0)
m_boredom = 0;
PassTime();
}
void Critter::stats()
{
cout << "Creature's Happiness level is: ";
cout << Critter::GetMood() << "\n";
cout << "Creature's Hunger level is: \n";
cout << Critter::m_hunger << "\n";
cout << "Creature's Boredom level is: \n";
cout << Critter::m_boredom << "\n";
}
int main()
{
Critter crit;
crit.talk();
int choice;
do
{
cout << "\nCritter Caretaker\n\n";
cout << "0 - QUIT\n";
cout << "1 - LISTEN TO YOUR CRITTER\n";
cout << "2 - FEED YOUR CRITTER\n";
cout << "3 - PLAY WITH YOUR CRITTER\n\n";
cout << "(DEVELOPERS MENU)\n";
cout << "4 - (LIST CREATURES STATISTICS)\n";
cout << "Please type a number to choose an action\n";
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 0:
cout << "Good-bye.\n";
break;
case 1:
crit.talk();
break;
case 2:
crit.eat();
break;
case 3:
crit.play();
break;
case 4:
crit.stats();
break;
default:
cout << "\nSorry, but " << choice << " is not a valid choice.\n";
}
}while(choice != 0);
return 0;
}