FAQ: Loops - Guess Number

This community-built FAQ covers the “Guess Number” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Guess Number

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’ve compiled and run the following code for “guess number” exercise, and it works for the correct answer but if you enter the wrong answer it just exits the program. Any ideas?

#include

int main() {

int guess;

int tries;

std::cout << “I have a number between 1-10.\n”;
std::cout << "Please guess it: ";
std::cin >> guess;

// Write a while loop here:

while (guess != 8 && tries < 50) {

std::cout << "Wrong guess, try again: ";
std::cin >> guess;

tries++;

}

if (guess == 8) {

std::cout << "You've got it!\n";

}

}

Hi buckydharma.

If you declare tries = 1

int tries = 1;

as above and your program will work.

Iam new to this so i dont really know, but i think the while loop cant compare any “tries < 50” if its just declared as tries.

That worked great – thank you!

Hi!
I have a doubt. on the exercice :

#include

int main() {

int guess;

int tries = 0;

std::cout << “I have a number 1-10.\n”;
std::cout << "Please guess it: ";
std::cin >> guess;

// Write a while loop here:
while (guess !=8 && tries < 50)
{
std::cout << “Wrong guess, try again :”;
std::cin >> guess;
tries++;
}

if (guess == 8) {

std::cout << "You got it!\n";

}

}

on the while condition means :
while (guess !=8 && tries < 50)

Does that mean that the while continue just tries = 50 or it continue just to guess isn’t equal to 8 AND tries is less than 50 so if guess isn t equal to 8, it can ask you how many time it is necessary before the condition ?

the ‘&&’ is it here just for the exercice because the while condition could be while (guess !=8) . if we want to limit the tryes, do we have to had an : if tries == 3 or something like that ?

Thanks

Hi @freejackjc,

The loop will continue while guess does not equal 8 and tries is less than 50. The && logical operator means that both conditions (guess != 8 and tries < 50, in this case) must be true for the loop to continue. If the guess is 8, or tries is equal to 50, the loop will stop operation.

If you wanted to limit the number of tries to a smaller number, you just need to change “50” to a different value, like below:

while (guess != 8 && tries < 3)

Because tries starts with a value of 0, this will limit the user to 3 tries.

Your default code creates an infinite loop:

while (guess == 8) { ...}

If you happen to compile this example then happen to guess 8, you will crash your browser tab. I would probably warn users of this or leave this out entirely until the next step.

What worked ? As I am having the same problem as you.It is already
int tries = 0;
so its correct nothing wrong in it , maybe glitch of terminal ?