Trouble with bracketing search exercise.

Hey.

I've been reading these forums for ages, trying over and over to find a solution to my problem and, despite many answers to questions surrounding this exercise, none have actually answered my specific question. I have also researched and asked many friends etc. to no avail.
Anyway, that being said, I'll just ask the damned question.

I've been writing the bracketing search exercise for a while now. The program runs well. I've eliminated the issue of the computer guessing outside of the 1 - 100 limit and it's also remembering and guessing between the high and low values set after each user input.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    int x;
    int h = 100;
    int l = 1;
    int g = 1;
    char yn;
    char hlc;
    srand(time(0));
    x = 50;
//My variables. yn is just for "are you ready?", hlc is for "too high, too low, correct". g is guesses.


// This is the code for after the 1st guess (which at the moment is always 50).
        while (g < 7)
        {
        g++;
        x = rand()%(h - l) + l;
        cout << endl << "Ok. Guess number " << g <<  " is: " << x << endl;
        cout << "Am I right? (H/L/C): ";
        cin >> hlc;
        switch (hlc)
        {
            case 'h':
                h = x;
            break;
            case 'l':
                l = x;
            break;
        }


I cannot for the life of me work out two things...

One is how to stop duplicate numbers from appearing, as this just wastes guesses. I've seen some examples of how to do so on here before, but they all seem way out of my league in terms of ability.

The second I think is a question of my maths. I'm wondering if someone could give me some tips on what formula to use to whittle down the numbers in fewer guesses.

If you think I'm stupid and terrible at programming, I understand and am totally ok with you telling me. Some answers would be wonderful though.

I really appreciate any help given. Thanks in advance :)
Mathematically, you're better off at line 6 to guess half the difference between h and l, rather than taking a random number. You will converge on the answer faster.
Thank you... SO MUCH!

You are literally the best person ever for the next few days.

Seriously though, it works beautifully now. No duplicates and it's correct every time.
Thanks again! :)
Topic archived. No new replies allowed.