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;
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;
}
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
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;
}
}
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
}
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.
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string> //added strings
usingnamespace 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;
}
elseif(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;
}
:)
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).