Making a number guessing game

closed account (jwC5fSEw)
One of the exercises I found on this forum is to make a guessing game where the user thinks of a number and the computer has to get the right answer in seven or less guesses. I know how to get it in seven or less guesses, but I'm having trouble with what seems like a simple while loop

WARNING: this is a spoiler for one of the exercises found in the Articles forum. Don't look if you want to solve it yourself.
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
// Computer guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int getNum(int guess) {
    unsigned int i = 1;
    cout << "I guess " << guess << ". If that is correct, enter 1. If it's too high, enter 2. If it's too low, enter 3. ";
    while (i && i < 4) {
        cin >> i;
    }
    return i;
}

int main() {
    srand(time(NULL));

    int guess;
    int count = 0;
    int result;
    bool hasWon = false;
    while (!hasWon) {
        cout << "Think of a number. The computer will guess, and you must answer (truthfully!) whether the guess is too high or too low.\n";
        if (!count) {
            guess = 50;
            result = getNum(guess);
            switch(result) {
                case 1:
                    cout << "I win!\n";
                    hasWon = true;
                    break;
                case 2:
                    cout << "Too high, eh?\n";
                    break;
                case 3:
                    cout << "Two low, eh?\n";
                    break;
            }
        }
        else {
            switch(result) {
                case 2:
                    guess /= 2;
                    break;
                case 3:
                    guess += guess / 2;
            }
            result = getNum(guess);
        }
        ++count;
    }
}


The while loop on line 10 repeats endlessly, constantly asking for input. I'm frustrated that something as simple as a while loop is foiling me, but I can't figure out what's going wrong. I'm sure the answer is right there, could someone tell me what's wrong?
Your logical operator is probably not doing what you think.

while (i && i < 4) means while i is True AND i is less than 4.

This loop should only end if you input a 0 (Zero=False).
closed account (jwC5fSEw)
Ah, I knew it was gonna be something stupid like that. I gotta pay more attention. Thanks!

Also, now that that's fixed, I realize this program doesn't work close to as intended, so I'm fixing the rest of it.
Topic archived. No new replies allowed.