i have been struggling with this project i have that is due later, we are supposed to create a guessing game where we prompt the user for their name, prompt them to enter a number from 1-100 and basically create a loop. the number must be random each time generated by the computer, if they get it right i congratulate them and ask if they want to try again. my loop does not work when the user input 'y' to play again. please help!! please notify me if anything needs to be fixed as well, i am still a beginner; i have been all over this forum and everything i have tried has failed so i thought maybe i should ask and someone will answer.
thank you for helping me! i changed it to three just to test it out myself in a smaller quantity since it is randomly generated by the computer, i think i may have solved it? my only issue now is that the numbers in the loop are the same, they do not randomize every time the user chooses to play again
i changed it to this, which may be wrong, but it got the loop working when the input 'y'
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <random>
#include "ProgrammingProject2.h"
using namespace std;
int main()
{
int num;
int guess;
string firstName;
bool isGuessed;
char again = 'y';
srand(time(0));
num = rand() % 100 + 1;
isGuessed = false;
cout << "Welcome to the Guessing Game. What is your name?" << endl;
cin >> firstName;
cout << "Hi " << firstName << endl;
while (again == 'y' || again == 'Y')
{
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;
cin >> guess;
cout << endl;
if (guess < num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess > num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> again;
if (again == 'n')
{
again = false;
cout << "Thank you for playing!" << endl;
}
}
int main()
{
int num;
int guess;
string firstName;
bool isGuessed;
char playAgain = 'y';
srand(time(0));
num = rand() % 100 + 1;
isGuessed = false;
cout << "Welcome to the Guessing Game. What is your name?" << endl;
cin >> firstName;
cout << "Hi " << firstName << endl;
while (playAgain == 'y' || playAgain == 'Y')
{
while (!isGuessed)
{
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;
cin >> guess;
cout << endl;
if (guess < num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess > num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
if (playAgain == 'n')
{
playAgain = false;
cout << "Thank you for playing!" << endl;
}
}
return 0;
}
}
i changed it to this but i am still having the same issues
my main issues are:
when the user guesses correctly and wants to play again, it shuts down
when they do play again, the number will be the same as before instead of a new randomized number
while (!isGuessed)
{
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;
cin >> guess;
cout << endl;
if (guess < num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess > num)
{
cout << "I'm sorry, that is incorrect." << endl;
}
if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
}
cout << "Thank you for playing!" << endl;
}
return 0;
}
i feel as though the more i try, the more i doubt myself and cause more problems.
do i have too many #includes?
should i get rid of bool all together?
im in way over my head here
#include <iostream>
#include <ctime>
#include <string>
usingnamespace std;
int main()
{
int num, guess;
string firstName;
bool isGuessed = false;
char playAgain = 'y';
srand(time(0));
cout << "Welcome to the Guessing Game. What is your name?" << endl;
cin >> firstName;
cout << "Hi " << firstName << endl;
while (playAgain == 'y' || playAgain == 'Y')
{
while (!isGuessed)
{
num = rand() % 100 + 1;
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;
cin >> guess;
cout << endl;
if (guess < num)
{
cout << "I'm sorry, number is too small." << endl;
}
if (guess > num)
{
cout << "I'm sorry, number is too big." << endl;
}
if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
if (playAgain == 'y' || playAgain == 'Y')
isGuessed = false;
else
playAgain = 'n';
}
cout << "Thank you for playing!" << endl;
}
}
thank you sooo much that helped a ton!!!! it helped with the randomization of numbers but when the user enters 'n' it simply goes right back to "I am thinking of a number..", any quick fixes for that? should i also forget the return code at the end?
int main()
{
int num, guess;
string firstName;
bool isGuessed = false;
char playAgain = 'y';
srand(time(0));
cout << "Welcome to the Guessing Game. What is your name?" << endl;
cin >> firstName;
cout << "Hi " << firstName << endl;
while (playAgain== 'y' || playAgain == 'Y')
{
while (!isGuessed)
{
num = rand() % 100 + 1;
cout << "I am thinking of a number between 1 and 100,"
<< " can you guess what it is?" << endl;
cin >> guess;
cout << endl;
if (guess < num)
{
cout << "I'm sorry, that guess is too small." << endl;
}
if (guess > num)
{
cout << "I'm sorry, that is too big." << endl;
}
if (guess == num)
{
cout << "That is correct!"
<< " Congratulations on winning! Thank you for playing my game." << endl;
isGuessed = true;
}
cout << "Would you like to play again? (y/n)" << endl;
cin >> playAgain;
if (playAgain == 'y' || playAgain == 'Y')
{
isGuessed = false;
}
else
{
playAgain = 'n';
cout << "Thank you for playing!" << endl;
return 0;
}
}
return 0;
}
}
this is the solution i came up with but im pretty sure i should not have 2 return statements? anyone know if this is ok or if there is another way to get the same result? without them both the loop would not end and 'n' or 'y' would not work
Most often an example is the best way of explaining something most clearly and I hope you have the common sense not to hand this example in, as your instructor will know immediately it's not your work. However, you can take the flow and easily modify your application to suit.