【问题标题】:Something strange with a simple program, possible scanf problem一个简单的程序有些奇怪,可能是scanf问题
【发布时间】:2010-11-08 21:06:05
【问题描述】:

我有一个简单的作业...制作一个需要 3 个值 3 次的 C 程序

  1. 食物标识(字符)
  2. 食物卡路里值(浮动)
  3. 食物量(浮动)

这是程序给我的代码和输出:

#include <stdio.h>
#define NUM_OF_FOOD 3

int main()
{
    float food_cal[NUM_OF_FOOD];
    float food_amount[NUM_OF_FOOD];
    char food_id[NUM_OF_FOOD];
    int i;
    float total_cal=0;
    int most_fat_food_id=0;
    printf("enter the following data: food ID, Cal value and Amount eaten \nexample A 10 3\n");
    for (i=0;i<NUM_OF_FOOD;i++)
    {
        printf("\nEnter product #%d:",i);
        int inputLength = scanf("%c %f %f",&food_id[i],&food_cal[i],&food_amount[i]);
        if ( inputLength < 3 ) {
            printf("input error, input length was %d excpexted 3", inputLength);
            break;
        }
        if ( !((food_id[i]>96 && food_id[i]<123) || (food_id[i]>64 && food_id[i]<91)) ) {
             printf("ID input error");
             break;
        }
        if ( food_cal[i] < 0 ) {
            printf("Food Cal input error");
            break;
        }
        if ( food_amount[i] < 0 ) {
            printf("Food Amount input error");
            break;
        }
        printf("\n%c %5.2f %5.2f",food_id[i],food_cal[i],food_amount[i]);
    }
    for (i=0;i<NUM_OF_FOOD;i++)
        total_cal+=food_cal[i]*food_amount[i];

    printf ("\nTotal amount of calories is %5.2f\n",total_cal);
    for (i=1;i<NUM_OF_FOOD;i++)
         most_fat_food_id = (food_cal[most_fat_food_id]<food_cal[i]) ? i : most_fat_food_id;

    printf ("\nThe most fattening product is: %c with %5.2f calories",food_id[most_fat_food_id],food_cal[most_fat_food_id]);

    return 0;
}

/*
enter the following data: food ID, Cal value and Amount eaten 
example A 10 3

Enter product #0:A 1000 2

A 1000.00  2.00
Enter product #1:B 500 3
input error, input length was 1 excpexted 3
Total amount of calories is 2000.00

The most fattening product is: A with 1000.00 calories
*/

它只是跳过第三个的输入

不知道为什么……两个人看了代码,看起来不错,但还是有错误。

【问题讨论】:

  • @Roger - 过失 - 但请注意 OP 发布了一个运行示例,完美显示了他的错误。
  • @Kevin:是的,不幸的是,我一直在使用类似表单的消息,并为此提供一般性建议——这些链接对用户 5.. 以后的问题和其他人的问题也很有用.
  • @Roger - 如果你愿意,我会改变 :)
  • @Kevin:(怎么样?;)"The homework tag, like other so-called 'meta' tags, is now discouraged," 但是,@user501160,请继续关注general guidelines,说明任何特殊限制,展示您迄今为止尝试过的内容,并询问具体是什么让您感到困惑。
  • @Roger - 我认为他已经完成了第二部分;)(展示你到目前为止所做的尝试)

标签: c scanf


【解决方案1】:

输入:A 1000 2[ENTER]B 500 3[ENTER]...

第一个 scanf("%c %f %f") 读取“A”、1000 和 2 并在 [ENTER] 处停止

第二个scanf("%c %f %f") 读取 [ENTER] 并与 B 阻塞以进行“%f”转换。

您可以尝试使用scanf(" %c %f %f") 在读取字符之前强制跳过可选空格。

【讨论】:

  • 只需要记住一件重要的事情:“正常”scanf 转换说明符(%d、%i、%f、%s)跳过前导空格(空格、换行符、...); “%c”没有。要强制跳过空格,请在格式字符串中使用空格。
【解决方案2】:

在第 33 行之后添加:

char nl = 0;
scanf("%c", &nl);

这将消耗最后一个换行符,这会把事情搞砸。

在上述建议之后,这是输出:

./test
enter the following data: food ID, Cal value and Amount eaten
example A 10 3

Enter product #0:a 10 2

a 10.00  2.00
Enter product #1:b 3 29

b  3.00 29.00
Enter product #2:c 32 4

c 32.00  4.00
Total amount of calories is 235.00

The most fattening product is: c with 32.00 calories

【讨论】:

    【解决方案3】:

    你不消费'\n'

    快速/脏方法是添加一个变量:

    char readNewLine;
    

    在您的scanf() 之后,添加另一个:

    scanf("%c", &readNewLine);
    

    这很难看,但足够了 - 在您使用 C 一段时间后,会有更好的方法可用。

    【讨论】:

    • 更好的方法已经可用了。为什么不建议 OP 阅读 fgets(),尤其是因为他的输入数据会非常小?
    • 因为作为作业,我不预设他/她的学习水平。如果教授(等人)还没有走到那一步,那就是信息太多了。我几乎为我完全回答了这个问题而感到羞愧(除了只是说'\n'没有被消耗)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多