jeudi 1 octobre 2015

Asking for input twice with if statements in C

My code asks for input twice. I know that's because of the two scanf() calls, but I don't know how I could get rid of one of the calls in order to make it only ask for input once. I need to check to make sure the input is a number and if it is a number then make sure it's between 0 and 90.

printf("Hello! Let's compute the escape velocity of a baseball!\n");
printf("Please supply the angle of travel, in degrees: ");
scanf(" %lf", &angleTravel);
    if (scanf(" %lf", &angleTravel) != 1){
        printf("Please enter a number.");
        return(0);}
    if ((angleTravel > 90) || (angleTravel < 0)){ 
        printf("Please try again with an angle between 0 and 90.\n");
        return(0);}
radianTravel = angleTravel * (M_PI / 180.0); 
printf("The angle you supplied is %lf degrees, or %lf radians\n",       angleTravel, radianTravel);

Aucun commentaire:

Enregistrer un commentaire