【发布时间】:2017-04-10 13:16:32
【问题描述】:
#include <stdio.h>
main(void)
{
struct computer
{
float cost;
int year;
int cpu_speed;
char cpu_type[16];
} model;
printf(“The type of the CPU inside your computer?\n”);
gets(model.cpu_type);
printf(“The speed(MHz) of the CPU?\n”);
scanf(“%d”, &model.cpu_speed);
printf(“The year your computer was made?\n”);
scanf(“%d”, &model.year);
printf(“How much you paid for the computer?\n”);
scanf(“%f”, &model.cost);
printf(“Here are what you entered:\n”);
printf(“Year: %d\n”, model.year);
printf(“Cost: $%6.2f\n”, model.cost);
printf(“CPU type: %s\n”, model.cpu_type);
printf(“CPU speed: %d MHz\n”, model.cpu_speed);
return 0;
}
以上代码来自Teach Yourself C in 24 hours,但运行时显示杂散错误。
书中还显示了一个输出。
在输出中,模型的成本是$1234.56。 1234.56 怎么能适应 %6.2f... 我的意思是 %6.2f 我们只会得到 234.56,对吧?
【问题讨论】:
-
“显示杂散错误” 什么错误? edit 并添加 complete 错误消息。另请参阅minimal reproducible example。
-
您的源代码中并没有真正的智能引号,是吗?
-
获得更好的教科书。任何人都不应该使用
gets()。另外,main(void)应该是int main(void) -
/temp/file.cpp:12:2: 错误:程序中出现了“\342”...等等。