Scanf语句被忽略

时间:2018-03-29 00:05:40

标签: c

我的程序在if语句之前忽略我的scanf语句,该语句将用户输入关闭程序或继续添加新信息(在main内部的while(1)末尾)。

int main (void)
{
while (1)
    {
        float weight = 0;   
        char animal , b;

        printf("Zoo Food Calculator: \n\n");
        printf("What is the animal type? \n [M]Mammal or [A]Amphibian or [R]Reptile: \n");
        scanf("%c", &animal);

        printf("What is the weight of the animal in pounds?:\n");
        scanf("%f", &weight);

        weight = round(weight);

        printf("weight in pounds: %f\n", weight);

        if (animal == 'M')
        {
             Mammals (weight) ;
             printf("For this animal you'll need %f lbs of food a week!\n", Mammals(weight));
        }
        else if (animal == 'A')
        {
        Amphibians (weight) ;
        printf("For this animal you'll need %f lbs of food a week!\n", 
Amphibians(weight));
        }

    else 
        {
            Reptiles (weight) ;
            printf("For this animal you'll need %f lbs of food a 
week!\n", Reptiles(weight));
        }

        printf("Do you want to input new information? Y/N \n");
        scanf("%c", &b);

        if (b == 'N' || b == 'n')
        {
            break;
        }    
     }
    return 0; 
}

2 个答案:

答案 0 :(得分:0)

  

我不确定为什么我的程序运行两次功能(哺乳动物,两栖动物,爬行动物)。

您正在调用两次函数:

Mammals (weight);  //<-- first time
printf("For this animal you'll need %f lbs of food a week!\n", Mammals(weight));
//                                                             ^
//                                                         second time
  

它也忽略了我的scanf语句,在if语句之前将用户输入关闭

那是因为在此之后换行符仍然在输入缓冲区中 scanf

scanf("%f", &weight);

%f转换浮点数,但换行符(按下 ENTER 时输入)不是 float的一部分,因此它保留在输入缓冲区中。下一个scanf

scanf("%c", &b);

读取输入缓冲区中的换行符,这就是为什么它似乎“它有 被忽略了。你必须“清除”输入缓冲区,你可以使用它 功能:

void clear_stdin(void)
{
    int c;
    while((c = getchar()) != '\n' && c != EOF);
}

并在scanf("%f", &weight);之后调用它,如下所示:

printf("What is the weight of the animal in pounds?:\n");
scanf("%f", &weight);
clear_stdin();

如果您不想使用clear_stdin函数,另一种选择是 是以scanf格式添加一个空格,如下所示:

scanf(" %c", &b);

使用空格scanf将忽略空格,换行符和制表符 输入缓冲区。有关scanf的格式字符串的更多信息, 请看一下documentation of scanf

答案 1 :(得分:0)

以下是更正后的版本:

void flush() //to clear the buffer to remove whitespaces after your input
{
    int n;
    while((n = getchar()) != 10 && n != -1);
}

int main ()
{
    for(;;) //this is a 'better' version of while(1), according to some
    {
        float weight = 0;
        char animal , b;

        printf("Zoo Food Calculator: \n\n");
        printf("What is the animal type? \n [M]Mammal or [A]Amphibian or [R]Reptile: \n");
        scanf("%c", &animal);

        flush();

        printf("What is the weight of the animal in pounds?:\n");
        scanf("%f", &weight);

        flush(); //to remove the Line Feed [Carriage Return]

        weight = (float) round((double) weight);

        printf("weight in pounds: %f\n", weight);

        float food; //we use this as variable for the result of your functions

        if (animal == 'M')
        {
            food = Mammals (weight); //you need to save the return value
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }
        else if (animal == 'A')
        {
            food = Amphibians (weight);
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }

        else
        {
            food = Reptiles (weight);
            printf("For this animal you'll need %f lbs of food a week!\n", food);
        }

        printf("Do you want to input new information? Y/N \n");
        scanf("%c", &b);

        flush();

        if (b == 'N' || b == 'n')
        {
            break;
        }
    }
    return 0;
}

而不是在'food'下保存返回值,然后在printf中使用它,你也可以使用printf你有但删除第一个电话。