Need help with number guessing program

I'm trying to make a program where the user guesses a predetermined number in a program with only 10 tries before the program ends. I can't figure out how to have the code end once the user runs out of tries, can anyone help me? Really stumped on this.
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
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    
    int myAge = 15;
    int guess;
    int guessCount = (10);
    cout << "Hi! Lets play a game where you guess my age. Make any guess between 0 and 100\n";
    while (guess != myAge){

    if (guess != myAge) {
        guessCount = guessCount - (1);
        cin >> guess;
        if (guess > 15){
                
            cout << "Lower! You have " << guessCount << " guesses remaining";
              if (guess > 100) {
            cout << "\nThats just preposterous. Of course I'm not that old. You have " << guessCount << " guesses remaining\n";
                
              }
        }
        if (guess < 15){
              cout << "\nHigher! You have " << guessCount <<" guesses remaining.\n";
              if (guess < 0){
              
             cout <<"\nThat's just preposterous. Of course my age isn't a negative number idiot. You have " << guessCount <<" guesses remaining\n";
                
                 }
            }
           
         else if (guess == myAge) {
         cout << "Good job";
            cout << endl;
         }
        else if (guessCount == 0) {
        cout << "You lost. Fool.";
           cout << endl;
          }
         }
        }                          
    return 0;}

please dont help me with optimizing the code or anything like that. I just need help with the last bit of code
Last edited on
I cant check this thread often, so I might not get back until tomorrow . srry
You can add a break; statement after the "you lost" line. This immediately breaks out of the while loop.

Or, make the looping condition be: while (guess != myAge && guessCount > 0)

Also, your guess variable is uninitialized. You should initialize to some invalid value like -1.
Also also, your indentation is inconsistent, and this makes your code hard to read and makes it prone to errors.
Last edited on
you have the right idea: decrement a counter (x--; is the same as x = x -1).
the problem is your if and else if chains are not fully connected, so its not always going down a path that can end the program. This is easier to see if you align your braces rather than use book style. Book style is valid, but it hides some alignment errors.

you want to tie all the logic together.
something that flows like this logic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(guesscount && guess!= age)
{
   if (invalid) //negative, and possibly > 130 or something
   invalid stuff;
   else if (win) 
    winstuff;
   else if(lower)
    lowerstuff;
   else if(higher)
    higherstuff; 
//***********************************
  guesscount--; //not under any condition
}//end loop
-> if guess count is zero here, they lost.
->else they won. 

Last edited on
Perhaps something like:

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

int main() {
	constexpr int myAge {15};
	constexpr int MaxGuess {10};
	int guess {};

	std::cout << "Hi! Lets play a game where you guess my age.\n";

	for (int guessCount {MaxGuess}; guessCount && guess != myAge; --guessCount) {
		std::cout << "\nYou have " << guessCount << " attempts remaining. Make any guess between 0 and 100: ";
		std::cin >> guess;

		if (guess > myAge)
			if (guess > 100)
				std::cout << "That's just preposterous. Of course I'm not that old.\n";
			else
				std::cout << "Lower!\n";
		else
			if (guess < 0)
				std::cout << "That's just preposterous. Of course my age isn't a negative number idiot.\n";
			else if (guess < myAge)
				std::cout << "Higher!\n";
	}

	if (guess != myAge)
		std::cout << "\nYou lost. Fool.\n";
	else
		std::cout << "\nGood job.\n";
}


Update: thanks guys! appreciate the help a lot.
Topic archived. No new replies allowed.