I am making a guessing game for my programming class, and i have it all working correctly, except that when you run the program the number it guesses is always lower. Are my math equations wrong? what do i need to change?
There are no error messages.
//P

//Project 8.4. Guesses the number the user enters between 1-100.
#include <iostream>
using namespace std;

int UsersNum, YesNo, HighLow, Re_Use;
int n = 50, lastguess;
int HigherThan = 0, LowerThan =101;

int main ()
{
    cout << "Welcome to the number guessing game! \n"; 
    cout << "I will try and guess the number you will enter. \n";
    cout << "Please enter a number between 1 - 100. \n";
    cin >> UsersNum;

    cout << "Is your number 50? Enter 1 for Yes or enter 2 for No. \n";
    cin >> YesNo;

    if (YesNo == 1){
        cout << "I have guessed your number correctly. Thanks for playing. \n";
        return 0;}              
do
{
    if (YesNo == 2){
        cout << "Higher or lower? \n";
        cout << "Enter 1 for higher or enter 2 for lower. \n";
        cin >> HighLow;}

    switch 
    {   case 1:(HighLow)
            n = (n - HigherThan) / 2 + HigherThan;
            HigherThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 1 for yes or 2 for no. \n";
            cin >> Re_Use;
        break;

        case 2:
            n = (LowerThan - n) / 2 + n;
            LowerThan = lastguess;
            cout << "Is this your number " <<n<< " ? \n";
            cout << "Please enter 1 for yes or 2 for no. \n";
            cin >> Re_Use;
        break;

        default:
            cout << "Invalid Entry. \n";
        break;
    }

    if (Re_Use == 1)
        cout << "I have guessed your number correctly! Thanks for playing. \n" ;
}
while (Re_Use == 2);

return 0;
}

Recommended Answers

All 4 Replies

At a quick glance, your switch statement should look like this:

switch(HighLow)
{
    case 1:
    ...
    break;

    case 2:
    ...
    break;

    default:
    ...
    break;
}

okay i fixed that. any help with the math part?

Well, you haven't assigned anything to lastGuess, which means you're assigning HigherThan and LowerThan to whatever happens to be in the memory location of lastGuess.

Im confused... what is it i should assign to it?

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.