#include "critter.h"
int main()
{
critter Animal;
std::string name;
int choice;
std::cout << "What would you like to do: " << endl;
std::cout << "1: Set name" << endl;
std::cout << "2: Set bordom" << endl;
std::cout << "3: Set hunger" << endl;
std::cout << "4: Get name" << endl;
std::cout << "5: Get bordom" << endl;
std::cout << "6: Get hunger" << endl;
std::cout << "7: Talk" << endl;
std::cout << "8: Feed" << endl;
std::cout << "9: Play" << endl;
std::cout << "10: Quit" << endl;
std::cin >> choice;
switch (choice)
{
case 1:
std::cout << "What would you like to name your critter?";
std::getline (std::cin,name);
Animal.SetName(name);
break;
case 2:
std::cout << "What Would you like to set the bordom to?" << endl;
break;
}
return 0;
}
//Critter Class definition is within header file
#ifndef _CRITTER_H
#define _CRITTER_H
#include <iostream>
#include <string>
usingnamespace std;
class critter
{
public: // begin public section
// constructor
critter();
// member functions
int GetHunger();
int GetBordom();
string GetName();
void SetHunger(int hunger);
void SetBordom(int bordom);
void SetName(string name);
private: // begin private section
int myHunger;
int myBordom;
string myName;
};
critter::critter()
{
myHunger = 0;
cout << "A new critter has been born!" << endl;
}
int critter::GetHunger()
{
return myHunger;
}
int critter::GetBordom()
{
return myBordom;
}
string critter::GetName()
{
return myName;
}
void critter::SetHunger(int hunger)
{
if (hunger < 0)
cout << "You can't set a critter's hunger to a negative number.\n\n";
else
myHunger = hunger;
}
void critter::SetBordom(int bordom)
{
if (bordom < 0)
cout << "You can't set a critter's bordom to a negative number.\n\n";
else
myBordom = bordom;
}
void critter::SetName(string name)
{
myName = name;
}
#endif
output
1 2 3 4 5 6 7 8 9 10 11 12 13 14
A new critter has been born!
What would you like to do:
1: Set name
2: Set bordom
3: Set hunger
4: Get name
5: Get bordom
6: Get hunger
7: Talk
8: Feed
9: Play
10: Quit
1
What would you like to name your critter?Press any key to continue . . .
After you enter 1 and press enter there is a newline in the stream which stops getline. Change your getline call to std::getline (std::cin>>std::ws,name);http://www.cplusplus.com/reference/istream/ws/