简体   繁体   English

简单的 C 编码问题:跳过 Printf 并跳过用户输入

[英]Simple C Coding Problem: Skipping Over Printf and Skipping User Input

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

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. int main() 代码是正确的,output 数字是正确的,但用户没有输入华氏温度。 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.我不希望 output 跳过第 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.您在 function 中使用了return两次,这样它就won't go ahead go 询问华氏度问题。 Whenever return is encountered while compiling it terminates the function there and there only.每当在编译时遇到return时,它只会在那里终止 function。

Now what you can do is that just make another function for fahrenhiet and copy paste the fahrenhit part in it.现在你可以做的就是为华氏温度制作另一个function并将华氏温度部分复制粘贴到其中。

#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;!!希望能帮助到你;!! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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