Guessing game - code the player

So, i just started coding, and i got this assignment where i need to make a code where my program has 4 tries to guess a number from 1 to 10. My program has 4 tries, and there are 3 possible inputs:
1 if it's bigger
0 if it's smaller
2 if it's equal

I tried to submit the code but it exceeded the time limit(2000ms)
As i mentioned, i am very new to coding. I wrote this code but i'm not sure if it is even on the right trail at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main(void) {
    int var1,var2;
    var1 = 5;
    cout << var1 << endl;
    cin >> var2 >> endl;
    if (var2 == 2){
        cout << "Congrats!" << endl;
    } else {
        while(var2 == 1); {
            var1++;
            cout << var1 <<endl;
        }
        while(var2 == 0);{
            var1--;
            cout << var1 <<endl;
        }
    }
    
    return 0;
}.
Last edited on
The easiest way to do what your program asks is to create a FOR LOOP that stops when the variable within equals 4, or rather, 5, but won't execute once it hits 5. Happy coding :)
Last edited on
You have extraneous semi-colons on lines 13 and 17 which makes your loops infinite. Get rid of those empty statements.

Note that when you do remove those empty statements, since var2 cannot change within the body of the compound statements governed by the loop conditions, the loops will either be (still) infinite or never run. You should look into that.
Last edited on
Topic archived. No new replies allowed.