Forgive me if this has been asked and answered. I have searched the forums and could not find an answer.
I am a newbie to programming and have been going through the tutorials and am having a issue with my play again loop.
Here is my code:
Code:
#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;
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;
}
cout<<"would you like to play again? yes/no" <<endl;
cin.getline (playAgain, 4);
}
while (strcmp (playAgain, "yes") == 0);
// cin.getline (playAgain, 4);
cout<<"Thanks for playing ";
return 0;
}
else {
cout<< "OK see you next time" ;
}
return 0;
}
The program compiles and runs -- the first time. When it gets to the hilighted part of the code it asks
"would you like to play again? yes/no
Thanks for playing"
It never does go back to the game part of the code. As you can see, I have tried putting cin.getline(playAgain) both above and below the while statement and get the same result.
I know it is something easy but I can not figure out what I am doing wrong.
Thanks in advance for any insight you might be able to provide