I first had some troubles and it was because of something SO stupid, SO simple and I really cannot believe I made the mistake and spent so long trying to fix it.
I had my function worked out correctly and tried so many methods to try and get it working, even though I didnt have to, but in the end, all my problems were caused by typing out a declaration...not a function call.
I put 'string' infront of the function call, which ultimately skipped the function all together resulting in an infinite loop.
Here is my new code with the function. If you think it needs any improvements please let me know, and maybe let me know why. (I think that MAYBE I did more than I needed to....maybe).
//Critter Caretaker//Simulates caring for a virtual pet#include<iostream>#include<string>#include<sstream>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";}string getchoice(string& choice);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: "; string line; getchoice(line); // We set 'choice' to an invalid value first. That way, // if the input is garbage, nothing is read in, so it's still // invalid, and the default case of the switch is triggered. choice = -1; stringstream parser(line); parser >> choice; // If you want, you can do additional checking that there is // no extra text at the end of the line, e.g. "76 trombones": char c; if (parser >> c) { choice = -1; } // That way lets the user put spaces and tabs at the end, which // I think is reasonable. If you want to be even stricter, try // using parser.get(). 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; case 5: cout << "ERROR!\n"; break; default: cout << "\nSorry, but " << line << " is not a valid choice.\n"; } }while(choice != 0); return 0; }string getchoice(string& choice){ string line; getline(cin, line); choice = line; return choice;}
I have some questions for you again.
- Say for example I type 546 as my choice.
Is parser >> choice; getting the value of line (546) and seeing if it is an integer value (choice)? I'm still not entirely sure how the parser works.
----------------------
Nobodynews, thankyou for your explanation, unfortunately though, i'm still a little lost. So i'm going to ask a few more questions about your explanation which might help me understand your post a bit better.
- Firstly, what is the difference between an "internal string" and "data from a keyboard"?
- You said "Both of these differences can make stringstream more useful for converting text to an integer than cin". What is the purpose of converting text into an integer?
I probably could ask more, but i'll just leave it at that for now.
Thankyou again Nobodynews for your explanation, i'm sorry that I STILL could not make sense of it all though. I'm sure I will soon enough lol.