简体   繁体   中英

Simple C Coding Problem: Skipping Over Printf and Skipping User Input

https://onlinegdb.com/-9jZX-uU0 [link]

I want to have the second line of code to have the user input the Fahrenheit not the Celsius twice. The int main() code is correct and the output number is correct but the user does not input the Fahrenheit. How do I fix this? enter image description here

What is expected is that the user inputs the Fahrenheit and the code continues. I do not want the output to skip over lines 12-14. I don't know how to fix this. I expect that once the user inputs Fahrenheit the code will continue to run as it currently does

#include <stdio.h>
#include <math.h>
double ask_for_air_celcius()

{
double temp_celcius, temp;

printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);

printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}


int main()


{

double temp_celcius, spsound_1,spsound_convert_1,temp, 
spsound,spsound_convert;

{
    temp_celcius = ask_for_air_celcius(); 
    spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297) 
     / 247);
    spsound_convert_1=spsound_1*1.09728;
    
    temp = ask_for_air_celcius(); 
    spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
    spsound_convert=spsound*1.09728;
    
    printf("\nThe speed of sound at %.2f degrees Celcius is %.2f 
    ft/s.\n", temp_celcius, spsound_1);
    printf ("The speed of sound at this temperature in Celciusis 
    equivalent to %.2f km/h.\n\n", spsound_convert_1);
   printf("The speed of sound at %.2f degrees Fahrenheit is %.2f 
   ft/sec.\n", temp, spsound);
   printf ("The speed of sound at this temperature in Fahrenheit 
   is equivalent to %.2f km/h.\n", spsound_convert);
    }
   return(0);
    }

You used used return twice in the function so that it won't go ahead for asking the fahrenhiet question. Whenever return is encountered while compiling it terminates the function there and there only.

Now what you can do is that just make another function for fahrenhiet and copy paste the fahrenhit part in it.

#include <stdio.h>
#include <math.h>
double ask_for_air_celcius(){
    double temp_celcius;
    printf("Enter the current air temperature(in Celcius): ");
    scanf(" %lf", &temp_celcius);
    return(temp_celcius);
}

double ask_for_air_Fahrenhiet(){
    double temp;
    printf("Enter the current air temperature(in Fahrenheit): ");
    scanf(" %lf", &temp);
    return (temp);
}

int main()


{

double temp_celcius, spsound_1,spsound_convert_1,temp, 
spsound,spsound_convert;

{
    temp_celcius = ask_for_air_celcius(); 
    spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297) 
     / 247);
    spsound_convert_1=spsound_1*1.09728;
    
    temp = ask_for_air_Fahrenhiet();//changes here 
    spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
    spsound_convert=spsound*1.09728;
    
    printf("\nThe speed of sound at %.2f degrees Celcius is %.2f ft/s.\n", temp_celcius, spsound_1);
    printf ("The speed of sound at this temperature in Celciusis equivalent to %.2f km/h.\n\n", spsound_convert_1);
    printf("The speed of sound at %.2f degrees Fahrenheit is %.2f ft/sec.\n", temp, spsound);
    printf ("The speed of sound at this temperature in Fahrenheit is equivalent to %.2f km/h.\n", spsound_convert);
}
   return(0);
    }

Hope it helps;!! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM