random number guessing game

I need help with an assignment for a class.Im supposed to write a program where the program picks a number between 1 and 1000 and you have to guess the number.After you guess the number you have the option of if you want to play again or not meaning its supposed to display "Would you like to play again? enter y/n"

this is what i have:
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand(time(0));
int number=rand()%1000+1;;
int guess;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess;

while(guess!=number)
{
if(guess>number)
{
cout<<"Too high,Guess again: ";
cin>>guess;
}
if(guess<number)
{
cout<<"Too low,Guess again: ";
cin>>guess;
}
}
if(guess=number)
{
cout<<"Congrats!! You got it.";
}

return 0;
}

this doesnt work right and i have no idea how to do the play again feature.Anybody who knows how to do this,Please help me..........
The above code works fine, only problem is that you dont pause the program at the end. You could use system("pause"), wich isnt a very clean solution. I recommend you to read the discussion "console closing down" at the top of this forum.

About the 'play again feature': create a bool variable called goOn or someting like that, and place the whole code in a while or do loop with as condition that variable. Give the user inside the loop the possebility to chance that value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//...
int main()
{
//....
bool goOn=true;
string answer;

while (goOn)
{
//....
cout<<"Would you like to go on? [yes/no]\n";
cin>>answer;
if (!(answer=="yes"))
goOn=false;
}
return 0;
}


could you define system pause and how it affects this program?

and for the bool vairable,you would put the code for the game inside the bool while loop?
Also, if(guess = number) is not the correct syntax, it is if(guess == number).
i suggest making a do-while lop instead,

just a suggestion, but to make it interesting put a limit to number of guess maybe 7.
ok,i got the game to work.But when i try to add a play again feature using a bool variable i get these errors.
error C2446: '!=' : no conversion from 'const char *' to 'int'
There is no context in which this conversion is possible
error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]

all for this line: if(answer!="y")

this is what i got in new code:
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand(time(0));
int number=rand()%1000+1;;
int guess;
bool goOn=true;
char answer;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess;

while(goOn)
{
while(guess!=number)
{
if(guess>number)
{
cout<<"Too high,Guess again: ";
cin>>guess;
}
if(guess<number)
{
cout<<"Too low,Guess again: ";
cin>>guess;
}
}
if(guess==number)
{
cout<<"Congrats!! You got it.";
}
cout<<"would you like to play again? Enter y or n";
cin>>answer;
if(answer!="y")
{
goOn=false;
cout<<"thanks for playing!";
}
}
return 0;
}

if anybody has any ideas,i would love to hear them
Answer is of type char, while "..." is of considered as a string. You should use this:if(answer!='y'), since '...' is the declaration for char

And you should put the declartion of variables etc. in the beginning of main also in the loop to make it work.
Last edited on
game works,still having trouble restarting the game when person hits y

my current code:
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

srand(time(0));
int number=rand()%1000+1;;
int guess;
bool goOn=true;
char answer;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess;

while(goOn)
{
while(guess!=number)
{
if(guess>number)
{
cout<<"Too high,Guess again: ";
cin>>guess;
}
if(guess<number)
{
cout<<"Too low,Guess again: ";
cin>>guess;
}
}
if(guess==number)
{
cout<<"Congrats!! You got it.";
}
cout<<"would you like to play again? Enter y or n: ";
cin>>answer;
if(answer!='y')
{
goOn=false;
cout<<"thanks for playing!"<<endl;
}
}

return 0;
}
Use the #format when uploading code next time.

You dont clear the variables. This is how it should be:
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
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

bool goOn=true;
while(goOn)
{
srand(time(0));
int number=rand()%1000+1;;
int guess;

char answer;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess; 


while(guess!=number)
{
if(guess>number)
{
cout<<"Too high,Guess again: ";
cin>>guess;
} 
if(guess<number)
{
cout<<"Too low,Guess again: ";
cin>>guess;
}
}
if(guess==number)
{ 
cout<<"Congrats!! You got it.";
}
cout<<"would you like to play again? Enter y or n: ";
cin>>answer;
if(answer!='y')
{
goOn=false;
cout<<"thanks for playing!"<<endl;
} 
}

return 0;
}
scorpio,you rule.works perfectly.
glad i could help you :)

I'm new to c++ but find that i learn better by fixing problems, usually ones i've made.
anway, i was playing around and got it working using this.
Scipio's is probably better though, use his.



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
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string> //added strings

using namespace std;

int main()
{
srand(time(0));
int guess;
char playAgain='y'; //new variable

while (playAgain !='n') 
/*added a while loop so that the whole program 
will loop while the new variable isn't 'n'*/
{
int number=rand()%1000+1;;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess;

	while(guess!=number)
	{
		if(guess>number)
		{
			cout<<"Too high,Guess again: ";
			cin>>guess;
		}
		else if(guess<number)
		{
			cout<<"Too low,Guess again: ";
			cin>>guess;
		}
	}
		if(guess=number)
		{
			cout<<"Congrats!! You got it.";
		}

//do while loop so that the play again question loops for right answer
	do { 
		cout << "Would you like to play again? y/n ";
		cin >> playAgain;
		} while (playAgain !='y' && playAgain !='n');
	
}
return 0;
}
Last edited on
:)
I agree its better to loop until you get a right answer.

One note: you dont pick a new random number when the player wants to play again. Cut/paste line 12 to the beginning to the beginning of the while loop (i think its better to use a do-while loop here to, aldo it doesnt make much difference).
However, you may want to rename your variable since continue is a reserved keyword.
Topic archived. No new replies allowed.