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

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    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.

    Am I even remotely close?

    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*/
    
    
    while 
    	{
    	( guess >= 1 && guess <= 10 );
          	printf("Please enter another guess within the correct range:\n");
    	scanf("%d", &guess);
    
    
    
    
    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;
    }
    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Interista View Post
    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 View Post
    Also, is there any online I can search for examples of code using while and if.
    http://publications.gbdirect.co.uk/c...w_control.html
    Last edited by MK27; 10-19-2011 at 07:46 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok.. you're not all that far off, but a few things do need fixing...

    Line 1 ... you cannot put multiple #include or #<anything> statements on one line, the preprocessor won't understand. So this should be two lines.

    Lines 7 through 18 will work (sort of) but you could streamline your inputs quite a bit like this...
    Code:
    int guess;
    
    do
      { 
        printf("Enter a guess between 1 and 10 : ");
        scanf("%d",&guess);
        if (guess < 1 || guess >10)
          printf("Out of range\n");
        }
    while (guess < 1 || guess > 10);
    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.
    Last edited by CommonTater; 10-19-2011 at 07:54 AM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    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*/
    
    
    while 
        ( guess >= 1 && guess <= 10 ); /*While loop to determine if guess is within correct range*/
        {
        printf("Please enter another guess within the correct range:\n"); /*Prompts for a new guess*/
        scanf("%d", &guess);
    }
    
    
    while 
        ( guess == 1 && guess == 10 ); /*While loop to determine if guess is within correct range*/
        {
        
    
    
    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;

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    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.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Trust me... they behave the same way together as they do separately...

    Think about it in little tiny blobs the way a computer does...
    Code:
    int x = 0;
    
    while( x < 10 )           // ok x < 10 lets do this
      {                           // totally forgets all about while()
    
         if (x == 3)            // nope x isn't 3 don't do this
           puts("Hello");      // or x is 3 and do it
                                  
                                  // totally fogets about if()
         x++;                   // add 1 to x;
    
                                  // totally forgets about x++   
       }                         // ok lets start over...
    Really... it's that immediate. To understand it, you have to think like it...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Interista View Post
    We've been shown to use while, and shown to use if, but separately, never together.
    Using them "separately" is not really distinguishable from using them "together".

    While() and if() both use a condition. The possible forms of that condition are identical; the difference is that while() creates a loop whereas if() does not.

    Perhaps by "together" you mean nested inside one another?

    Code:
    #include <stdio.h>
    
    int main(void) {
    	int i = 0, j;
    
    	while (i < 10) {
    		if (!(i%3)) {
    			printf("i = %d\n", i);
    			j = 0;
    			while (j < 10) {
    				if (!(j%3)) printf("\tj = %d\n", j);
    				j++;
    			}
    		}
    		i++;
    	}
    
    	return 0;
    }
    Try compiling and running that. If you do not understand something, ask. Notice that correct indenting is important to clear coding especially when nesting blocks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Code:
    while 
        ( guess == 1 && guess == 10 ); /*While loop to determine if guess is within correct range*/
    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;
    Code:
    while      ( guess >= 1 && guess <= 10 ); /*While loop to determine if guess is within correct range*/     {     printf("Please enter another guess within the correct range:
    "); /*Prompts for a new guess*/     scanf("%d", &guess); }
    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
    Last edited by joybanerjee39; 10-19-2011 at 08:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  2. Guess the number program help needed
    By msbrownsugah in forum C Programming
    Replies: 7
    Last Post: 05-06-2009, 05:58 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM
  5. can u guess my number?
    By strider496 in forum C Programming
    Replies: 16
    Last Post: 03-22-2005, 10:19 PM