scanf过早地在循环中终止(我认为)

时间:2017-12-08 02:27:28

标签: c while-loop scanf termination

我正在制作一个简单的程序,根据您当前的分数,课程考试的总价值和您想要的分数来计算所需的考试分数。我没有遇到任何问题,直到我想要一个简单的while循环重做proses如果用户想要这样做。出现的问题是当我让scanf从用户那里得到一个char响应时,它会终止while循环然后终止程序。当我注释掉scanf行并保持代码原样(无限循环)时,它可以正常循环,但是使用它,它将终止而没有错误。有什么想法吗?

    #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;
        }
    }

5 个答案:

答案 0 :(得分:1)

  

出现的问题是,当我让scanf从用户那里得到一个char响应时,它会终止while循环然后终止程序。

这是因为\n正在从输入缓冲区中读取迷路%(换行符)。

要解决此问题,请在scanf()中的scanf(" %c", &responce); 字符之前添加一个空格,如下所示:

name

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

答案 1 :(得分:0)

之前

scanf("%c", &response );

需要清除stdin,可能是:

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

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

答案 2 :(得分:-1)

您面临此问题的原因与前导空格

有关

将您的scanf行更改为:

scanf(" %c", &responce);

这是解释的链接

How to do scanf for single char in C

答案 3 :(得分:-1)

试试这个,按预期工作。

原文:

scanf("%c", &responce);

<强>替换

responce=fgetc(stdin);

答案 4 :(得分:-1)

我个人喜欢在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 );