Program to prompt user to guess for a number and re-ask if guess outside range

This is getting to make-or-break for me. We've been given a list of ten questions for our assignment. I've done three, but the others are breaking my heart. I can't get anywhere with any of them. I'm plugging away but I'm a hair's breath from dropping out of the course because I'm just not capable of this.

This is a program which is supposed to ask for a user to guess a number between 1 and 10, and to keep reasking the user to guess if the guess is outside that range. I've come up with this but to be honest its probably complete BS and would never work if I kept at it till Xmas.

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;
}

Also, is there any online I can search for examples of code using while and if. Perhaps if I could find something it would make more sense to me. I don't want to keep hounding you guys with questions.

10-19-2011

MK27

Quote:

Originally Posted by Interista

I've come up with this but to be honest its probably complete BS and would never work if I kept at it till Xmas.

Am I even remotely close?

You are not too far off; perhaps by Halloween ;)

Code:

while
{
( guess >= 1 && guess <= 10 );

You want the condition outside the block, ie:

Code:

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

I would say, to finish up, you want another while() around the whole thing, so that the user is prompted to guess until they get the answer correct.

Quote:

Originally Posted by Interista

Also, is there any online I can search for examples of code using while and if.

This will loop until you get an acceptable value. From a strictly personal standpoint I like to keep user prompts as short and concise as possible.

The rest of your program looks ok, although it would be more efficient as an if() - else if() chain.

However; for the struggle you seem to be having. You should know that even on something this basic a programmer will spend some time analysing the task and planning out a solution before they ever sit down at the keyboard. The reason is pretty simple: Nobody can solve a problem they don't understand. Conversely, understanding the problem most often suggests a solution... So in future follow the 4 steps in my .sig (in green) and do a little thinking and planning beforehand... you'll find things get a lot easier.

10-19-2011

Interista

Based on your advice, this is what I've come up with. The problem now is that it will ask for a new number if the number is outside the range, but it won't do anything if the number is inside the range.

Code:

#include<stdio.h>
#define ANSWER 4 /*Define constant of ANSWER*/

int main()

int guess;
printf("This is a number between 1 and 10. Enter your guess:"); /*Read guess from the keyboard*/
scanf("%d" ,&guess); /*Read user input*/

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;

10-19-2011

Interista

I tried again using your suggestions common tater, and still can't complete it.

The problem is, I understand the advice to analyse and work out a route around it. But the problem is I haven't a clue how to use while and if together as no-one has ever shown us how to do it. We've been shown to use while, and shown to use if, but separately, never together. Most of the things are like this on the course.

10-19-2011

CommonTater

Trust me... they behave the same way together as they do separately...

a AND(&&) expression is correct only when both parts of it are correct simultaneously. here the expression says that guess must be equal to 1 and simultaneously be equal to 10. which is clearly impossible.
to determine if guess is within the correct range use this concept, if guess has to be in between 1 & 10 , then guess must be greater than or equal to 1. now greater than 1 can mean anything from 1 to infinity. so , guess must simultaneously be less than or equal to 10 . now write the expression.
see this part also;

and as common tatter has rightly said at first use a pen & paper and try to analyse the problem and the way yoyu want to solve it. then write the code asumming that if u were the compiler how would have you thought.
hoping it helps:D:D