# Thread: Program to prompt user to guess number and re-guess if outside range - almost there!

1. Originally Posted by Interista
Sorry.
Apology accepted

Originally Posted by Interista
And I fixed the problem with five ifs, so maybe my tantrum paid off cos I started thinking of other things than the program.
Yeah, taking a break can help. Anyway, you don't need 5 if statements in the loop body: the two that you had are fine. In fact, they are key to how you should write your loop condition. The logic should be something like this:
Code:
```print request for guess
while guess is not in the range:
if guess is greater than the upper bound of the range:
print "too high"
else if guess is less than the lower bound of the range:
print "too low"

2. Originally Posted by laserlight
Apology accepted

Yeah, taking a break can help. Anyway, you don't need 5 if statements in the loop body: the two that you had are fine. In fact, they are key to how you should write your loop condition. The logic should be something like this:
Code:
```print request for guess
while guess is not in the range:
if guess is greater than the upper bound of the range:
print "too high"
else if guess is less than the lower bound of the range:
print "too low"
Problem is I've been at this assignment, which is supposed to take 10 hours max, for about 25 hours. I'm fed up. I really don't want to do anymore, and it could well be futile to do more anyway because if next week is as bad as this I'll be dropping off the course.

I'm not a kid throwing my toys out the pram, I'm a lot older than a kid, and I've done enough courses on other things to know that things are not always rosy, and things need work. But the course is ridiculous, they give us about 1% of the information we need and expect us to work the things out ourselves.

Which would be likely to get me higher marks, submitting the code I initially gave you which doesn't completely work or submitting the code with the five ifs that works but is too long. Honestly, I'm drawing a line on this assignment now, I can't do anymore or I'll lose my mind. My neck is sore, my fingers are sore, my brain hurts.

3. Originally Posted by Interista
Not being funny, but as I said above, we're all having huge problems with our course because its being taught badly. We're trying to get something done about it now. They told us how to use while, how to enter it, but not exit it. Hence you may as well ask me what's the Russian for elephant as I've as much chance of knowing.

I tried switching the whiles around but it doesn't work. That's logic, but doesn't do anything.
Exiting while is pretty simple... program flow (called "control") resumes at the closing brace when the condition is no longer met.

For example...
Code:
```#include <stdio.h>

int main (void)
{
int x = 0;

while ( x < 5 )
{
printf("%d  ",x);
x++;
}
printf("Done!\n\n");

return 0;
}```
Type that up and run it... you'll see how it works.

As for the course... we hear this complaint a lot, especially from India where they are turning out 1980s grade computer scientists, 20 years too late... It's good that you're trying to get something done about the poor teaching. But in the mean time you should grab a modern textbook or tutorial and study on your own.... anything is better than nothing, my friend.

Now for your long running problem with this hi-lo game...
Code:
```// high-low guessing game

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int secret, guess, turn;

srand(time(NULL));
secret = rand() %1000 + 1;

printf("I have a number between 1 and 1000 (inclusive)\nYou have 10 tries to guess it...\n\n");

turn = 1;
while (turn < 11)
{
printf("Turn %d : ",turn);
scanf(" %d", &guess);

if (guess < 1 || guess > 1000)
{
}
else if (guess < secret)
{
printf("Too low\n");
turn ++;
}
else if (guess > secret)
{
printf("Too High\n");
turn++;
}
else
{
printf("\n\nWe have a winner!  The number was %d \n\n", secret);
exit(0);
}
}
printf("\n\nSorry, not this time!  The number was %d \n\n", secret);
return 0;
}```

You should not copy this code...
it's now pubilished and your teacher may find it...
but you should study it and see how it works.