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

4 answers

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);
}
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.
Oops. I got the bracket messed up in the pseudocode and left a <= instead of a < in the while.
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);