The point of the program is a guessing game. The program chooses a number between 1-100 and the user guesses. If they guess too high it says 1, too low it says -1 and correct guess a 0. Then it outputs the number of tries it took. It works, but my teacher says that i should be using a == instead of =, and also the use of else instead of else if. Im lost on how to fix it. Please help.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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;
}
elseif (guess < number){
cout << "-1" <<endl;
}
elseif (guess = number){
cout << "0" <<endl;
cout << "Congrats you did it in " << trials << " tries" << endl;
}
} while (guess != number);
system("pause");
}
It checks if 'guess' is greater than or less than 'number', and if it is neither than in must be equal to 'number'. So, a simple 'else' statement will do.