checking inputs meets multiple conditions

This is a discussion on checking inputs meets multiple conditions within the C Programming forums, part of the General Programming Boards category; Hi, I'm making a program for a uni assignment where the user inputs an initial velocity and angle and the ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    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){
              printf("please enter a real number for your angle:\nangle = ");
              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. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Try to use getting the angle in a separate function.

    Then do the error checks in the angle in that function and return.
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    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);
    }
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    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. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The magic word is "fmod".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. checking multiple inputs
    By killdragon in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 03:26 PM
  4. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 01:57 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21