我有一个非常简单的程序,可以简单地要求重量并将其转换为铂金的价值。我是C语言的新手,所以错误可能在任何地方。但是当我使用scanf时,它从一开始就要求输入,而不是遵循代码序列:
代码:
#include <stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n");
printf("Please enter your weight in pounds: ");
scanf("%f", &weight);
printf("%.2f\n", weight);
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");
return 0;
}
输出:
123
Are you worth your weight in platinum?
Let's check it out.
Please enter your weight in pounds: 123.00
Your weight in platinum is worth $3049368.00.
You are easily worth that! If platinum prices drop,
eat more to maintain your value.
如果您在输出中注意到,用户必须在打印第一行之前输入输入。为什么是这样?
我尝试了您的程序,它按您的需要运行。您可以stdout
在扫描输入之前尝试刷新缓冲区。
fflush(stdout); // right before your scanf line.
操作系统可以自由地推迟写输出缓冲区以提高效率。因此,例如,您以块而不是一次一个字符的形式写入磁盘。fflush强制写入缓冲区。它是C ++中的“ endl”,而fflush是直接的c版本。我不确定这就是您所看到的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句