Comment me

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <iostream>
#include <windows.h>
#include <fstream>
#include <ctime>
using namespace std;
void updateThingsHere();
int sec;
int hisHappines, hisHunger, hisEnergy;
string pet;
// class animal
class animal
{
      public:
             animal(){age = 0, happines = 100, hunger = 100, energy = 100;}
             ~animal(){}
             //get things
             int getAge ()         {return age;}
             int getHappines ()    {return happines;}
             int getHunger()       {return hunger;}
             int getEnergy()       {return energy;}
             
             void growUp () ;
             //set things
             void setHappines (int hisHappines)  {happines = hisHappines;}
             void setHunger   (int hisHunger)    {hunger = hisHunger;}
             void setEnergy   (int hisEnergy)    {energy = hisEnergy;}
             
             void sleep() {cout << "snoring,, dreaming\n";}
                    
      protected: 
               int age;
               int happines;  
               int hunger; 
               int energy;                
};

void animal::growUp()
{
     if (sec + clock() > 10000&& sec + clock() < 100000)
     {
                 age = 1 ;
     }
     if (sec + clock() > 100000 && sec + clock() < 200000)
     {
                 age = 2;
     }
     if (sec + clock() > 200000 && sec + clock() < 300000)
     {
                 age = 3;
     }
     if (sec + clock() > 300000 && sec + clock() < 400000)
     {
                 age = 4;
     }
     if (sec + clock() > 400000 && sec + clock() < 500000)
     {
                 age = 5;
     }
     if (sec + clock() > 500000 && sec + clock() < 600000)
     {
                 age = 6;
     }
     if (sec + clock() > 600000 && sec + clock() < 700000)
     {
                 age = 7;
     }
     if (sec + clock() > 700000 && sec + clock() < 800000)
     {
                 age = 8;
     }
     if (sec + clock() > 800000 && sec + clock() < 900000)
     {
                 age = 9;
     }
     if (sec + clock() > 900000 && sec + clock() < 1000000)
     {
                 age = 10;
     }
     if (sec + clock() > 1000000 && sec + clock() < 1100000)
     {
                 age = 11;
     }
     if (sec + clock() > 1100000 && sec + clock() < 1200000)
     {
                 age = 12;
     }
     if (sec + clock() > 1200000 && sec + clock() < 1300000)
     {
                 age = 13;
     }
     if (sec + clock() > 1300000 && sec + clock() < 1400000)
     {
                 age = 14;
     }
     if (sec + clock() > 1400000 && sec + clock() < 1500000)
     {
                 age = 15;
     }
     if (sec + clock() > 1500000 && sec + clock() < 1600000)
     {
                 age = 16;
     }
     
}

//class cat
class cat : public animal
{
      public:
             
             void speak(); 
             void eat(int food)      {hunger = hunger + food;}
             void tickle()           {happines = happines + 5;}
             void skein()            {happines = happines + 30;}
             void out()             {happines = happines + 3;}   
             
            void downEnergy()       {if (energy > 20) {energy = energy - 20;}}
            void downHappines()     {if (happines > 35) {happines = happines - 35;}}
            void downHunger()       {if (hunger > 30) {hunger = hunger - 30;}}   
};

void cat::speak()
{
     int speak = (rand()% 3)+1;
           switch(speak)
           {
              case 1:      
              cout << "mnau";
              break;
              case 2:
              cout << "grrrrrrrr grrrrrr";
              break;
              case 3:
              cout<< "mnauuu, mnau, grrrrrr";
              break;
           }
}

//class dog
class dog: public animal
{
    public: 
            void speak();
            void eat(int food)      {hunger = hunger + food;}
            void fetch()            {happines = happines + 15;}
            void sit()              {happines = happines + 10;}
            void stay()             {happines = happines + 5;}
            
            void downEnergy()       {if (energy > 20) {energy = energy - 20;}}
            void downHappines()     {if (happines > 35) {happines = happines - 35;}}
            void downHunger()       {if (hunger > 30) {hunger = hunger - 30;}}
};

void dog::speak()
{
     int speak = (rand()% 3)+1;
           switch(speak)
           {
              case 1:      
              cout << "haf";
              break;
              case 2:
              cout << "vrrrrrrrr haf haf";
              break;
              case 3:
              cout<< "haf haf haf haf!!!";
              break;
           }
}
// main starts here
int main()
{
    animal cAnimal();
    dog rex;
    cat muro;
    string name;
    back2:
    cout << "Would you like to load the game or play a new game?"<<endl;
    cout << "(1)  new game  (2)  load game: " ;
    int game;
    back:
         // loading
          if (true)
    {
    ifstream save ("animals.txt");
    save >>pet>> sec>>hisHappines >>hisHunger >> hisEnergy>>name;
    }
          if (hisHappines!= 0 || hisHunger != 0 || hisEnergy != 0)
          {
             rex.setHappines(hisHappines);
             rex.setHunger(hisHunger);
             rex.setEnergy(hisEnergy);
             muro.setEnergy(hisEnergy);
             muro.setHunger(hisHunger);
             muro.setHappines(hisHappines);
          }
// menu
    cin >> game;
    if (game == 1)
       {
back1:
             cout << "Please choose what kind of pet you would like to play with. Write down cat or \ndog: ";    
             cin >> pet;
             if (pet == "dog")
             {
                rex.setHappines(100);
                rex.setHunger(100);
                rex.setEnergy(100);
                sec = 0;
             }
             else if (pet == "cat")
             {
                muro.setHappines(100);
                muro.setHunger(100);
                muro.setEnergy(100);
                sec = 0;
             }
             else
             {
                cout << "You need to choose in between dog and cat and spell it corectly\n";
                goto back1;
             }
        cout << "Please enter the name of your pet: ";
        cin >> name;
        goto forward;
        }
    else if (game > 2 || game < 1)
        {
            cout << "You must choose in between number 1 and number 2: ";
            goto back;
        }
        else if (game == 2)
  {
     

forward:
         bool fEnd = true;
         srand (time(0));
         int death = (rand () % 2600000) + 1600001;
//dog
        if (pet == "dog")
        {
                double startTime = GetTickCount();
                while (fEnd)
                      {
                            
		double currentTime = GetTickCount() - startTime;

		if( currentTime >= 15000) 
		{
			rex.downEnergy();
            rex.downHappines();
            rex.downHunger();
			//Reset the timer.
			startTime = GetTickCount();
		}
                            
                             rex.growUp();
                             if (sec >= death)
                                {
                                    cout << "I am really sorry but " << name << " died. It was too old. Would you like another pet?: y/n";
                                    string end;
                                    cin>> end;
                                    
                                    if (end == "y")
                                    {
                                            goto back1;
                                    }
                                    else
                                      {
                                      cout << "Thank you for playing. Please press enter to quit.";
                                      cin.ignore();
                                      cin.get();
                                      return 0;
                                      }
                                }
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
 switch (choice)
                                    {
                                    case 0:
                                    char progres;
                                    cout << "Do you want to save the progres?? y/n\n";
                                    cin>> progres;
                                    if (progres == 'y')
                                        {
                                          fEnd = false;
                                          break;
                                        }
                                    else
                                        {
                                           cout << "Thank you for playing. Please press enter to quit.";
                                           cin.ignore();
                                           cin.get();
                                           return 0;
                                        }
                                    case 1:
                                         system("cls");
back3:
                                         if (rex.getHunger()<=100)
                                         cout <<rex.getHunger()<<"% full"<<endl;
                                         if (rex.getHunger()>100)
                                         {
                                            rex.setHunger(100);
                                            cout <<rex.getHunger()<<"% full"<<endl;
                                         }
                                         cout << "Choose what food you want to give to " <<name<<endl;
                                         cout << "(0) back  (1) meat - full +25  (2) water - full +5 (3) sweets -  full +15 (4) bread - full + 10:\n";
                                         int food;
                                         cin>>food;
                                         if (rex.getHunger()<100)
                                         {
                                         switch (food)
                                                {
                                                case 0:
                                                       goto forward1;
                                                case 1:
                                                       rex.speak();
                                                       cout<<"\nahhhh\n";
                                                       rex.eat(25);
                                                       rex.speak();
                                                       cout << endl;
                                                       break;
                                                case 2:
                                                       rex.eat(5);
                                                       cout<<"ahhhh\n";
                                                       rex.speak();
                                                       cout << endl;
                                                       break;
                                                case 3: 
                                                       rex.speak();
                                                       cout<<"\nahhhh";
                                                       rex.eat(15);
                                                       cout << endl;
                                                       break;
                                                case 4:
                                                       rex.speak();
                                                       rex.eat(10);
                                                       cout<<"\nahhhh\n";
                                                       rex.speak();
                                                       cout << endl;
                                                       break;
                                                }
                                        goto back3;
                                        }
                                        else
                                        rex.speak();
                                        Sleep(1500);
forward1:
                                        system("cls");
                                        break;
                                    case 2:
                                         system("cls");
                                         rex.speak();
                                         cout << " ";
                                         if (rex.getHappines()<100)
                                         rex.setHappines(rex.getHappines()+1);
                                         else
                                         rex.speak();
                                         cout <<endl<<endl;
                                         break;
                                    case 3:
                                         system("cls");
                                         rex.sleep();
                                         cout << "Please wait 10 seconds until "<<name <<" gets full recover of energy";
                                         rex.setEnergy(100);
                                         Sleep (10000);
                                         cout<< "\nYYYAAWwwnnn...";
                                         rex.speak();
                                         cout<<endl<<endl;
                                         break;
                                    case 4:
                                         system("cls");
back4:                                   
                                         if (rex.getHappines() >100)
                                         rex.setHappines(100);
                                         cout <<rex.getHappines()<<"% happy"<<endl;
                                         cout << "Choose what food you want to give to " <<name<<endl;
                                         cout << "(0) back  (1) fetch - happines +15  (2) sit - happines +10 (3) stay -  happines +5:\n";
                                         int fun;
                                         cin>>fun;

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 switch(fun)
                                         {
                                          case 0:
                                               goto forward2;
                                          case 1:
                                               rex.fetch();
                                               rex.speak();
                                               cout << endl;
                                               break;
                                          case 2:
                                               rex.sit();
                                               rex.speak();
                                               cout << endl;
                                               break;
                                          case 3:
                                               rex.stay();
                                               rex.speak();
                                               cout << endl;    
                                               break;  
                                         }
                                         goto back4;
forward2:
                                         system("cls");
                                         break;
                                    case 5:
                                         cout << "Are you sure you want to quit this game?? All the data will be lost. y/n:";
                                         string yn;
                                         cin>> yn;
                                         if (yn == "y" )
                                         {
                                          goto back2;
                                          system("cls");
                                         }
                                         else 
                                         system("cls");
                                         break;
                                    }
                                   
                      }     
         cout << "Saving\n";
         int x = sec + clock();
         ofstream save ("animals.txt");
         save <<pet<<' '<< x<< ' ' << ' ' << rex.getHappines()<< ' ' << rex.getHunger()<< ' ' <<rex.getEnergy()<< ' ' <<name;
         Sleep (1200);
         cout << "Done. Thank you for playing. Please press enter to quit.";
         cin.ignore();
         cin.get(); 
         return 0;
}
//cat
        else if (pet == "cat")
   {
        double startTime = GetTickCount();
                while (fEnd)
                      {
                            double currentTime = GetTickCount() - startTime;

	                        	if( currentTime >= 15000) 
	                         	 {
	                          		muro.downEnergy();
                                    muro.downHappines();
                                    muro.downHunger();
		                                	//Reset the timer.
                            	    startTime = GetTickCount();
                                 }
                            muro.growUp();
                            if (sec >= death)
                            {
                                   cout << "I am really sorry but " << name << " died. It was too old. Would you like another pet?: y/n"; 
                                   string end;
                                   cin>> end;
                                    
                                    if (end == "y")
                                    {
                                            goto back1;
                                    }
                                    else
                                      {
                                      cout << "Thank you for playing. Please press enter to quit.";
                                      cin.ignore();
                                      cin.get();
                                      return 0;
                                      }       
                            }
                         cout<< name <<" is " <<muro.getAge()<<" years old and "<< muro.getHappines()<< "% happy and "<< muro.getHunger()<<"% full and has "<<muro.getEnergy()<<"% energy."<<endl ;
                         int choice;
                         cout <<"Choose number: \n(1) Feed it  (2) Pet it  (3) Let it sleep  (4) Play with it (5) Load or restart game (0) End ";
                         cin >> choice;
                             switch (choice)
                             {
                             case 0:
                                  char progres;
                                  cout << "Do you want to save the progres?? y/n\n";
                                  cin>> progres;
                                  if (progres == 'y')
                                  {
                                  fEnd = false;
                                  break;
                                  }
                                  else
                                  {
                                      cout << "Thank you for playing. Please press enter to quit.";
                                      cin.ignore();
                                      cin.get();
                                      return 0;
                                  }
                             case 1:
                                  system("cls");
back5:
                                         if (muro.getHunger()<=100)
                                         cout <<muro.getHunger()<<"% full"<<endl;
                                         if (muro.getHunger()>100)
                                         {
                                            muro.setHunger(100);
                                            cout <<muro.getHunger()<<"% full"<<endl;
                                         }
                                         cout << "Choose what food you want to give to " <<name<<endl;
                                         cout << "(0) back  (1) mouse - full +10  (2) water - full +5 (3) milk -  full +15 (4) cat food - full + 25:\n";
                                         int food;
                                         cin>>food;
                                         if (muro.getHunger()<100)
                                         {
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
109
110
111
112
113
114
115
116
117
118
119
switch (food)
                                                {
                                                case 0:
                                                       goto forward3;
                                                case 1:
                                                       muro.speak();
                                                       cout<<"\nahhhh\n";
                                                       muro.eat(10);
                                                       muro.speak();
                                                       cout << endl;
                                                       break;
                                                case 2:
                                                       muro.eat(5);
                                                       cout<<"ahhhh\n";
                                                       muro.speak();
                                                       cout << endl;
                                                       break;
                                                case 3: 
                                                       muro.speak();
                                                       cout<<"\nahhhh";
                                                       muro.eat(15);
                                                       cout << endl;
                                                       break;
                                                case 4:
                                                       muro.speak();
                                                       muro.eat(25);
                                                       cout<<"\nahhhh\n";
                                                       muro.speak();
                                                       cout << endl;
                                                       break;
                                                }
                                        goto back5;
                                        }
                                        else
                                        muro.speak();
                                        Sleep(1500);
forward3:
                                        system("cls");
                                        break;
                             case 2:
                                  system("cls");
                                  muro.speak();
                                  cout << " ";
                                  if (muro.getHappines()<100)
                                  muro.setHappines(muro.getHappines()+1);
                                  else
                                  muro.speak();
                                  cout <<endl<<endl;
                                  break;
                             case 3:
                                  system("cls");
                                  muro.sleep();
                                  cout << "Please wait 10 seconds until "<<name <<" gets full recover of energy";
                                  muro.setEnergy(100);
                                  Sleep (10000);
                                  cout<< "\nYYYAAWwwnnn...";
                                  muro.speak();
                                  cout<<endl<<endl;
                                  break;
                             case 4:system("cls");
back6:                                   
                                         if (muro.getHappines() >100)
                                         muro.setHappines(100);
                                         cout <<muro.getHappines()<<"% happy"<<endl;
                                         cout << "Choose what food you want to give to " <<name<<endl;
                                         cout << "(0) back  (1) tickle - happines +5  (2) give it skein - happines +30 (3) let it go out  -  happines +3:\n";
                                         int fun;
                                         cin>>fun;
                                         switch(fun)
                                         {
                                          case 0:
                                               goto forward4;
                                          case 1:
                                               muro.tickle();
                                               muro.speak();
                                               cout << endl;
                                               break;
                                          case 2:
                                               muro.skein();
                                               muro.speak();
                                               cout << endl;
                                               break;
                                          case 3:
                                               muro.out();
                                               muro.speak();
                                               cout << endl;    
                                               break;  
                                         }
                                         goto back6;
forward4:
                                         system("cls");
                                         break;
                             case 5:
                                  cout << "Are you sure you want to quit this game?? All the data will be lost. y/n:";
                                  string yn;
                                  cin>> yn;
                                  if (yn == "y" )
                                     {
                                      goto back2;
                                      system("cls");
                                     }
                                  else 
                                  system("cls");
                                  break;
                             }
                      }   
    //saving  
    cout << "Saving\n";
    int x = sec + clock();
    ofstream save ("animals.txt");
    save <<pet<< ' '<< x<< ' ' << ' ' << muro.getHappines()<< ' ' << muro.getHunger()<< ' ' <<muro.getEnergy()<< ' ' <<name;
    Sleep (1200);
    cout << "Done. Thank you for playing. Please press enter to quit.";
    cin.ignore();
    cin.get(); 
    return 0;
   }
  }
}

hi this is my project what i have done and not everythng but something what i learned in 2 months. All i want is that someone whould comment it or gove me some hints what i do wrong what i do ok what do you like about my work and so on.. what whould you do differently... it is long code for one post so if somebody feel like send me whole project changed this is my e-mail -> 1991kovac.miroslav@gmail.com.
thank you

it is just a game where you choose a pet you want to play with and then he has some needs which are going down in a time of 15 sec. it is also getting older and it will die. you may play with him feed him pet him or send him to sleep.

only thing what I didn t know how to do was to make go down his needs in every 15 seconds and that is where Lynx876 helped me. so thx Lynx876. I ll know how to make it next time.
Last edited on
pretty please anybody somebody
Have patience, it's only been a few hours.

That long list of if statements in your growUp function doesn't look too good, normally if you end up using more than 2-3 if statements in a row a loop could do it better (especially when they are all going up by the same amount). Something like this should work(it can be modified how you like).

1
2
3
4
5
6
7
for(int i = 1; i < i < 17; i++)
{
	if( sec + clock() > (i * 10000) && sec + clock() < (i * 100000) )
	{
		age = i; 
	}
}


I see quite a few goto statements in your code. These don't need to be used 99.99% of the time and just make it much harder to keep control of program flow and make it harder for people to read your code later on. There are usually better ways to accomplish things using while/do while loops. Try redoing some parts of code that rely on that.

You are using system calls quite often too, in my opinion system("cls") is one of the harder ones to avoid, but seeing as you are using windows.h anyway you might want to look up a better way to clear the screen (system calls aren't efficient and aren't liked by virus scanners).

There are some minor things I see like happines = happines - 35; is usually written as happines -= 35; just because it looks cleaner, but that's all personal preference.

Keep an eye on your indenting (which looks pretty good for the most part), it can make a big difference when people are reading through your code. Stuff like this doesn't look good.
1
2
3
4
5
if (progres == 'y')
{
fEnd = false;
break;
}


Definitely a good start just some cleaning up needed in certain places, keep working at it!
Last edited on
thx James2250 for your opinions i ll do better next time. it is a big help for me if someone would tell me what I did wrong. I ll do better next time.
hey James

I tryed your for loop but it didn t exactly worked because the first i is only 10 000 so it s 10 seconds till it grow a one year and it should be 100 sec so I tryed something totaly different and here it is:

1
2
3
4
 for (int h = 0 ;h<clock();h+=2000)
     {
          age++;
     }


but it didn t work either because it was giving me bad numbers. first 2 seconds it was ok but then it was like he put those clock() more times so age went up rapiddly to big numbers and not as I expected every two seconds and I don t know why.
and than I tryed your version like this

1
2
3
4
5
6
7
8
9
void animal::growUp()
{
      for (int i = 1 ;i<17;i++)
     {
          if (sec + clock()>i*100000)
          age = i;
     }
     
}

and it works.. so does it look better than 17 times if statements??? (: thanks a lot.

if you can then tell me why first for loop doesn t work.. thanks.
Topic archived. No new replies allowed.