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.
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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <clocale>
usingnamespace 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++;
}
elseif(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;
}
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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <clocale>
usingnamespace 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++;
}
elseif(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!