【问题标题】:Scanf skips variable and reuses old oneScanf 跳过变量并重用旧变量
【发布时间】:2020-08-18 09:18:07
【问题描述】:

我正在阅读 C 编程绝对初学者指南第 3 版来学习编程,我在 Chapter8ex2.c 遇到了问题,我的程序在哪里获得成本,然后应该获得最高。相反,它会绕过它并重用第一个变量,而不是使用正确的第二个变量。

这是我的代码

// This is a sample program that asks users for some basic data and prints it on 
// the screen in order to show what was entered
#include <stdio.h>

int main()
{
    char topping[24];
    int slices;
    int month, day, year;
    float cost;

    // The first scanf will look for a floating-point variable, the cost of the 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?");
    printf("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", topping);
    printf("can you eat in one sitting?\n");
    scanf("%i", &slices);

    printf("What is today's date (enter it in XX/XX/XX format).\n");
    scanf(" %i/%i/%i", &month, &day, &year);

    printf("\n\nWhy not treat yourself to dinner on %i/%i/%i", month, day, year);
    printf("\nand have %i slices of %s pizza!\n", slices, topping);
    printf("It will only cost you $%.2f!\n\n\n", cost);

    return(0); 
}

这是正在运行的代码

How much does a pizza cost in your area?enter as $XX.XX)
15.00
What is your favorite one-word pizza topping?
How many slices of 15.00 pizzacan you eat in one sitting?

【问题讨论】:

  • “绝对初学者”和“scanf”不能一起使用。在您充分理解该语言并知道为什么不需要它之前,请不要触摸 scanf。
  • @WilliamPursell scanf 没有问题,这对整个 C 来说也不会有问题。
  • @user3121023 成功了!感谢您提供了惊人的有用评论,您能否将其发布为答案,我会这样标记它!
  • @MintCollie 删除了什么?什么“骚扰”? (一年后?)
  • @MintCollie 如果有人骚扰你,可以解决;没有必要删除问题。 (这个问题没有什么特别的问题。)

标签: c scanf


【解决方案1】:

scanf 存在一些问题。考虑对所有输入使用fgets,并根据需要进行解析。
scanf 失败时,有问题的输入会留在输入流中,并可能导致后续扫描失败或出现意外行为。 fgets 将从输入流中读取并删除最大缓冲区大小。
解析输入可以通过许多函数来完成。 sscanfstrcspn 在这里使用,但还有许多其他可用。 strspnstrtodstrtolstrchrstrstrstrpbrkstrtokstrsep 等等。
避免使用%i,尤其是日期。输入0809 表示日或月将导致问题。前导零告诉 %i 处理八进制(以 8 为基数)值,但 8 和 9 不是有效的八进制数字。
do{}while(); 允许重复输入直到可接受的结果。解析 这里有限制,但可以改进。

#include <stdio.h>
#include <string.h>

int main()
{
    char line[100] = "";
    char topping[100] = "";
    int result = 0;
    int slices = 0;
    int month = 0;
    int day = 0;
    int year = 0;
    float cost;

    do {
        printf ( "How much does a pizza cost in your area?");
        printf ( "enter as $XX.XX)\n");
        if ( ! fgets ( line, sizeof line, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            return 1;
        }
        result = sscanf ( line, " $%f", &cost);
        if ( 0 == result) {
            printf ( "be sure to use a $\n");
        }
    } while ( 1 != result);

    printf ( "What are your favorite pizza toppings?\n");
    if ( ! fgets ( topping, sizeof topping, stdin)) {
        fprintf ( stderr, "fgets EOF\n");
        return 1;
    }
    topping[strcspn ( topping, "\n")] = 0;//remove newline

    do {
        printf ( "How many slices of %s pizza", topping);
        printf ( " can you eat in one sitting?\n");
        if ( ! fgets ( line, sizeof line, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            return 1;
        }
        result = sscanf ( line, "%d", &slices);
        if ( 0 == result) {
            printf ( "enter a number\n");
        }
    } while ( 1 != result);

    do {
        printf ( "What is today's date (enter it in XX/XX/XX format).\n");
        if ( ! fgets ( line, sizeof line, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            return 1;
        }
        result = sscanf ( line, "%d /%d /%d", &month, &day, &year);
        if ( 3 != result) {
            printf ( "be sure to use a / between numbers\n");
        }
    } while ( 3 != result);

    printf ( "\n\nWhy not treat yourself to dinner on %i/%i/%i", month, day, year);
    printf ( "\nand have %i slices of %s pizza!\n", slices, topping);
    printf ( "It will only cost you $%.2f!\n\n\n", cost);

    return(0);
}

【讨论】:

    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2013-12-08
    • 2011-02-02
    • 2015-07-05
    • 2014-10-04
    相关资源
    最近更新 更多