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.

1 Like

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 ?

Hey, i have a question.
What does tries++ mean,i cant figure out what is its purpose.

the “++” on the variable “tries” increments the value by 1.

So whenever you run something that iterates multiple times and you want to keep track you can use an already initialized int variable and just add 1 to it.

So in this case:

tries++
translates to “Add 1 to the current value of tries”

it can also be written as:

tries = tries + 1 [I think. I don’t know if it works the same in C++ as it does in Python].

1 Like

Hey guys, super confused by this task.

Here’s my code:

int main() {

int guess;

int tries = 1;

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::cint >> guess;

tries++;

}

if (guess == 8) {

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

}

}

I compile and everything is fine, if I input the number 8 then I get the “You got it!” message. But if I get it wrong, I get nothing. It’s just dead. I tried my own way, then copied the hint and the same result. Any ideas?

Why won’t this work : removing “tries++” and making “<50” to “==50”?
Can there be a further explanation to ++. I read the comments but still confused why “++” is necessary near the end of the code. What I see is 49+1 but why not just have ==50?

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;

}

if (guess == 8) {

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

Can anyone explain why this code won’t work?

Increment and decrement operators are unary operators that add or subtract one, to or from their operand, respectively. They are commonly implemented in imperative programming languages. C-like languages feature two versions (pre- and post-) of each operator with slightly different semantics.

The increment operator increases, and the decrement operator decreases, the value of its operand by 1. The operand must have an arithmetic or pointer data type, and must refer to a modifiable data object. Pointers values are increased (or decreased) by an amount that makes them point to the next (or previous) element adjacent in memory. In languages that support both versions of the operators:

  • The pre -increment and pre -decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value.
  • The post -increment and post -decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand’s original value prior to the increment (or decrement) operation.

What the code is doing is this.

Set a variable guess but don’t give it a value other than its an integer.

Do the same with tries but set the int value to 0.

Send message to console an add a new line…

Send another message to console.

Prompt for user input.

Enter a while loop and repeat forever until guess does not equal 8 and tries equals 50.

After we leave the loop, if guess equals 8 then we send a message to the console.

Reading what I just said is that what you are trying to accomplish?

I understand very little of what you wrote but I’ll read your response again. I was wondering why ==50 wouldn’t work to replace <50 and i++? This is the only source I came across to learning code and have no other knowledge of the field. If 50 tries is the maximum number why couldn’t I just have “==50”? Why do I have to insert basically 49 (<50) then put i++ to make it to 50? again I’ll re-read to try to understand what you posted. Thanks

That is represented as ++i.

The post -increment and post -decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand’s original value prior to the increment (or decrement) operation.

That is represented as i++.

I was wondering why ==50 wouldn’t work to replace <50

Because the loops continues until a condition is met. That condition you set never finishes because you removed the increment variable inside the loop. Thus, the value never updates. So you could do =50 or <50, and never will be met until you add the increment variable back inside the loop.

Also, even if you guessed the correct number, it still won’t work because you want to do the correct guess AND 50 tries, on that iteration. So, basically if you did have the increment variable back in the loop, you would have to guess that number 50 times before you broke out of the loop.