guessing game

I dont get whats wrong. Here are the errors.

C:\Users\\Desktop\New folder\guessing game\main.cpp||In function 'int main()':|
C:\Users\\Desktop\New folder\guessing game\main.cpp|24|error: expected '(' before 'else'|
C:\Users\\Desktop\New folder\guessing game\main.cpp|27|error: 'else' without a previous 'if'|
||=== Build finished: 2 errors, 0 warnings ===|

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

using namespace std;

int main()
{
    int tries = 0;
    int answer;
    bool gameover = false;
    int guess;

    srand(time(0));
    answer = rand() %101;
    do{

    cout << "Im thinking of a number from 1 to 100. Guess what it is: ";
    cin >> guess;

    if(guess > answer){
        cout << "That guess is too high. Guess again!" << endl;
        tries++;
    }if else(guess > answer){
        cout << "That guess is too low. Guess again!" << endl;
        tries++
    }else{
        tries++;
        gameover = true;
    }
    }while (!gameover);

    cout << "Good job!!! You got it right in " << tries << " tries!!" << endl;

    system("pause");
    return 0;
}


replace if else with else if.

Point on efficiency; you can put tries++; outside of the if clauses, that way you only have to write it once.
Last edited on
ok ill do the tries thing after this but why is the answer like always 1 or 2??im also like always right 90 percent of the time??

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

using namespace std;

int main()
{
    int tries = 0;
    int answer;
    bool gameover = false;
    int guess;

    srand(time(0));
    answer = rand() %101;
    do{

    cout << "Im thinking of a number from 1 to 100. Guess what it is: ";
    cin >> guess;

    if(guess > answer){
        cout << "That guess is too high. Guess again!" << endl;
        tries++;
    }else if(guess > answer){
        cout << "That guess is too low. Guess again!" << endl;
        tries++;
    }else{
        tries++;
        gameover = true;
    }
    }while (!gameover);

    cout << "Good job!!! You got it right in " << tries << " tries!!" << endl;

    system("pause");
    return 0;
}
Last edited on
Something to consider...your if statement looks like this:

if(guess > answer)

Your else if looks like this:

else if(guess > answer)

Something doesn't make sense with your "if" and your "else if". Do you see it?
Last edited on
there's your answer... didn't spot that!
i dont get it? what is the problem...those if and else if statements look fine?
oh wait nevermind they are both >
Topic archived. No new replies allowed.