一个简单的程序有些奇怪,可能是 scanf 问题

发布于 2024-10-01 09:32:27 字数 2075 浏览 1 评论 0原文

我有一个简单的作业...制作一个 C 程序,它需要 3 个值 3 乘以

  1. 食物的 id(字符)
  2. 食物的 cal 值(浮点)
  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
*/

它只是跳过了第三个的输入

不知道为什么......两个人查看了代码,看起来不错但仍然有错误。

I have a simple homework... to make a C program that takes 3 values 3 times

  1. id of a food (char)
  2. cal value of food (float)
  3. amount of food (float)

Here is the code and the output the program gives me:

#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
*/

it just skips the input of the 3rd

No idea why... two people looked at the code, looked fine but still have an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-10-08 09:32:27

输入: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") 在读取字符之前强制跳过可选空格。

Input: A 1000 2[ENTER]B 500 3[ENTER]...

The first scanf("%c %f %f") reads 'A', 1000, and 2 and stops at the [ENTER]

The second scanf("%c %f %f") reads the [ENTER] and chokes with the B for the "%f" conversion.

You can try using scanf(" %c %f %f") to force a skip of optional whitespace before reading the char.

贪恋 2024-10-08 09:32:27

在第 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

after line 33 add:

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

that will consume the final newline character that's screwing things up.

after the said suggestion, this is the output:

./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
素食主义者 2024-10-08 09:32:27

您不使用“\n”,

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

char readNewLine;

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

scanf("%c", &readNewLine);

这很丑陋,但它就足够了 - 更好的方法将成为使用 C 一段时间后即可使用。

You don't consume the '\n'

the quick/dirty method is to add a variable:

char readNewLine;

after your scanf(), add another:

scanf("%c", &readNewLine);

This is ugly, but it will suffice - better methods will become available after you've used C for a while.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文