Guessing within 10 tries

So Ive been trying to write a program that guesses a number 100-999 within 10 tries. I cant figure out how to write the tries remaining along with if the number is higher or lower than the guess. It needs to count down, most examples im finding are just showing the number of tries in which they guessed the number.

do {
// generate random number between 100 - 999
number = (rand() % (999 - 100 + 1)) + 100;

cout << "You have 10 tries!\n";
cout << "Guess the number (100-999): ";
for (int tries = 0; tries < 10; tries--) {
cin >> guess;
// validate users input
if ((guess < 100) || (guess > 999)) {
cout << endl << "You must guess a number between 100 - 999. Guess again: ";
tries++;
}

I'm really lost as to where to take this! please help!
You do it just like your validation..
if(guess == number) //win
win code;
else if(guess < number) //
//less than code
else //greater than code

tries is being modified by the loop and inside the loop.
That is wrong, just do one or the other.
to count down:
for(tries = 10; tries; --tries)
and print how many tries they have left
or count up and say how many they have used, whatever on that...

its boring, but the binary search ensures you can guess in just a few tries ... you cut the number of options in half each time...
Last edited on
I find using a while loop more intuitive.
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
#include <iostream>
#include <ctime>

const int MaxGuesses = 10;

int main()
{
    int num_guesses = 0, input = 0;
    bool found = false;
    
    srand(time(nullptr));
    int number_to_guess = (rand() % (999 - 100 + 1)) + 100;
    // For testing
    // std::cout << "Number to guess: " << number_to_guess << "\n";
    
    while (num_guesses < MaxGuesses && !found)
    {
        
        std::cout << "Enter number between 100 and 999: ";
        std::cin >> input;
        num_guesses++;
        // maybe do some input validation here
        if(input == number_to_guess)
        {
            found = true;
            std::cout << "You found it!\n";
            break;
        }
    }
    if(!found) // only wrong guesses
        std::cout << "Sorry, you didn't guess it.\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <random>

constexpr unsigned MaxGuesses {10};

int main() {
	std::mt19937 rng(std::random_device {}());
	const std::uniform_int_distribution<unsigned> distrib(100, 999);
	const auto number_to_guess {distrib(rng)};
	unsigned num_guesses {};

	// For testing
	// std::cout << "Number to guess: " << number_to_guess << "\n";

	for (unsigned input {}; num_guesses < MaxGuesses; ++num_guesses)
		if ((std::cout << "Guess " << num_guesses + 1 << " of " << MaxGuesses << ". Enter number between 100 and 999: ") && (std::cin >> input) && (input == number_to_guess))
			break;

	if (num_guesses < MaxGuesses)
		std::cout << "You found it!\n";
	else
		std::cout << "Sorry, you didn't guess it.\n";
}

Topic archived. No new replies allowed.