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

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

After much effort, I've gotten to here. This tells a user to re-guess if outside range, but does not react to a number within the correct range, nor the correct number.

I know its probably full of things that could be tidier, but we're having severe problems with our course. Most of the students are feeling like we're sinking and we're trying to work out ways to rectify.

Please could someone help, in the simplest terms possible because my brain is toast at this stage, how to finally finish this off?

Code:
```#include <stdio.h> #include <stdlib.h>
#include <math.h>
#include <conio.h>
int main(void)
{
int guess;

printf("Enter your guess. Number is between 1 and 10:");
scanf("%d", &guess);

while( guess >= 1 || guess <= 10)
{

if(guess > 10)
{
printf("Too high, Guess again:");
scanf("%d", &guess);
}

if(guess < 1)
{
printf("Too low, guess again:");
scanf("%d", &guess);
}

}

while( guess <= 1 || guess >= 10)

{

if ( guess == ANSWER ) /*If clause to define if user's guess is correct*/
{
printf("You guessed correctly, the answer was 4."); /*If guess is correct this message will show*/
}

if (guess < ANSWER) /*If clause to define if user's guess is smaller than ANSWER*/
{
printf("You guessed incorrectly, the answer is greater."); /*If guess is smaller this message will show.*/
}

if (guess > ANSWER) /*If clause to to define if user's guess is greater than ANSWER*/
{
printf("You guessed incorrectly, the answer is smaller."); /*If guess is smaller then this message will show */
}

}
return 0;
}```

2. Please indent your code properly. It will make the logic much easier to follow.

3. Originally Posted by laserlight
Please indent your code properly. It will make the logic much easier to follow.
Logic? Seriously? Illogical I think. A computer saying I won't do what you want cos you didn't ask in EXACTLY the right way?

4. Let's examine this loop:
Code:
```int guess;

printf("Enter your guess. Number is between 1 and 10:");
scanf("%d", &guess);

while (guess >= 1 || guess <= 10)
{
if (guess > 10)
{
printf("Too high, Guess again:");
scanf("%d", &guess);
}

if (guess < 1)
{
printf("Too low, guess again:");
scanf("%d", &guess);
}
}```
Suppose the user enters 5. 5 >= 1, so control enters the loop body. But wait, you don't want that to happen! So change the loop condition to terminate when the user enters input within the range.

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

6. 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.
A while loop keeps looping while its condition evaluates to true.

EDIT:
Originally Posted by Interista
Logic? Seriously? Illogical I think. A computer saying I won't do what you want cos you didn't ask in EXACTLY the right way?
Your C compiler does not care about indentation. I care about indentation, because "it will make the logic much easier to follow". I am obviously a far better C programmer than you, so perhaps you should care about indentation too

7. Originally Posted by laserlight
A while loop keeps looping while its condition evaluates to true.

EDIT:

Your C compiler does not care about indentation. I care about indentation, because "it will make the logic much easier to follow". I am obviously a far better C programmer than you, so perhaps you should care about indentation too
Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?

8. Originally Posted by Interista
Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?
That is NOT arrogant; that is reality laserlight is one of best, if not the best, programmer that posts on this site.

Tim S.

9. Originally Posted by Interista
Why so arrogant? I'm pretty sure I speak better Italian than you. Do I get a medal?
I gave you good advice with a reason for it, but you called it "illogical", thereby stating that I was being illogical. Arrogant, no?

Now I point out that this advice carries the weight of my experience with it, you call me arrogant. If you don't want to be helped, then I won't help you. Carry on complaining about your teachers, for all the good it will do you.

Originally Posted by Interista
I'm pretty sure I speak better Italian than you. Do I get a medal?
I am not learning Italian; you are learning C programming.

10. Look, perhaps I misinterpreted what you meant. I took it as a slap-down that I hadn't indented. I'm putting hours into this ******* course and I'm pretty much sick of it. If you meant it as a slap-down then I stand by what I said, if not, and it seems you meant it genuinely, then I apologise.

Don't belittle Italian though, I think you'll find its pretty useful.

11. indentation is a very basic thing about programming . it helps every programmer who is reading the program afterwards to understand the logic better . even after few days when you will see your program you will find it easier to understand if you use indentation. that is basically what laserlight had to say.

12. Originally Posted by Interista
I was attacking programming rather than Laserlight. I had no quip with Laserlight.
Fair enough.

Originally Posted by Interista
I do have a quip with putting hours into an assignment and getting nowhere with it because a computer refuses to accept anything I write.
You could read the thread on a development process. The main point: make a change, compile and test. Make another change, compile and test. Don't make many changes at one go, because it will be harder to spot what change introduced an error (not just a compile error, which can be easy to fix with line numbers in the compile error: it could be a logic error too).

Originally Posted by Interista
People are logical, computers illogical.
If computers are illogical, it is because people designed, manufactured and/or programmed them that way

Originally Posted by Interista
And when an "expert" kicks off on you because you made a small error, when you've already said you're completely confused, its hard to take that you've worked hours at something and someone tells you what you've done is a load of ****.
Who said that what you've done is "a load of ****"?

Originally Posted by Interista
And there's no shame in being able to speak a language pally, I'd say more people understand Italian than this code stuff.
Who said it was shameful to be able to speak a language?

13. Thanks all. Look I'm not at my best at the moment. I'm probably searching for an outlet for my frustration over the course and I took it out on the people trying to help me.

Sorry.

OT Ironically enough an Italian salesman just called to my house looking to sell me broadband, had a good conversation about football lol.

14. And I fixed the problem with five ifs, so maybe my tantrum paid off cos I started thinking of other things than the program.