将用户输入存储在变量中

时间:2010-08-07 05:13:42

标签: c stdio

我想知道如何提示我的程序的最终用户输入他们希望从华氏温度转换为C摄氏度的值。

基本上,因为我总共n00b而且我正在编写令人惊叹的“程序”,比如这个:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
     double celsius, fahrenheit, result;

     celsius = result;
     fahrenheit = 27;
     result = (fahrenheit - 32) / 1.8;

     printf("27 degress Fahrenheit is %g degrees Celsius!", result);

     return 0;
    }

如果您知道我的意思,我想为它添加一些实际的“功能”。而不仅仅是将它作为一个测试程序,它实际上只展示了一些简单的算术表达式评估,我想实际上使它有点温和有用。

无论如何,我想知道我是否可以使用scanf(3) Man page中列出的功能来帮助识别用户输入的数据,然后以某种方式将其存储到华氏变量中。

现在,如果程序在运行时可以提示最终用户询问他或她是想将摄氏温度转换为华氏温度还是从华氏温度转换为摄氏温度,那真的很酷,但是让我们来看看吧一步一步,我会等到我读完书中关于“做决定”的章节! :)

更新

删除kiamlaluno所指出的无用变量结果:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
 double fahrenheit, celsius;

 fahrenheit = 27;
 celsius = (fahrenheit - 32) / 1.8;

 printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);

 return 0;
    }

更新更新:

我一直在尝试将每个人在这里发布的有用建议纳入其中,但我的代码遇到了更多问题:

//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit

int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;

printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
    printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
    scanf("%i", &fahrenheit);
    printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);

}
else {
    printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
    scanf("%i", &celsius);
    printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}

return 0;
}

一切都很好,除了计算本身,完全出错了...... 有一秒钟,我认为这可能是因为我将数字本身改为整数类型,但我再次将它们翻了一遍,但它仍然有些麻烦。

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

要使用scanf()读取double,您需要使用正确的格式字符串。对于“long float”(%lf单独读入浮点数)将为%f。然后你可以直接读到双。将其打印出去也是如此。

int main (int argc, char *argv[])
{
    double fahrenheit, celsius;

    printf("farenheit? ");     /* write a prompt */
    scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */

    celsius = (fahrenheit - 32) / 1.8;

    printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);

    return 0;
}

请注意,这根本不处理非数字输入。您需要使用其他输入技术。

[编辑]

你肯定会在你的代码中跳跃,这似乎是你取得了很大的进步。 :)

要解决您当前的最新更新,有几个问题。您的代码实际上并没有计算任何内容,您过早地应用了公式。您应该等到fahrenheitcelsius具有有意义的值(即,在用户输入要转换的值之后)。将这些公式移动到执行转换的函数中是个好主意。您还应该坚持使用double而不是整数。您将无法使用整数获得所需的精度。

double convert_fahrenheit_to_celsius(double fahrenheit)
{
    return (fahrenheit - 32)*(5/9);
}

double convert_celsius_to_fahrenheit(double celsius)
{
    return (celsius*(9/5)) + 32;
}

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, celsiusResult, fahrenheitResult;
    int prompt;

    printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
    scanf("%i", &prompt);
    if(prompt == 1) {
        printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
        scanf("%lf", &fahrenheit);

        /* now convert user-input fahrenheit to celsius */
        celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);

        printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
    }
    else {
        printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
        scanf("%lf", &celsius);

        /* now convert user-input celsius to fahrenheit */
        fahrenheitResult = convert_celsius_to_fahrenheit(celsius);

        printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
    }

    return 0;
}

答案 1 :(得分:1)

好的,如果你想提示用户输入,只需使用带有标签%lf:

的scanf
//Simple program to convert Fahrenheit to Celsius

int main (int argc, char *argv[])
{
    double fahrenheit, result;

    printf("Please enter a number for farenheit: ");
    scanf("%lf", &fahrenheit);
    result = (fahrenheit - 32) / 1.8;

    printf("27 degress Fahrenheit is %g degrees Celsius!", result);

    return 0;
}

至于提示用户他/她是否想要celcius或farenheit,你需要一些叫做if和statement以及else语句。

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, result;
    int ForC;

    printf("1 for Celsius or 0 for Fahrenheit plz: ");
    scanf("%d", &ForC);
    if(ForC == 1) {
        scanf("%lf", &fahrenheit);
        result = (fahrenheit - 32) / 1.8;
        printf("fahrenheit to celsius:%lf", result);
    }
    else {
        scanf("%lf", &celsius);
        result = (celsius*9.0)/5.0 + 32;
        printf("celsius to farenheit:%lf", result);
    }

    return 0;
}

答案 2 :(得分:0)

可以通过检查scanf()的返回值来处理可能错误的用户输入。它必须等于预期值的数量(在这种情况下为1)。在其他情况下,应重复输入。

答案 3 :(得分:0)

对于您的更新更新:尝试从用户获取值后进行计算。如果不是,则celsius, fahrenheit, celsiusResult, fahrenheitResult的结果值未知。

相关问题