Guess the Number

I looked at a few other guess the number topics on this forum but I couldn't find one that addressed my issue. So sorry for making another topic.

This is a standard quess the number game using C++. The problem I am having is the beginning. I start the game asking, "Do you want to play a game. (Y/N)", prompting the user to type in an answer. The problem is that whatever i type in for the question, it proceeds to the next part. I want it to stop if i press N. Heres my code.

char input[100];
char response[100];
int target;
int guess;
int tries;


srand(time(0));


do
{
std:: cout<<"Do you want to play a guessing game? (Y/N)\n";
tries = 0;
if(scanf("%s",input)==0)
break;
else if (strcmp(response,"Y")==0)
break;
else target = rand() % 50 +1;
std::cout<< "Enter a Number between 0 and 50. Hit q to quit\n";
for(;;)
{

tries++;
if(scanf("%s", input)==0)
return(0);
else if (input[0]=='q')
break;
else if (sscanf(input, "%d", & guess) == 0)
printf("Enter an Integer! \n");
else if (guess > target)
{
std::cout<< "Try a lower number. You guessed " <<tries<< " times \n";

}
else if (guess < target)
{
std::cout<< "Try a higher number. You guessed " <<tries<< " times\n";

}
else
{
std:: cout<< "Congratulations! You found it! \n";
std::cout<< "You guessed the number in " << tries<< " tries! \n";
break;
}
}
}while(strcmp(response,"Y") != '0');
}


The actual guessing part of the game works fine. It's just the initial question doesn't work. Any ideas on what I'm doing wrong?

Thanks
Hi
This is a standard quess the number game using C++.

Then you'd be better off using std::cin for getting user input from the console. That's the C++ way :)
For example:
1
2
3
4
5
6
7
8
9
10
11
std::cout << "Play a game? (y/n) ";
char answer;
std::cin >> answer;
if ( 'y' == answer )
{
    // play game
}
else
{
    // don't bother
} // if ... else 
Thank You!! That fixed it.

Thank you very very much!
Topic archived. No new replies allowed.