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

line 49: Why is time() part of the equation? rand() will generate a random number, no need to add value returned by time().
int secret = 1 +(rand() % 100);

lines 56-65: Use a series of if statements instead of that switch statement.

if( guess < secret )
{

}
else if( guess == secret)
{

}
else // guess > secret
{

}

lines 7-12: delete that enumeration -- I don't see any need for it.

I don't see the user's guess actually being compared to anything.

The test variable never gets set to anything, yet you use it to control the switch. Wait, do you mean the integer test that is global, or the Outcome test in main?

Hi everyone!
Actually i did big misstake :P The first one i put the vairable (outcome test = OK ) is wrong and the second one in switch statement for test . I have to make prototype function of test to get output correctlly so i changed my code it will be (test (int guess, int secret)) instead of showResult. So i fixed my code and now it works :). This is update my code.

#include<iostream>
#include<ctime>
using namespace std;

const int MAXTRY = 7;
int Guess[MAXTRY];
enum Outcome
{
    TOOLOW,
    TOOHIGH,
    OK,
};
//Prototypes functions
void gameSession();
int test(int guess, int secret);
 //int test;
int main()
{
    srand(static_cast<unsigned int>(time(0)));
    char answer;
    int guess = 0;
    int secret = 0;
    //int test;
    do
    {
        do
        {
            system("CLS");
            cout << "HI-LO game" << endl
                << "**********" << endl << endl;


            gameSession();
            //test = 
            test(guess, secret);

        } while (guess != secret);

        cout << "One more time (Y/N)? ";
        cin >> answer;
    } while (answer == 'Y' || answer == 'y');
    return 0;
}

void gameSession()
{
    int guess = 0;
    bool finished = false;
    int secret = 1 +(rand() % (100 -1 + 1));
    int nrOfTries = 0;

    do
        {
            cout << "Guess a number between 1-100: ";
            cin >> guess;
            Guess[nrOfTries] = guess;
            ++nrOfTries;
            for (int i = 0; i < nrOfTries; i++)
            {
                cout << "Guess: " << Guess[i] << "\n";
            }
            switch (test(guess, secret))
            {
            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;
            default:
                cout << "your guess is not within the guidelines of this game" << endl;

            }

            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;
    }

}

int test(int guess, int secret)
{

    if (secret > guess)
        return TOOLOW;
    else if (secret < guess)
        return TOOHIGH;
    else if (secret == guess)
        return OK;
    return 0;

}

Thanks for eveyone ;)

Are you required to have a switch statement in your program? If not, then why not just simplify it by replacing the swith with the if statemsements in test()? Simple is good -- no point in making it more complicated than necessary just to show off your programming skills.

Hi there yeah it is required to use the switch statement during programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.