If in DOWHILE loop?

I want the loop to stop when totalGuesses turn into 0. How do I do that? In the bottom code I added a break for this condition but the first time I run it. it breaks automatically.
Ignore the identation.
1
2
3
4
5
6
7
8
9
10
11
12
13
  int totalGuesses =11 ;
	if(guess != number)
		do{
			cout<< "Wrong, enter your guess: ";
			cin>> guess;
			cout<<"Correct Digits: ";
			cout<<"Correct Spots: ";
			cout<<"Guesses left: "<<totalGuesses;
			totalGuesses-- ;
			if(totalGuesses==0)	
				cout<<"u lose";break;
				
		}while(guess != number);
Last edited on
1
2
if(totalGuesses==0)	
				cout<<"u lose";break;

is the same as
1
2
3
4
5
if(totalGuesses==0)	
{
	cout<<"u lose";
}
break;


See your mistake?
Yea I got it
1
2
3
if (totalGuesses==-1){
     cout<<"u lose";break
}

Last edited on
1
2
3
4
if(totalGuesses==0)	
{
	// EVERYTHING you want to happen if the condition is true goes in here
}
Are these different?
1
2
3
4
5
6
7
8
9
10
// A
if ( guess != number )
  do {
    // code
  } while ( guess != number );

// B
while ( guess != number ) {
  // code
}


Are these different?
1
2
3
4
5
6
7
8
9
10
11
12
13
// C
while ( guess != number ) {
  cin>> guess;
  --totalGuesses;
  if (totalGuesses==0) break;
}

// D
while ( guess != number ) {
  if (totalGuesses==0) break;
  cin>> guess;
  --totalGuesses;
}
Topic archived. No new replies allowed.