Function calling in another function help

I'm making a guessing game where the program has to guess the user's number, so it's kinda backwards than some of the guessing games I've seen. I'm having trouble using the variable of one function and calling it in another one. My variable 'guess' works in one function but I want to transfer that value the user gives it and use it in another function retaining the value.

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
void playOneGame();
void getUserResponseToGuess(char);
int getMidpointLow(int, int);
int getMidpointHigh(int, int);
bool shouldPlayAgain();

int main()
{
    do
    {
        playOneGame();
    }
        while (shouldPlayAgain());
    return 0;
}


// starts the game creates a random guess for the user to tell whether it's 
// high, lower, or the correct number they have in mind

void playOneGame()
{
    static int low = 0;
    static int high = 100;
    static int guess;
    
    unsigned seed = time(0);
    srand(seed);
    guess = (rand() % (high - low + 1)) + low;
    
    cout << "Go pick a number between 0 - 100 and I will guess it" << endl;
    cout <<  "If I am wrong, press h if your number is higher, l if it's";
    cout << "lower than my guess and c if I get";
    cout << " it correct" << endl;
    cout << "My guess is that your number is: " << guess << endl;
    getUserResponseToGuess(guess);
}


// asks the user for a guess and checks whether it's higher or lower
// this is where I'm having problems with
// I want to use the value from 'guess' and use it in this function that  
// would call another function to use it

char getUserResponeToGuess(int guess)
{
    char user_guess;
    
    cin >> user_guess;
    if(user_guess == 'h')
        getMidpointHigh(guess, 100);
    if (user_guess == 'l')
        getMidpointLow(0, guess);
    return user_guess;
}

// if the guess was too low, the program would take the guess and give it 
// the value of the lower range and it should give another guess
int getMidpointHigh(int low, int high)
{
    static int midpoint;
        midpoint = (high - low)/2;
        low = low + midpoint;
    
    return midpoint;
}

// same as above but if the value is lower, the program's guess is given the 
// higher range
int getMidpointLow(int low, int high)
{
    static int midpoint;

        midpoint = (low - high)/2;
        high = high - midpoint;
    return midpoint;
}

// at the end of the midpoint functions, it should change the high/lower
// range to the midpoint that it guessed and whether the 
// user entered 'h' meaning that the number guess is too low
// or 'l' if the number guessed is too high 

Last edited on
Why the midpoints? Shouldn't the logic be:

Choose guess from [low-high]
IF guess is too small, low=guess;
OR
IF guess is too large, high=guess;


Note You don't handle case 'c' at all.
Last edited on
Topic archived. No new replies allowed.