Thursday
July 30, 2015

Homework Help: C Programming

Posted by gnozahs on Wednesday, September 30, 2009 at 5:48pm.

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

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Members