I cannot figure out why my loop doesnt work!

I need to design a guess the number game that only allows five tries...please help if you can.

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main ()
{
int magic;
int guess;
int count;
srand(time(NULL));
magic = rand() % (100 + 1);



for(count=0; count >= 6; count++) {

cout << "Enter your guess: " << endl;
cin >> guess;

if(guess == magic)
{
cout << "**Right** " << endl;
break;
}
else {
cout << "Wrong guess: " << endl;

if(guess < magic)
cout << "Your guess is lower than the magic number. " << endl;
else
cout << "Your guess is higher than the magic number. " << endl;
}
cout << "You have " << count << " many tries left. " << endl;


cout << "**You have guessed the magic number!**" << endl;
}
return 0;
}
@Def Muse

For one, your for loop is wrong
for(count=0; count >= 6; count++) should be
for(count=0; count < 5; ++count // That gives you 0,1,2,3 and 4. So, 5 guesses

For two, you're showing how many guesses (or counts ) were used, not how many are left. For that, you could use
cout << "You have " << 4 - count << " tries left. " << endl;

Move the line cout << "**You have guessed the magic number!**" << endl; under the line that says "**Right**", otherwise, it prints it no matter if you're high or low
Last edited on
for(count=0; count >= 6; count++) {

This loop will run exactly zero times.

for loops operate like so:

1
2
3
4
for( initialize; condition; iterate )
{
  body
}


1) initialize is done first
2) condition is checked. If condition is false... then the loop exits. Otherwise...
3) body is performed
4) iterate is performed
5) go to #2


So what's happening in your case is this:

1) Initialize: count = 0
2) Check condition: count >= 6

since count is 0, that makes the condition false... therefore the loop immediately exits.
Last edited on
Thank you both for taking time to help further my knowledge and education.
Topic archived. No new replies allowed.