Jun 7, 2012 at 10:00am UTC
would some one be so kind to explain me how to get user input in the example
the part that i want is at the end where i commented
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Monster
{
public :
//constructor
Monster(){cout << "\n\tbuilding a Monster" ;}
~Monster(){cout << "\n\tDestroying a Monster" ;}
//member methods
void rampage() {cout<<"\n\tMonster rampagin through the city!" ;}
void DisplayStats()
{
cout<<"\n\n\t----------Monster Stats---------" ;
cout<<"\n\tName: " << MonsterName;
cout<<"\n\tLife: " << LIFE;
cout<<"\n\tSize: " << SIZE;
cout<<"\n\tWeight: " << WEIGHT;
cout<<"\n\t----------------------------------" ;
}
//Accessor Methods
int GetLife(){return LIFE;}
void SetLife (int x){LIFE=x;}
double GetSize(){return SIZE;}
void SetSize (double x){SIZE=x;}
double GetWeight(){return WEIGHT;}
void SetWeight (double x){WEIGHT=x;}
string GetMonsterName(){return MonsterName;}
void SetMonsterName (string x){MonsterName=x;}
private :
int LIFE;
double SIZE;
double WEIGHT;
string MonsterName;
};
//--------------------------------------------------------
class Dragon : public Monster
{
public :
Dragon(){ cout << "\n\tBuilding a dragon." ; }
~Dragon(){ cout << "\n\tDestroying a dragon." ; }
void BreatheFire() {cout << "\n\tDragon breathing fire" ;}
};
//----------------------------------------------------------
class Unicorn : public Monster
{
public :
Unicorn(){ cout << "\n\tBuilding a Unicorn." ; }
~Unicorn(){ cout << "\n\tDestroying a Unicorn." ; }
void prance () {cout << "\n\tUnicorn prancing" ;}
};
//--------------------------------------------------------
int main()
{
//stack
Monster Godzilla;
Godzilla.SetLife(500);
Godzilla.SetSize(1000);
Godzilla.SetWeight(5000.8);
Godzilla.SetMonsterName("Godzilla" );
Godzilla.DisplayStats();
//the heap (free store)
Monster * Mothra = new Monster();
Mothra->SetLife(300);
Mothra->SetSize(500);
Mothra->SetWeight(1000.5);
Mothra->SetMonsterName("Mothra" );
Mothra->DisplayStats();
/* i would like to know how can i get user input for this , should i use cin.getline or how do i do ??
Monster ;
Monster.SetLife();
Monster.SetSize( );
Monster.SetWeight();
Monster.SetMonsterName(" ");
*/
//---------- i was thinking something like
cout << " new monster name " ;
cin.getline ( ); // but i don't know what should i put inside or how to get the user input
cout << "set starting life " ;
cin.getline ( );
cout << "set size " ;
cin.getline ( );
cout << "set weight " ;
cin.getline ( );
cout << "monster name " ;
cin.getline( );
cout << "\n\n\t" ;
return 0;
}
thanks
Last edited on Jun 7, 2012 at 10:02am UTC
Jun 7, 2012 at 10:29am UTC
1 2
cout << " new monster name " ;
cin >> MonsterName;
Last edited on Jun 7, 2012 at 10:30am UTC
Jun 7, 2012 at 10:40am UTC
thanks again for the answer i tried but i get
error: 'MonsterName' was not declared in this scope|
also after i give the monster a name how do i add the attributes to the monster ?