random number generation

I need some help getting the computer to guess a number within 7 tries. I think I know what needs to be done but I'm not sure how to implement in code. For instance, user enters 50, computer guess 75, then the computer must guess again but lower than 75 etc.

The problem I have is I can get the computer to guess less than 75 on the first turn but after that it doesn't work.

Anyways my code is

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(NULL));
    int num = rand() % 100 + 1, guess = -1, count = 0, sent, numGuessUp = 0, numGuessDown = 0;
    cout << "Enter a number for the computer to guess: ";
    cin >> guess;
    do {
        if (num > guess){
            cout << num << "guess to high" << endl;
            num = rand() % num + 1;
        }
        else if (num < guess){
            cout << num << "guess to low" << endl;
            num = rand() % 100 + num;
        } else {
            cout << "Computer got it right";
        }

        cout << "sent: ";
        cin >> sent;
        cout << endl;

    } while (sent != -1);
    cout << count;

    return 0;
}

  
Topic archived. No new replies allowed.