Guess my number

My guess my number game doesn't allow you to guess multiple times. Please help!

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


using namespace std;

int main()
{
    int num, t, guesses;
    srand((unsigned)time(0));
    int random_integer = rand()%100;

    t = rand();
    cout << "Guess your first number\n";
    cin >> num;
    do
    {
        if (num == t)
        {
            cout << endl;
            system ("Pause");
            break;
        }
        else if (num > t)
        {
            cout << "Lower!\n";
            system ("Pause");
            break;
        }
        else if (num < t)
        {
            cout << "Higher!\n";
            system ("Pause");
            break;
        }
        else
        {
            cout << "Invalid input\n";
            system ("Pause");
            break;
        }
    }
    while (num != t);
    {
        cout << endl;
    }

    return 
}
closed account (17pz3TCk)
I think your main problem is that you are asking the user for input outside of the loop, therefore the user will only get asked once.

Aside from that, you do not need break in if/else/else if blocks.

Finally, your return at the end of main should say "return 0" and not just "return."

Hope that helps =)

Edit: I don't know how I missed it but you should take out system ("Pause") from the code too. Its bad practice and make the user have to press too many keys to get through the program
Last edited on
My 1 cent worth:

If you are going to use a do loop, put the while part on the same line as the closing brace - so it doesn't look like a while loop with a null statement.

I prefer not to use do loops unless i have to. They can very often be coded as a while or for loop.

Edit: with my first statement about do loops, it looks like that exact thing has happened to you already:

1
2
3
4
5
6
7
do {
//do loop code here
}
while (num != t);
    {
        cout << endl;
    }


The while is part of the do loop!! But you have put a compound statement after it ( albeit with 1 statement)

You can see how this can cause huge confusion.
Last edited on
You have to remove break statement form all if condition.
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
int main()
{
    int num, t, guesses;
    srand((unsigned)time(0));
    int random_integer = rand()%100;

    t = rand();
    do
    {   
        cin.clear();
        cout << "Guess your first number\n";
        cin >> num;
        if (num == t)
        {
            cout << endl;
        }
        else if (num > t)
        {
            cout << "Lower!\n";
        }
        else if (num < t)
        {
            cout << "Higher!\n";
        }
        else
        {
            cout << "Invalid input\n";
        }
    }while (num != t);

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