0

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

Edited by vicky30312

2
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by vicky30312
0

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

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

    case 2:
    ...
    break;

    default:
    ...
    break;
}
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.