Play Again not working

I am a total newbie and am self teaching myself c++. My appoligies if this has been asked and answered. I did not find anything in the forums
I have a program that lets the user guess a random number
here is the code:
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
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <ctime>
#include <conio.h>

using namespace std;

int main()
{
char name[15], answer[4], playAgain[4];
srand(time(0));
int number=rand()%100;
int guess=-1;
int trycount=0;



 cout<<"please enter your first name: ";
 cin.getline(name, 15);
 cout<<"hello " << name <<"\n";
 cout<<"did you have a good day? yes/no " ;
 cin.getline ( answer, 4 );

  if
   ( strcmp ( answer, "yes" ) == 0)
   cout<< "I am so glad you had a good day" <<endl;
  else
   cout<< "I am sorry you had a bad day" <<endl;

 cout<<"Would you like to play a game? yes/no ";
 cin.getline (answer, 4 );

  if (strcmp (answer, "yes") ==0)
  {
     cout<< "Great, this game is find the number " <<endl;
     cout<< "The number will be between 0 and 100" <<endl;
     cout<< "You have 8 tries " <<endl;
      do
      {
       while(guess!=number && trycount<8)
       {

         cout<<"Please enter a guess: ";
         cin>>guess;
         cin.ignore();

         if(guess<number) cout<<"Too low"<<endl;
         if(guess>number) cout<<"Too high"<<endl;

         trycount++;
       }
      if(guess==number)
       cout<<"You guessed the number" <<endl;
      else {
            cout<<"Sorry, the number was: "<<number <<endl;
           }
         //This begins the section I am having trouble with 
         cout<<"would you like to play again? yes/no ";
         cin.getline (playAgain, 4);

       }
      while (strcmp (playAgain, "yes") == 0);
         //cin.getline (playAgain, 4);
         //This ends the section I am having trouble with
     cout<<"Thanks for playing ";

     return 0;
   }
   else {
         cout<< "OK see you next time" ;
        }



return 0;
}


Program compiles and runs -- the first time. The problem is when ig gets to the
Would you like to play again? if user says yes it only loops to the guess = number sectionlike this:

You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no yes
You guessed the number
would you like to play again? yes/no no
Thanks for playing

I am not sure what I am doing wrong to not have it go to the do part of my do/while statement. Any help and explanation would be appreciated.

On a side note -- someone told me its better to use strcmpi() instead of strcmp(). Can you tell me the difference and why one is better than the other?
Thanks in advance
Taking a quick look I think it's because your variables are not being reset after you've played the game.


I also think strcmpi() is a C function, it ignores case sensitivity. It is not part of the ANSI standard.
Last edited on
thanks lordmat
That makes sense .
The program is still looking at trycount and number as having been where they were before I start the game again. I knew it was something easy
Sometimes you just need another set of eyes to help, have fun programming!
Topic archived. No new replies allowed.