Thread: How to reprompt after user inputs wrong number?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    How to reprompt after user inputs wrong number?

    Would I have to do a loop of some sort?

    I want my code to do this:

    Enter a Compass Heading: 500.7
    Please Input a Compass Heading between 0 and 360 Degrees.
    Enter a Compass Heading: 360.0

    If the user puts a number not in 0 - 360, I want the program to reprompt or start over again. How would I do that?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yes, you need a loop.

    Code:
    int input_valid = 0;
    while(!input_valid)
    {
        input = get_input();
        input_valid = input >= 0 && input <= 360;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    or...

    Code:
    while(!(input >= 0 && input <= 360;))
    {
         input = get_input();
    }
    Arduino rocks!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could also interpret anything above 360 as 360, and anything below 0 as 0. Alternatively, interpret for example 500.7 as 140.7 degrees.
    Last edited by Subsonics; 09-30-2009 at 10:14 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Alright, so I tried putting the above codes in and I keep getting errors. Where would I insert the while loop in my code?

    Code:
    /* Directives */
    
    #include <stdio.h>
    
    int main(void)
    {
    
    double heading; /*compass heading in degrees*/
    
    /* Get compass heading */
    printf("Enter a Compass Heading: ");
    scanf("%lf", &heading);
    
    /* Display equivalent compass bearing */
    if (heading < 0.0)
    printf("Please Input a Compass Heading between 0 and 360 Degrees.\n", heading);
    
    else if(heading < 90.0)
    	printf("Compass Bearing: North  %.1f Deg East\n", heading);
    
    else if(heading <= 180.0)
    	printf("Compass Bearing: South %.1f Deg East\n", 180 - heading);
    
    else if(heading <= 270.0)
    	printf("Compass Bearing: South %.1f Deg West\n", heading - 180.0);
    
    else if(heading <360.0)
    	printf("Compass Bearing: North %.1f Deg West\n", 360.0 - heading);
    
    else
    	printf("Please Input a Compass Heading between 0 and 360 Degrees.\n", heading);
    
    /* End Program */
    return(0);
    }

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    do
        angle = get_angle();
    while( angle < 0 || angle >= 360 );
    But I agree with other posters that there is nothing inherently bad about a heading of 500.7. It's the same as a heading of 140.7.

    (Note that heading isn't the same as rotation. A rotation of 500.7 degrees is not the same as a rotation of 140.7 degrees. In the first case the object makes an extra revolution.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by brewbuck View Post
    Code:
    do
        angle = get_angle();
    while( angle < 0 || angle >= 360 );
    But I agree with other posters that there is nothing inherently bad about a heading of 500.7. It's the same as a heading of 140.7.

    (Note that heading isn't the same as rotation. A rotation of 500.7 degrees is not the same as a rotation of 140.7 degrees. In the first case the object makes an extra revolution.)
    Ok, so where would I put that code? And would I need to declare anything for the code?

    I want the program to only take in 0-360 that's why I am asking for help to reprompt the user for 0-360.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    printf("Enter a Compass Heading: ");
    scanf("%lf", &heading);
    Looks to me like that's the code that needs to be in a loop.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What we're saying is to out this whole thing inside a loop. Perhaps you could have a function which validates the current inputed value which returns a bool and use the return value to decide whether to prompt for the values again. Like

    Code:
    printf(" Prompt for a value ");
    scanf( num )
    
    while( inputvalidate( num ) )
    {
           printf( "Prompt again!" );
           scanf( num );
    }
    
    or use do while
    You could also camp the value to min and max if it goes beyond or bellow as its been suggested.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Thanks to everyone that helped me. I finally got it all working. I couldn't have done it without you guys.

  11. #11
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Could you post the whole code, if you want?
    Arduino rocks!

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by yann View Post
    Could you post the whole code, if you want?
    No problem.

    Code:
    /* Directives */
    
    #include <stdio.h>
    
    int main(void)
    {
    
    double heading; /*compass heading in degrees*/
    
    /* Get compass heading */
    
    do
    {
    printf("Enter a Compass Heading: ");
    scanf("%lf", &heading);
    
    /* Display equivalent compass bearing */
    
    if (heading < 0.0 || heading > 360.0)
    printf("Please Input a Compass Heading between 0 and 360 Degrees.\n", heading);
    } while (heading < 0.0 || heading > 360.0);
    
    if(heading <= 90.0)
    	printf("Compass Bearing: North %.1f Deg East\n", heading);
    
    else if(heading < 180.0)
    	printf("Compass Bearing: South %.1f Deg East\n", 180 - heading);
    
    else if(heading <= 270.0)
    	printf("Compass Bearing: South %.1f Deg West\n", heading - 180.0);
    
    else if(heading <360.0)
    	printf("Compass Bearing: North %.1f Deg West\n", 360.0 - heading);
    
    else if(heading == 360.0)
    	printf("Compass Bearing: North %.1f Deg East\n", 360.0 - heading);
    else
    	printf("Please Input a Compass Heading between 0 and 360 Degrees.\n", heading);
    
    
    
    /* End Program */
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. prompt user for number of loops
    By wonderpoop in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 06:58 AM
  3. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM