Guess the number game

Hey, i need help with my homework. The assignment is to create a game which generates a number between 1-100 and then you have to guess it, after each guess the computer tells you if the guess is too high or too low. Also you only have 7 guesses, if you fail or succeed in guessing the number the computer must ask you if you want to play again.

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
 #include<iostream>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <clocale>

using namespace std;

int main()  {
    
    	setlocale(LC_CTYPE, "");
    	
srand(time(0));
int x;  // guessed number
int y; //  guesses
string vastus; 




{
int number=rand()%100+1;;
cout<<"Guess: ";
cin>>x;
y=1;

	while(x!=number)
	{
		if(x>number)
		{
			cout<<"Too high, guess again: ";
			cin>>x;	
                        y++;	
		}
		else if(x<number)
		{
			cout<<"Too low, guess again: ";
			cin>>x;	
                        y++;
		}
		
    }
	   
	   
	if(x=number)
	{
	  cout<<"gratz, do you want to play again? [y/n]";
	}



}
return 0;
}



I managed to get the basic to work but i need help with ending the task if you have already guessed 7 times and how to ask if you want to play again.
closed account (2UD8vCM9)
Two things
First off
Don't make code using variables whose names are completely irrelevant to their purpose (i.e. int x, int y) when making code, it will just make big projects impossible to understand.

Second off

I tried to put in necessary comments and I changed your variable names, check out if this makes sense or not
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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <clocale>

using namespace std;

int main()  {
    
setlocale(LC_CTYPE, "");
    	
srand(time(0));
int GuessedNumber;  // guessed number
int TotalGuesses=0; //  guesses
bool StillPlaying=true; //true if user is still playing, false if user isnt playing



	while (StillPlaying==true) //By putting the game code in the loop it will restart if they choose to play again
	{
		int number=rand()%100+1;;
		cout<<"Guess: ";
		cin>>GuessedNumber;
		TotalGuesses=1;

		while(GuessedNumber!=number && TotalGuesses<7) //By adding checking for TotalGuesses in the loop we know that after 7 guesses they cant guess anymore
		{
			if(GuessedNumber>number)
			{
				cout<<"Too high, guess again: ";
				cin>>GuessedNumber;	
							TotalGuesses++;	
			}
			else if(GuessedNumber<number)
			{
				cout<<"Too low, guess again: ";
				cin>>GuessedNumber;	
							TotalGuesses++;
			}
		}
	   
		if(GuessedNumber==number) //== to check if they are the same
		{
		  cout<<"gratz, do you want to play again? [y/n]" << endl;
		} 
		else
		{
			cout <<"you lost :( play again? [y/n]" << endl;
		}
		//Now we've displayed whether they've lost or won. We need to check what they enter
		char UserInput;
		cin >> UserInput;
		while (UserInput!='y' && UserInput!='n' && UserInput!='Y' && UserInput!= 'N') //While user doesnt enter a y or a n
		{
			cout << "Invalid response please enter 'y' or 'n'" << endl; 
			cin >> UserInput;
		}
		//Now we've made sure they've entered a y or a n, so we can just check if it was 'n'because we know if they chose n
		//then we can stop the current loop we're in which can be stopped by setting StillPlaying to false
		if (UserInput == 'n' || UserInput == 'N') //If they entered n or N
		{
			StillPlaying=false;
		}
	}

return 0;
}
closed account (jvqpDjzh)
Something like this should work (make me know if there are bugs ;) , I have written it on the volley)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    int num = 0,
        random_num = 0;//number the user has to guess
    char answer = 'y';
    int c = 0;//counter variable

    do
    {
        random_num = rand() % 100 + 1;
        do
        {
            std::cout << "Guess a number: ";
            std::cin >> num;

            if(num == random_num) std::cout << "You guessed it!\n";
            else if(num > random_num) std::cout << "Too high!\n";
            else std::cout << "Too low!\n";

        } while(random_num != num && ++c < 7);
        std::cout <<"Do you want to play again? (y/n) ";
        std::cin >> answer;
        c = 0;
        std::cout <<"\n\n";
    }while(answer != 'n');

Last edited on
Two things
First off
Don't make code using variables whose names are completely irrelevant to their purpose (i.e. int x, int y) when making code, it will just make big projects impossible to understand.

Second off

I tried to put in necessary comments and I changed your variable names, check out if this makes sense or not
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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <clocale>

using namespace std;

int main()  {
    
setlocale(LC_CTYPE, "");
    	
srand(time(0));
int GuessedNumber;  // guessed number
int TotalGuesses=0; //  guesses
bool StillPlaying=true; //true if user is still playing, false if user isnt playing



	while (StillPlaying==true) //By putting the game code in the loop it will restart if they choose to play again
	{
		int number=rand()%100+1;;
		cout<<"Guess: ";
		cin>>GuessedNumber;
		TotalGuesses=1;

		while(GuessedNumber!=number && TotalGuesses<7) //By adding checking for TotalGuesses in the loop we know that after 7 guesses they cant guess anymore
		{
			if(GuessedNumber>number)
			{
				cout<<"Too high, guess again: ";
				cin>>GuessedNumber;	
							TotalGuesses++;	
			}
			else if(GuessedNumber<number)
			{
				cout<<"Too low, guess again: ";
				cin>>GuessedNumber;	
							TotalGuesses++;
			}
		}
	   
		if(GuessedNumber==number) //== to check if they are the same
		{
		  cout<<"gratz, do you want to play again? [y/n]" << endl;
		} 
		else
		{
			cout <<"you lost :( play again? [y/n]" << endl;
		}
		//Now we've displayed whether they've lost or won. We need to check what they enter
		char UserInput;
		cin >> UserInput;
		while (UserInput!='y' && UserInput!='n' && UserInput!='Y' && UserInput!= 'N') //While user doesnt enter a y or a n
		{
			cout << "Invalid response please enter 'y' or 'n'" << endl; 
			cin >> UserInput;
		}
		//Now we've made sure they've entered a y or a n, so we can just check if it was 'n'because we know if they chose n
		//then we can stop the current loop we're in which can be stopped by setting StillPlaying to false
		if (UserInput == 'n' || UserInput == 'N') //If they entered n or N
		{
			StillPlaying=false;
		}
	}

return 0;
}


Yes, thank you very much for your help, it makes a lot of sense to a beginner like me. I guess i was stuck because we haven't actually studied or even looked at the bool command.
Also thanks for the tip!
Topic archived. No new replies allowed.