程序运行时学习C scanf函数的问题(编译期间无错误)C编程绝对初学者指南第8章
我从书中逐字输入了以下代码:程序运行时学习C scanf函数的问题(编译期间无错误)C编程绝对初学者指南第8章,c,printf,scanf,C,Printf,Scanf,我从书中逐字输入了以下代码: // Ex program #2 from Ch 8 of ABGTC // File Ch8Ex2.c // This is a sample program that asks users for some basic data and prints it on screen in order to show what was entered #include <stdio.h> main() { float cost; cha
// Ex program #2 from Ch 8 of ABGTC
// File Ch8Ex2.c
// This is a sample program that asks users for some basic data and prints it on screen in order to show what was entered
#include <stdio.h>
main()
{
float cost;
char topping[24];
int slices;
int month, day, year;
// The first scanf will look for a floating-point variable, the cost of a pizza
// If the user doesn't enter a $ before the cost, it could cause problems
printf("How much does a pizza cost in your area? Enter as $XX.XX\n");
scanf(" $%f", &cost);
// The pizza topping is a string, so your scanf doesn't need an &
printf("What is your favorite one-word pizza topping?\n");
scanf(" %s", topping);
printf("How many slices of %s pizza can you eat in one sitting?", topping);
scanf(" %d", &slices);
printf("What is today\'s date (enter it in XX/XX/XX format).\n");
scanf(" %d/%d/%d", &month, &day, &year);
printf("\n\nWhy not treat yourself to a dinner on %d/%d/%d", month, day, year);
printf("\nand have %d slices of %s pizza!\n", slices, topping);
printf("It will only cost you $%.2f!\n\n\n", cost);
return 0;
}
这感觉就像是跳过了printf“What is your favorite…”后面的scanf行,不仅提示了printf行后面的“多少片…”,还插入了03.45作为用户在前一行中应该输入的字符数组/字符串值
我已经尝试了一些不同的调整,但对于我的生活,我无法找出我做错了什么。有什么想法吗 在将浮点变量作为输入后,您将无法刷新标准输入 只要使用下面给出的代码,它就会解决您的问题
#include <stdio.h>
main()
{
float cost;
char topping[24];
int slices;
int month, day, year;
printf("How much does a pizza cost in your area? Enter as $XX.XX\n");
scanf(" $%f", &cost);
fflush(stdin); //Just add this line here and your problem is solved.
printf("What is your favorite one-word pizza topping?\n");
scanf(" %s", topping);
printf("How many slices of %s pizza can you eat in one sitting?\n", topping);
scanf(" %d", &slices);
printf("What is today\'s date (enter it in XX/XX/XX format).\n");
scanf("%d/%d/%d", &month, &day, &year);
printf("\n\nWhy not treat yourself to a dinner on %d/%d/%d", month, day, year);
printf("\nand have %d slices of %s pizza!\n", slices, topping);
printf("It will only cost you $%.2f!\n\n\n", cost);
return 0;
}
#包括
main()
{
浮动成本;
炭素浇头[24];
int切片;
int月、日、年;
printf(“您所在地区的比萨饼价格是多少?输入$XX.XX\n”);
scanf($%f,&成本);
fflush(stdin);//只要在这里添加这一行,您的问题就解决了。
printf(“你最喜欢的单字比萨配料是什么?\n”);
scanf(“%s”,顶部);
printf(“一次可以吃多少片%s比萨饼?\n”,顶部);
scanf(“%d”,和切片);
printf(“今天的日期是什么(以XX/XX/XX格式输入)。\n”);
scanf(“%d/%d/%d”,&month,&day,&year);
printf(“\n\n为什么不在%d/%d/%d”,月、日、年,请自己吃顿饭?”;
printf(“\n并且有%d片%s比萨饼!\n”,片,顶部);
printf(“只需花费$%.2f!\n\n\n”,成本);
返回0;
}
问题是我在输入回复时没有使用$。当回答第一个问题“你所在地区的比萨饼价格是多少?输入$XX.XX”时,我的回答是03.45,而我本应该回答$03.45,因为scanf是$%f。从“$%f”中删除$并保持其余部分不变,仍会产生最终打印函数所需的结果
希望这能帮助其他人使用这本书 您不检查
scanf()
的返回值scanf()
返回正确解析和存储的输入数
如果出现故障,例如不匹配格式字符串“$%f”
缺少的$
,则有问题的输入将保留在标准输入中,并将在下次调用scanf()
或任何其他输入函数时读取
您可以通过使用fgets()
读取输入,然后使用sscanf()
对其进行解析,直到执行正确的转换或到达文件末尾,从而更正程序
请注意,您应该保护格式%s
以防止缓冲区溢出:将要读取的最大字符数传递为scanf(“%23s”,top)代码>
以下是一个例子:
#include <stdio.h>
int main(void) {
char line[80];
float cost;
char topping[24];
int slices;
int month, day, year;
for (;;) {
printf("How much does a pizza cost in your area? Enter as $XX.XX\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "$%f", &cost) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("What is your favorite one-word pizza topping?\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%23s", topping) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("How many slices of %s pizza can you eat in one sitting?\n", topping);
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%d", &slices) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("What is today\'s date (enter it in XX/XX/XX format).\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3)
break;
printf("invalid input\n");
}
printf("\n\nWhy not treat yourself to a dinner on %d/%d/%d\n",
month, day, year);
printf("and have %d slices of %s pizza!\n", slices, topping);
printf("It will only cost you $%.2f!\n\n\n", cost);
return 0;
}
#包括
内部主(空){
字符行[80];
浮动成本;
炭素浇头[24];
int切片;
int月、日、年;
对于(;;){
printf(“您所在地区的比萨饼价格是多少?输入$XX.XX\n”);
如果(!fgets(线条、线条尺寸、标准尺寸))
返回1;
如果(sscanf(行,$%f,&成本)==1)
打破
printf(“无效输入\n”);
}
对于(;;){
printf(“你最喜欢的单字比萨配料是什么?\n”);
如果(!fgets(线条、线条尺寸、标准尺寸))
返回1;
如果(sscanf(行,“%23s”,顶部)==1)
打破
printf(“无效输入\n”);
}
对于(;;){
printf(“一次可以吃多少片%s比萨饼?\n”,顶部);
如果(!fgets(线条、线条尺寸、标准尺寸))
返回1;
如果(sscanf(行、%d、&slices)==1)
打破
printf(“无效输入\n”);
}
对于(;;){
printf(“今天的日期是什么(以XX/XX/XX格式输入)。\n”);
如果(!fgets(线条、线条尺寸、标准尺寸))
返回1;
如果(sscanf(行“%d/%d/%d”、&月、&日、&年)==3)
打破
printf(“无效输入\n”);
}
printf(“\n\n为什么不在%d/%d/%d上请您自己吃饭呢\n”,
月、日、年);
printf(“并有%d片%s比萨饼!\n”,片,顶部);
printf(“只需花费$%.2f!\n\n\n”,成本);
返回0;
}
您显示的源代码是否真的是正在运行的代码?如果您看到输出,它与代码应该执行的操作(将价格打印为字符串?)不对应。程序的完整输出是什么?程序的实际输入是什么?我展示的源代码实际上就是正在运行的代码。我可以看出这是不对应的。下面是我运行程序时发生的情况:“你所在地区的比萨饼价格是多少?”这是第一个问题,我输入03.45并按enter键。然后,我在屏幕上看到以下内容:“你最喜欢的单字比萨配料是什么?”“你一次能吃多少片03.45比萨?”这是否更详细了一点?你错过了一个主要的“$”。您必须输入以下内容:$03.45
,正如问题所问(输入$XX.XX)^这就是问题的全部……噢,天哪,哈哈哈,谢谢你,阿玛迪斯@GuyWantingBoat:您能接受其中一个答案吗?是的,它可能会工作,但在Posix和C中使用fflush()
刷新输入流仍然是未定义的行为。但是您可以刷新输出流。我不知道为什么所有的书都使用scanf()
作为例子,scanf()
是一个真正的后遗症,即使你知道你在做什么。好吧,这解决了我以前遇到的问题,但现在当程序开始打印比萨饼的成本(最终打印函数)时,它说它将花费0.00美元,因为它应该说明用户输入是什么(例如03.45)。还有,既然我正在学习C,那么fflush(stdin)是什么意思?它做什么?@GuyWantingBoat该程序对我来说运行良好。如果用户输入
#include <stdio.h>
int main(void) {
char line[80];
float cost;
char topping[24];
int slices;
int month, day, year;
for (;;) {
printf("How much does a pizza cost in your area? Enter as $XX.XX\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "$%f", &cost) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("What is your favorite one-word pizza topping?\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%23s", topping) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("How many slices of %s pizza can you eat in one sitting?\n", topping);
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%d", &slices) == 1)
break;
printf("invalid input\n");
}
for (;;) {
printf("What is today\'s date (enter it in XX/XX/XX format).\n");
if (!fgets(line, sizeof line, stdin))
return 1;
if (sscanf(line, "%d/%d/%d", &month, &day, &year) == 3)
break;
printf("invalid input\n");
}
printf("\n\nWhy not treat yourself to a dinner on %d/%d/%d\n",
month, day, year);
printf("and have %d slices of %s pizza!\n", slices, topping);
printf("It will only cost you $%.2f!\n\n\n", cost);
return 0;
}