guessing game loop help

closed account (93v5LyTq)
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.


#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <random>

using namespace std;

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 (!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;
}
Last edited on
Basically you need two loops. The outer loop handles the play again part and the inner loop the guessing part.

1
2
3
4
5
6
while(play_again)
{
   while(!guessed)
   {
   }
}


Why do you deceive the user? You say the number is between 1 and 100 but your rand creates one between 1 and 3.
closed account (93v5LyTq)
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;
}
}

return 0;
}

but i am going to try your suggestion!
This line needs to be
num = rand() % 100 + 1
As what you have only generates numbers between 1 and 3.
closed account (93v5LyTq)
#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 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
You need to generate the number inside the inner loop.
playAgain = false; You can't assign a bool to a char.
closed account (93v5LyTq)
#include <iostream>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include <string>
#include <random>

using namespace std;

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')
{
playAgain = false;

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
You were quite close. I cleaned up your code a bit.
Have a look if you understand the logic.
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
#include <iostream>
#include <ctime>
#include <string>

using namespace 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;
  }
}
closed account (93v5LyTq)
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?
closed account (93v5LyTq)
#include <iostream>
#include <ctime>
#include <string>

using namespace 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, 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
Last edited on
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.

Please enclose your examples in code tags.

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
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main(void ) {
  int num;
  char retry;
  string player;
  
  cout << "\n\tWelcome to guessing game. Please enter your name? ";
  cin >> player;
  cout << "\n\tHi " << player << ". Guess my number between 1 and 100";
  srand (time (0));
  
  do {
    num = rand () % 100 + 1;
    cout << "\n\n\t\t";
    
    do {
      int guess;
      
      cout << "\t-> ";
      cin >> guess;
      
      if (guess == num)
        { cout << "\n\t\tYeah, you got it\n\n"; break; }
        
      if (guess < num)
        cout << "\t\tHigher";
      else
        cout << "\t\tLower";
      
    } while (1);
    
  cout << "\n\tPlay again [Y/N] ";
  cin >> retry;  
  } while ((retry & 0x5f) == 'Y');
  
return 0;
} 
Topic archived. No new replies allowed.