Guessing game need help. Basic C++

I need help with changing my program. The program is a guessing game where it chooses a number from 1-100. We need to input a number and if it works, it outputs 0. If the number is over the guess number it says 1 and if it under the guess number it says -1. I got it to work the way it was but my teacher wants me to change else if to else and also changing the = to == and i tried to but i failed. Ill show you my original code.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){
    int number, guess, trials = 0;
    srand(time(0));
    number = rand() % 100 + 1;
    cout << "Guess the number. If correct you will get a 1, wrong a -1, and correct a 0." << endl;
    do{
        cout << "Please enter a value between 1 and 100." << endl;
        cin >> guess;
        ++trials;
    
        if (guess > number){
            cout << "1" <<endl;
        }
        else if (guess < number){
            cout << "-1" <<endl;
        }
        else if (guess = number){
            cout << "0" <<endl;
            cout << "Congrats you did it in " << trials << " tries" << endl;
        }
        
    } while (guess != number);
    
    system("pause");
}
And your "second" code?

You don't seem to understand the difference between the assignment operator= and the comparison operator==.

You also don't seem to be aware that if guess is not greater than number and if guess is not less than number guess must be equal to number, so you don't really need that last if statement.

Also what is the purpose of the variable trials?

Topic archived. No new replies allowed.