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.
#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");
}
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.