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

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

To reprompt the user after they input a wrong number, you can use a loop structure like a while loop in C programming. Here's an example of how you can modify your code:

```c
#include <stdio.h>

int main(void)
{
double heading; /* compass heading in degrees */

/* Get compass heading */
printf("Enter a Compass Heading: ");
scanf("%lf", &heading);

/* Continue prompting user until a valid heading is entered */
while (heading < 0.0 || heading >= 360.0)
{
printf("Please Input a Compass Heading between 0 and 360 Degrees.\n");
printf("Enter a Compass Heading: ");
scanf("%lf", &heading);
}

/* Display equivalent compass bearing */
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);

/* End Program */
return 0;
}
```

In this modified code, we use a while loop to continuously prompt the user until a valid heading is entered. The condition for the loop is `heading < 0.0 || heading >= 360.0`, which checks if the heading is outside the range of 0 to 360 degrees. If the condition is true, the program will reprompt the user by printing the error message and asking for input again.

The loop code is placed after the initial `scanf` statement and before the conditional checks for the compass bearing. This ensures that the user is repeatedly prompted for a valid heading until one is entered.