Hello everybody!
I need some help in my program it is about Guess number game. the program is working but not that I have expected, when i enter guess 76 the output gives me that 76 is Too small :( instead of Too big .Even I type any number 64,99 it gives the same output (Too small). How i can fixed?
#include<iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
const int MAXTRY = 7;
enum Outcome
{
TOOLOW,
TOOHIGH,
OK,
};
//Prototypes functions
void gameSession();
int showResult(int guess, int secret);
int test;
int main()
{
srand(static_cast<unsigned int>(time(0)));
char answer;
int guess = 0;
int secret = 0;
Outcome test = OK;
do
{
do
{
system("CLS");
cout << "HI-LO game" << endl
<< "**********" << endl << endl;
gameSession();
showResult(guess, secret);
} while (guess != secret);
cout << "One more time (Y/N)? ";
cin >> answer;
} while (answer == 'Y' || answer == 'y');
return 0;
}
void gameSession()
{
int guess;
bool finished = false;
int secret = 1 +( rand() + time (0)) % 100;
int nrOfTries = 0;
do
{
cout << "Guess a number between 1-100: ";
cin >> guess;
++nrOfTries;
switch (test)
{
case TOOLOW: cout << "Too small! ";//TOOLOW
break;
case TOOHIGH: cout << "Too big! ";
break;
case OK:cout << "You guessed correct in " << nrOfTries << " tries!" << endl;
finished = true;
break;
}
//test = showResult(guess, secret); //OK
if (!finished)
{
cout << MAXTRY - nrOfTries << "tries left\n";
}
} while (!finished && nrOfTries < MAXTRY);
if (!finished)
{
cout << "Sorry, you lost...:-( \n. The secret number was " << secret << endl;
}
//return test;
}
int showResult(int guess, int secret)
{
//int test = 0;
if (test == TOOLOW)
{
if (guess < secret)
{
}
}
if (test == TOOHIGH)
{
if (guess > secret)
{
}
}
if (test == OK)
{
if (guess == secret)
{
}
}
return test;
}
Inline Code Example Here