# How to reprompt after user inputs wrong number?

• 09-30-2009
gnozahs
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:

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?
• 09-30-2009
bithub
Yes, you need a loop.

Code:

int input_valid = 0;
while(!input_valid)
{
input = get_input();
input_valid = input >= 0 && input <= 360;
}

• 09-30-2009
yann
or...

Code:

while(!(input >= 0 && input <= 360;))
{
input = get_input();
}

• 09-30-2009
Subsonics
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.
• 09-30-2009
gnozahs
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)
{

/* Display equivalent compass bearing */

printf("Compass Bearing: North  %.1f Deg East\n", heading);

printf("Compass Bearing: South %.1f Deg East\n", 180 - heading);

printf("Compass Bearing: South %.1f Deg West\n", heading - 180.0);

printf("Compass Bearing: North %.1f Deg West\n", 360.0 - heading);

else

/* End Program */
return(0);
}

• 09-30-2009
brewbuck
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.)
• 09-30-2009
gnozahs
Quote:

Originally Posted by brewbuck
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.
• 09-30-2009
bithub
Code:

Looks to me like that's the code that needs to be in a loop.
• 09-30-2009
ssharish2005
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
• 10-02-2009
gnozahs
Thanks to everyone that helped me. I finally got it all working. I couldn't have done it without you guys.
• 10-03-2009
yann
Could you post the whole code, if you want?
• 10-03-2009
gnozahs
Quote:

Originally Posted by yann
Could you post the whole code, if you want?

No problem.

Code:

/* Directives */

#include <stdio.h>

int main(void)
{

do
{

/* Display equivalent compass bearing */

printf("Compass Bearing: North %.1f Deg East\n", heading);

printf("Compass Bearing: South %.1f Deg East\n", 180 - heading);

printf("Compass Bearing: South %.1f Deg West\n", heading - 180.0);