我有一个非常简单的程序,只需要一个重量并将其转换为铂金的价值.我是C的新手,所以错误可能在任何地方.但是当我使用scanf时,它会在一开始就要求输入,而不是遵循代码序列:
码:
#includeint 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版本.我不确定那是你所看到的.