A simple code error I can't seem to find...

I am new. Very new. I made this in a couple minutes and I was wondering whats wrong? When you type in a number it should either say correct or incorrect. But it says nothing and lets you keep on putting in numbers. Whats wrong? I think it has something to do with my loop.

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
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
   int number;//Stores the random number
   int guess;//stores the guesses
   int tries;//Stores the amount of tries.

   srand(time(NULL));
   number = (rand() % 10) + 1;//Generates Random Number between 1 and 5.

   cout << "A Random number between 1 and 10 has been generated. Try and guess it in 3 tries" << endl; //Begins the game
   bool done = false;//Bool to declare the games end.

   while (done == false)//Will turn true when game ends
   cin >> guess;//Allows the user to input a guess
   if (guess == number) //Checks if the guess equals the number generated
    {
              cout << "Good job! You correctly guessed the Number!" << endl; // If thy matched is tells you you got it right
              done = true; // Ends the game since its correctly guessed.
    }
    else if (guess != number)
    {
              cout << "That number is incorrect!" << endl;// This gets diplayed if the numbers don't match
              tries + 1;
    }
    if (guess > 10)
    {
              cout << "That number is greater than 10! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number lower then 10.
    }
    if (guess == 0)
    {
              cout << "The number you guessed is 0! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number can't be 0.

    }
    if (tries == 3)
    {
              cout << "Your 3 tries are up! You lost!" << endl;// Tells the player that there 3 tries are up!

    }
    return (0);
    }
while loops, like if statements, only go until the first semicolon -- or for the next {code block}

You don't have braces around your while loop body, so it's only looping the cin line:

1
2
3
4
while (done == false)
    cin >> guess;  // this is part of the loop

if (guess == number) // this is NOT part of th eloop 


To solve, put the loop body in braces:

1
2
3
4
5
6
7
8
while(done == false)
{
    cin >> guess;

    if(guess == number)
       ...

}

Ok.. So this? I tried this and I did not work. I typed in a number. It said incorrect (because it was wrong) and then finished. I wanted it to give you 2 more chances.

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
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
   int number;//Stores the random number
   int guess;//stores the guesses
   int tries;//Stores the amount of tries.

   srand(time(NULL));
   number = (rand() % 10) + 1;//Generates Random Number between 1 and 5.

   cout << "A Random number between 1 and 10 has been generated. Try and guess it in 3 tries" << endl; //Begins the game
   bool done = false;//Bool to declare the games end.

   while (done == false)//Will turn true when game end
   {
   cin >> guess;//Allows the user to input a guess
   if (guess == number) //Checks if the guess equals the number generated

              cout << "Good job! You correctly guessed the Number!" << endl; // If thy matched is tells you you got it right
              done = true; // Ends the game since its correctly guessed.

    if (guess != number)

              cout << "That number is incorrect!" << endl;// This gets diplayed if the numbers don't match
              tries + 1;

    if (guess > 10)

              cout << "That number is greater than 10! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number lower then 10.

    if (guess == 0)

              cout << "The number you guessed is 0! Please guess a number between 1 and 10." << endl;// Reminds the player to have a number can't be 0.


    if (tries == 3)

              cout << "Your 3 tries are up! You lost!" << endl;// Tells the player that there 3 tries are up!

    return (0);
    }}
Last edited on
Topic archived. No new replies allowed.