A loop error... Anyone able to solve this?

I think there is a problem with my while loop in this code. But I can't find it, can you?
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);
    }}

hello ThePixel,

now you should tell how appears, but well.

You didn't initialize tries, so it will not function like you expect.
I'm completely new to this language, but why is the "return (0);" placed where it is? Should it not be like :::

}
return (0);
}

Because then, would it not terminate the program every time the user entered a number?

I only started yesterday, forgive me if I'm wrong.
coder777, I did this to initialize it.

int tries = 0;

It still does not work right.

Zero, I don't think it matters. I know for sure your way is right. The way mine is. Its just a typo that well... Worked out.
Last edited on
Line 29 doesn't do what you expect it to:

tries + 1;

When tries == 0, that's the same as saying

0 + 1;

You probably meant to say:

tries += 1;

or the simpler

++tries;
Tried it. Nothing. Every time I run the program it picks a number. lets you input your guess. Then it says incorrect or correct. Then closes. I want it to be like. Incorrect. Then lets you guess 2 more times. Then if you still don't get it. Closes.
Have you fixed the problem pointed out by zero0110111?
Yes.
Line 26:

1
2
3
4
if (guess != number) {
    cout << "That number is incorrect!" << endl;// This gets diplayed if the numbers don't match
    ++tries;
}

You were missing the curly braces I added.
That did not change anything. Here is the program now.

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

using namespace std;

int main()
{
   int number;//Stores the random number
   int guess;//stores the guesses
   int tries = 0;//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;
}

    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);
}
The if statement on line 21 needs curly braces too. Line 24 is being executed without condition, thus closing the loop every time. Remember to always use curly braces to delimit a statement block if you have more than one statement to be executed on condition.
Thanks that fixed my problem.

I have another question... How can I make it like... when tries = 3 then end the program?
Last edited on
hi
as zero0110111 pointed return 0;
should be at the end of your program cause it terminates it .
could you please say what error you get so that
My first question is solved. I now have another one.

How can I make it like... when tries = 3 then end the program?
1
2
3
4
5
if (tries == 3)
{
 cout << "stuff" << endl;
 return 0;
}
Thank you.
Topic archived. No new replies allowed.