首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >程序运行时学习C scanf函数的问题(编译期间没有错误)C编程绝对初学者指南第8章

程序运行时学习C scanf函数的问题(编译期间没有错误)C编程绝对初学者指南第8章
EN

Stack Overflow用户
提问于 2016-07-31 04:26:48
回答 3查看 709关注 0票数 2

我从书中逐字键入了以下代码:

代码语言:javascript
运行
复制
// 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;
}

编译时出现零错误(使用Code:Blocks,正如书中推荐和使用的那样);然而,一旦我运行程序,在输入请求的第一位用户数据(比萨饼的价格,假设我说它将是03.45)后,程序打印出:

代码语言:javascript
运行
复制
"What is your favorite one-word pizza topping?"
"How many slices of 03.45 pizza can you eat in one sitting?"

感觉就像是跳过了printf "What is your ...“后面的scanf行。并且不仅提示下面的printf行“多少个切片...”,而且插入03.45作为用户应该在前一行中输入的字符数组/字符串值。

我已经尝试了一些脑海中浮现的不同的调整,但对于我的生活,我无法找出我做错了什么。有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-31 04:50:54

问题是我在输入回答时没有使用$。在回答第一个问题“一个披萨在你的区域卖多少钱?输入为$XX.XX”时,我的回答是03.45,而由于scanf为$%f,我应该回答$03.45。从"$%f“中去掉$并保持其余部分不变仍然会得到最终打印函数中所需的结果。

希望这对其他使用这本书的人有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2016-07-31 05:44:02

不检查scanf()的返回值。scanf()返回正确解析和存储的输入数。

在失败的情况下,例如与格式字符串" $%f"的缺失$不匹配,有问题的输入将保留在标准输入中,并将由下一次调用scanf()或任何其他输入函数读取。

您可以使用fgets()读取输入,然后使用sscanf()解析它,直到执行正确的转换或到达文件末尾,从而纠正程序。

请注意,您应该保护格式%s以防止缓冲区溢出:传递要作为scanf(" %23s", topping);读取的最大字符数

下面是一个示例:

代码语言:javascript
运行
复制
#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;
}
票数 1
EN

Stack Overflow用户

发布于 2016-07-31 04:37:41

在将浮点变量作为输入后,您缺少对标准输入执行flush操作。

只需使用下面给出的代码,它将解决您的问题。

代码语言:javascript
运行
复制
#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;
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38678192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档