Hi, i'm trying to make a simple C++ program in which the user must try to guess a number, if they guess too high it says "too high" and if they guess too low it says "too low".
I also decided to add a feature which allows them to select how many tries they would like to guess the number. I tried to make "tries" type an enum so if the user could not pick an invalid number but for some reason i cannot use it in an if statement.
here is the code and i am getting the first error on line 27:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int guess;
int i;
int endProgram = 0;
int win = 0;
int j = 0;
enum tries
{
five = 5,
ten = 10,
fifthteen = 15,
twenty = 20
};
int getValue()
{
srand(time(NULL));
i = rand() % 100 + 1;
}
int finish()
{
if (guess == i || j == tries)
{
endProgram = 1;
}
else if ( guess > (i + 10) && guess < (i + 50) && guess > (i - 50))
{
cout <<"\n\n\n\nTOO HIGH\n\n\n\n";
}
else if (guess < (i - 10) && guess < (i + 50) && guess > (i - 50))
{
cout <<"\n\n\n\nTOO LOW\n\n\n\n";
}
else if (guess >= (i - 10) && guess < i)
{
cout <<"\n\n\n\nA LITTLE TOO LOW\n\n\n\n";
}
else if (guess >= (i + 10) && guess > i)
{
cout <<"\n\n\n\nA LITTLE TOO HIGH\n\n\n\n";
}
else if (guess >= (i + 50))
{
cout <<"\n\n\n\nWAY TOO HIGH\n\n\n\n";
}
else if (guess <= (i - 50))
{
cout <<"\n\n\n\nWAY TOO LOW\n\n\n\n";
}
}
int main()
{
char retry;
getValue();
cout <<"HOW MANY ATTEMPTS WOULD YOU LIKE (5, 10, 15, 20): ";
cin >>tries;
cout <<"\n\n\n\n";
while (endProgram != 1)
{
cout <<"TAKE A GUESS: ";
cin >>guess;
finish();
j++;
}
if (guess == i)
{
cout <<"CONGRATULATIONS YOU GUESSED THE NUMBER IN " <<j <<"/" <<tries <<" TRIES!";
}
else
{
cout <<"UNLUCKY, YOU FAILED TO GUESS THE NUMBER"
}
cout <<"\n\nPLAY AGAIN? (y/n)";
cin >>retry;
if (retry == 'y')
{
return main();
}
}
Could someone please explain to me what I am doing wrong here?
Thanks