My number guesser! Critisize me

So I've been working through these exercises on here, and I just finished the one where you enter a number, then it guesses it. I'm gonna post it, and I want you to point out every thing I did wrong with it. I wish to learn as much as I can from doing these practice problems.

P.S. Yes, I realize I have no input validation, and as I was writing this I also realized that I don't even need the user to enter the number, but I guess that keeps them from cheating :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
This program will just guess whatever number you give it in the range
of 1 - 100. At some point, I will have it down to 7 guesses or less from
the computer.

Started on 12/15/2011
*/

//Library inclusion
#include <iostream>
#include <String>
#include <stdlib.h>
#include <time.h>

//using statements
using std::cout;
using std::cin;
using std::endl;

//Variable declarations
int userNumber;
int userResponse;
int guessedNumber;

//Function declarations
void NumberGuess(int targetNumber)
{
    srand(time(NULL));
    int randomNumber = rand() % 100 + 1;
    int upperBound = 100;
    int lowerBound = 1;
    int choice;
    int numberOfGuesses = 1;
    bool numGuessed = false;

    while(numGuessed == false)
    {
        cout << "Here's my guess " << randomNumber << endl;
        cout << "Was my number too high, too low, or correct?\n";
        cout << "1. Too high.\n";
        cout << "2. Too low.\n";
        cout << "3. Correct!\n";
        cin >> choice;
        if(choice == 1)
        {
            upperBound = randomNumber;
            randomNumber = rand() % (upperBound - lowerBound) + lowerBound;
            numberOfGuesses++;
        }
        else if(choice == 2)
        {
            lowerBound = randomNumber;
            randomNumber = rand() % (upperBound - lowerBound) + lowerBound;
            numberOfGuesses++;
        }
        else if(choice == 3)
        {
            cout << "Cool! Told you I could guess it!\n";
            cout << "It only took me " << numberOfGuesses << " guesses!";
            numGuessed = true;
        }


    }
}

int main()
{

    //Dialog explaining to the user what to do
    cout << "Welcome to my program! I bet I can guess any number" << endl;
    cout << "you give me between 1 and 100!" << endl;
    cout << endl;
    cout << "So go ahead, give me a number. I'll then try to guess it." << endl;
    cout << "All I need you to do is tell me too high, or too low." << endl;

    //Gets user input and stores it into userNumber
    cin >> userNumber;


    //Runs while user did NOT enter a number between 1 and 100.
    while(userNumber < 0 || userNumber > 100)
    {
        cout << "Sorry! I can only do numbers from 1 to 100. \n";
        cout << "How about you try again?" << endl;
        cin >> userNumber;
    }

    //Confirms number was received and guessing will commence
    cout << "Cool! Thanks for the number. Now I'll begin to guess it." << endl;

    //Calls function for guessing
    NumberGuess(userNumber);



    return 0;
}
Last edited on
What if they lie and say you guessed below when it was actually higher? I'd put in some checks for that somewhere.

Also, you don't ever use the number they give you, so why do you even ask for it?
Yea I said I didn't really need the number entered. Waste of memory, I know.
The first issue you said was brought to my attention when I friend of mine was messing with it. He tried it, and ended up causing the program to crash. Im not sure how I would fix that and still keep the program from seemingly not knowing their number.
Think about this for a second:

You guess 20.
They say it's too low.
You guess 21.
They say it's too high.

What does this tell you? n > 20 && n < 21. That's impossible. If this was the case, what would your upper and lower bounds be at this point and how could you check?
So, if upper bound is equal to lower bound + 1, we have an issue? Or if lower bound is greater than upper bound and vice versa?
I guess a better algorithm would be to always keep your guess as the average of the upper bound and the lower bound... That way, you're bound to guess it much quicker than guessing random numbers while shortening your range... Like, for example:
Suppose their number is 51:
You guess 30: Too low.
You guess 31: Too low.
You guess 32: Too low.
.
.
.
Get the idea..? I know this is unlikely to happen, but basically, the number of guesses is more. This way, your range will reduce by a large amount after every guess... Just a suggestion.. :)
Hmm, thank you Caprico. I was actually trying to figure out a way to cut down on my number of guesses. The most it took was 8, but this sounds like it will cut it down quite a bit.
So...
 
guess = (upperBound + lowerBound) / 2;
?
Last edited on
Yup.. That should work well... I guess in the worst case scenario it should take 6 guesses..
6 guesses? How did you come up with this number?
Your range is basically 1-100. So considering that each guess is higher than the actual number:
50->25->13->7->4->2.
At this point you know the answer is either 1 or 3. This is your last and correct guess (so technically 7 guesses).
Also, since the number can either be higher or lower, the problem is symmetric about any choice. Hence no matter what the number is, the program will guess it correctly at the 7th guess (max). :)

This is pretty sound logic here. Thank you for all your help!
Always welcome.. :)
Topic archived. No new replies allowed.