I cannot get the math right for the higher option. When you run the program, the program will go lower, but it wont go go higher. What is it i need to change? this program is due by the end of my class today and i can't figure it out.

//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;}              
    if (YesNo == 2){
        cout << "Higher or lower? \n";
        cout << "Enter 1 for higher or enter 2 for lower. \n";
        cin >> HighLow;}

    switch (HighLow)
    {   case 1:
            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;

        case 2:
            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;

            cout << "Invalid Entry. \n";

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

return 0;

Recommended Answers

All 6 Replies

i figured out what i have to do but i cant figure out how to do it. I have to make LastGuess = the last guessed number, but i dont know what to declare it to...

You can simplify the coding and make it more obvious by using this pseudo-code. It eliminates some unnecessary testing and setup.

set low and high range for guess (lo=1 hi=100)
read user number


    calculate guess as hi - (hi-lo)/2
    if this is the correct number then exit loop

    if guess was too high then
        set lo to guess + 1
        set hi to guess - 1
    end if

end loop

"I guessed the number"

that doesnt help :/ I am fine with my code just not the math. that is what i need help with!

See lines 6, 10 and 12 in my previous post. What other math is required? And you might reconsider the structure of your code. If you are writing it for an assignment then you are going to lose marks for clarity and redundant/unnecessary code.

we were told in the project to do switch statments

Then use one

prompt "is this guess hi (h) or low(l): "
get response

switch (response) {
    case "h":
        set lo to guess + 1
    case "l":
        set hi to guess - 1
        "invalid entry"
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.