FAQ: Loops - Guess Number

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.

1 Like

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.

1 Like

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 ?

1 Like

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].

2 Likes

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?

1 Like

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.

I have a question just to clarify my understanding of the material. My program runs as expected when numbers are entered. When a letter or any string of characters besides a number is entered as a guess, the program returns a page or two worth of “Wrong guess, try again” and exits. I can hypothesize about why that might happen, but there’s nothing explicitly causing that in the code, so anything I come up with is a wild guess.

I know it’s not a learning goal for this particular lesson, but it seemed like a good place to ask whether this behavior can be controlled, and whether this behavior is an expected and predictable response in c++ or maybe an artifact of the virtual terminal.

#include <iostream>

int main() {
int guess;
int tries = 0;
  
std::cout << "I have a number 1-10.\n";
  std::cout << "Please guess it: ";
  std::cin >> guess;
  
  while (guess != 8 && tries < 50){
    std::cout << "Wrong guess, try again: \n";
    std::cin >> guess;
    tries++;}
  
if (guess == 8) {
std::cout << "You got it!\n";
  }  
}
1 Like

very late, but it keeps count of how many tries you’ve attempted by incrementing the variable tries by one every time you guess a number

1 Like

I think that, in the sample code provided, the initial value for tries should be declared as 1 and not 0. If it is 0, the program will let you try 51 times This is easy to detect if you change 50 with 5 - it lets you try 6 times.

Also slightly out of the scope of the exercise, but I’m curious: With the default code, why do I not need a new line symbol within the while loop? It seems like every other time I forget a \n at the end of my std::cout stuff, it mashes the line into the end of the previous one, but this doesn’t.

For reference, the while loop I’m using:

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

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

tries++;

}

Does std::cin count as a new line marker or something (since I notice that the first guess doesn’t have a \n either)?

please can someone explain to me why that tries++ stuff is necessary, i would really to its function?

who needs 50 tries to guess a number between 1-10?
this doesn’t make any sense to me.