scanf 过早终止 while 循环(我认为)

scanf prematurely terminating while loop (I think)

我正在制作一个简单的程序,根据您当前的分数、课程考试总价值的百分比以及您想要的分数来计算所需的考试分数。在我想要一个简单的 while 循环来重做散文(如果用户愿意的话)之前,我一直没有遇到任何问题。出现的问题是,当我使用 scanf 从用户那里获取 char 响应时,它会终止 while 循环,然后终止程序。当我注释掉 scanf 行并按原样保留代码(无限 while 循环)时,它可以正常工作,永远循环,但有了它,它将终止而不会出现错误。有什么想法吗?

    #include <stdio.h>

    #define TRUE 1
    #define FALSE 0

    int main()
    {
        int continueProgram = TRUE;
        float currentMark = 0.0, examPercent = 0.0, desiredMark = 0.0, requiredMark = 0.0;
        char response  = 'y';

        while (continueProgram == TRUE)
        {
            printf("What is your current mark in your class: ");
            scanf("%f", &currentMark);
            printf("How much is you exam worth: ");
            scanf("%f", &examPercent);
            examPercent /= 100.0;
            printf("What is your desiered mark: ");
            scanf("%f", &desiredMark);

            // Simple math to get mark
            requiredMark = (desiredMark - currentMark * examPercent) / examPercent;

            if (requiredMark > 100.0)
            {
                printf("\nI'm sorry but you need above 100%% to get to your required mark\nHere is a list of all the marks you can get:\n\n");

                for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
                {
                    desiredMark = currentMark * examPercent + requiredMark * examPercent;
                    printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
                }
            }
            else
            {
                printf("\nThe mark that you need to get on the exam is: %.2f\nHere is a list of all the marks you can get:\n\n", requiredMark);

                for (requiredMark = 0.0; requiredMark <= 100.0; requiredMark += 5.0)
                {
                    desiredMark = currentMark * examPercent + requiredMark * examPercent;
                    printf("Exam Mark: %.2f, Final Mark: %.2f\n", requiredMark, desiredMark);
                }
            }

            printf("\nDid you want to continue with a new grade (y or n): ");

            // THIS IS WHERE I AM HAVING THE ISSUE
            scanf("%c", &response );

            if (response  == 'y')
            {
                printf("\n\nResetting  terminal...\n\n");
                currentMark = 0.0;
                examPercent = 0.0;
                desiredMark = 0.0;
                requiredMark = 0.0;
            }
            else
                continueProgram = FALSE;
        }
    }

The problem that arose was that when I have the scanf to get a char response from the user, it terminates the while loop and then the program.

发生这种情况是因为 scanf() 正在从输入缓冲区读取杂散的 \n(换行符)。

要解决此问题,请在 scanf() 中的 % 字符前添加一个 space,如下所示:

scanf(" %c", &responce);

这将跳过前导的白色space 字符(包括换行符)并读取用户给出的输入。

您遇到这种情况的原因与前导空格有关

将您的 scanf 行更改为:

scanf(" %c", &responce);

这里有一个link的解释

How to do scanf for single char in C

试一试,效果如您所愿。

原文:

scanf("%c", &responce);

替换:

responce=fgetc(stdin);

我个人喜欢在 Windows

下使用 fflush
fflush(stdin);               //-- clear the input buffer
scanf("%c", &response );
在 Linux 环境下

fseek

fseek(stdin,0,SEEK_END);    //-- prepare the input buffer to allow fresh data from the keyboard
scanf("%c", &response );

在此之前

scanf("%c", &response );

需要清除 stdin,也许通过:

int ch;
while( (ch = getchar()) != EOF && '\n' != ch );

因为之前对 scanf() 的调用在输入流中留下了换行符。