Guess the Number Game

Hello, I am a beginner in C++ and really need help finishing my code. My program asks the user to guess a number from 1 to 1000 and when the user guesses the number correctly, the user is asked if they want to play again, if so, then the program should ask the user to guess the number and so forth, but I have trouble keeping the program running if the user enters y(yes). I do not know what to do I have tried my best and would appreciate help. Also, the header file has all the include statements I need. Also, all my indentations are correct and I don't know why it does not show that when I submit my topic/question.

[#include "cisp400asgn4.h" //Includes what's in the header file

int main()
{

int num;

int guesses;

int tries = 0;

char y;

char n;

srand(time(0)); // Random number generator.

num = rand() % 1000 + 1; // Random number between 1 and 1000.

cout << "Guess my number (1 - 1000) " << endl;

do
{

cout << "Please input your guess ";

cin >> guesses; // Scanning guesses variable.

tries++;
if (guesses > num)
{
cout << "Too high. Try again." << endl;
}

else if (guesses < num)
{
cout << "Too low. Try again." << endl;
}

else if (guesses == num)
{
cout << "Nice! Do you want to play again (y or n)?

cin >> y >> n; // Scanning
}

if (y == 'y')

{
continue;
}

else if (n == 'n')
{
break;
}

} while (guesses != num);

cin.get(); // Gets Value!

return 0; // Returns an integer.
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
char response = 'y';

do
{
	// generate number
	//
	// while( guess != number ) {
	//    get user's guess
	// }

	cout << "Play again? ";
	cin >> response;
} while( response != 'n' );


Currently you're reading two char variables (y and n) which is not useful. what is currently happening is that the program will only exit if the 2nd char entered is a 'n'.
Last edited on
Topic archived. No new replies allowed.