C Programming

posted by .

How do you reprompt after the user inputs a wrong number into the program in C Programming? Loop structure?

i.e.
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.

And where would I put the loop code in?
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);
}

  • C Programming -

    Hmm, code was too long for the post?

    Here's a second try.

    /* 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);
    }

  • C Programming -

    I doubt that it was too long, considering the programs you and I posted earlier. Maybe indentation?

    You could try something like a do loop around your validation, of the form:
    do }
    {get heading and display error message if necessary)
    } while (heading is not valid)

    e.g.
    /* Directives */

    #include <stdio.h>

    int main(void)
    {

    double heading; /*compass heading in degrees*/

    heading = 0.0;

    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. %lf isn't!\n", heading);
    } while (heading <= 0.0 || heading > 360.0);
    printf("Congratulations! You have entered a valid heading of %lf\n", heading);
    }

    There's lots of improvements you can make from that, but it should give the idea of what to do woth a do loop.

  • C Programming -

    Oops. I got the bracket messed up in the pseudocode and left a <= instead of a < in the while.

  • C Programming -

    Ah! Thanks.

    When I was doing my if and while statements, I kept doing (heading < 0.0 || > 360.0) and kept wondering why it wasn't working. It needs to be while (heading <= 0.0 || heading > 360.0);

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Computer Programming

    Java Programming Question: basically want to keep the user in a loop that displays the menu and requests a user choice again and again until the user enters choice option 0 and exits the program. When Option 1 is chosen, I want to …
  2. C Programming

    Write a program that prompts the user to enter a temperature, followed by a letter representing Celsius (C) or Fahrenheit (F). Then prompt the user for the conversion type and print the results to the screen with one decimal point. …
  3. C programming

    A few questions, all involving "for" loops that I am unsure how to do (Thank you in advance for the help): Q1.) Write a table of conversions from Celsius to Rankin. Allow the user to enter the starting temperature and increment between …
  4. programming

    Using Raptor, create a condition loop that will ask the user for input of two numbers. The numbers should be added and the sum displayed. The loop should also ask the user if he or she wishes to enter two more numbers and perform the …
  5. Programming in c++

    Write a program in c++ that inputs a number [1-12] from the user and displays the month. it also asks the user whether he wants to input another number or not, if the user inputs 1 then it again inputs number, if user inputs 0 then …
  6. programming

    Write a C++ code to count the number of characters, words, and lines entered by the user. The program stops if the user enters two dots.
  7. programming

    Write a program which allows user to enter 10 student names and scores for Java programming 1 and Java programming 2. Allow user to choose which course they want to enter the record for. Use the loop to ask input from user. The program …
  8. Computer Literary

    Which of the following is Not an example of debugging A)Looking for errors in programming code B)Fixing misspelled commands in programming code C)Solving faulty logic in programming code D)Upgrading to improve the programming code
  9. intro to computer programming

    Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount. Your program should use a loop …
  10. java programming

    the learner is asked to write a program using java with user input that caculate and displays, the number of months it will take the user to save an amount of money. the user must enter the

More Similar Questions