Thread: checking inputs meets multiple conditions

1. checking inputs meets multiple conditions

Hi, I'm making a program for a uni assignment where the user inputs an initial velocity and angle and the program will calculate a projectiles maximium height and range from these variables assuming it is launched from a 300m cliff.

The problem I am having is ensuring that the inputs are correct, ie that the user inputs an angle between 90 and -90 degrees. The program needs to not let the user input a character and also recognise when an angle is a multiple of 360 above or below an angle within the allowed range, ie 361 is equal to 1 and therefore allowed.

I have tried the following code:

Code:
```while(1){
while(scanf("%f", &theta) != 1){
scanf("%s", &theta);
}
while(theta >= 270){       //Makes angles that are greater than 270 but still within the range on a scale for the program to use
theta = theta - 360;
}
while(theta <= -270){
theta = theta + 360;
}
while(theta>90 || theta<-90){    //tells user to reenter angle if it was not within the specified range
printf("The angle entered was not within the specified range, please enter a correct value:\nangle = ");
scanf("%f", &theta);
}
if(theta >= -90 && theta <= 90) break;
}```
The problem with this code is that if a user is to enter a number not within the range, ie 100 and then a letter, ie a, the code will endlessly loop the error message "The angle entered was not within the specified range, please enter a correct value"

Any help in making these loops work so that any combination of incorrect inputs won't bomb out the program or start more infinte loops of error messages would be greatly appreciated

2. Try to use getting the angle in a separate function.

Then do the error checks in the angle in that function and return.

3. Code:
```int angle(void) {

printf("Enter\n");
scanf ("&#37;d", theta);

if((theta > 90) || (theta < - 90)) {
printf("Error, enter valid angle\n");
angle();
}
etc...
return (theta);
}```

4. For the 360° problem, try using modulus:

Code:
```if (angle > 360)
{
angle = angle % 360;
}```
I assume you do know what modulus does. If 361 is input, it divides by 360 and gets the remainder - 1.

5. Thanks guys, very helpful. I would use modulus but I want to be able to input decimal numbers and hence I didn't use modulus as I thought it only worked with integers, can you make modulus work with floats?

6. The magic word is "fmod".