scanf()只读取第一个输入(数字)

时间:2016-04-23 01:34:59

标签: c scanf

我无法解释它,只是scanf()只读取第一个值,然后计算基于此。

int main() {
    int i, students = 0;
    char name[20];
    int tests;
    float test_score;
    int test_sum = 0;
    char letter_grade;
    double test_average;

    printf("Number of students: ");
    scanf("%d", &students);

    for (i = 0; i < students; i++) {
        printf("\nStudent name %d: ", i + 1);
        scanf(" %s", &name);
        fflush(stdin);

        printf("Number of test(s) for %s: ", name);
        scanf("%d", &tests);
        fflush(stdin);

        printf("Enter %d test score(s) for %s: ", tests, name);
        if (i < students) {
            scanf("%f", &test_score);
            test_sum += test_score;
            test_average = test_sum / (float)tests;
        }
        printf("Average test score: %.2f", test_average);

        fflush(stdin);

    }
    return 0;
}

假设我输入2名学生,第一名学生有2个考试成绩,然后输入45 87.我应该得到66.00,但我得到22.50。对于第二个学生,我输入3个测试分数100 55 87,我得到48.33。 Waaayyy关闭。

我知道我做错了什么,但我无法弄明白,因为我之前已经开始工作了,但循环不会继续给第二个学生。

3 个答案:

答案 0 :(得分:1)

您始终需要检查scanf()的返回值,以查看它读取的令牌数量。如果无法阅读,则需要采取纠正措施。

不清楚为什么每次都需要fflush(stdin)

答案 1 :(得分:1)

if (i < students) {
     scanf("%f", &test_score);
     test_sum += test_score;
     test_average = test_sum / (float)tests;
}

应该是:

test_sum = 0;
for (int j = 0; j < tests; j++) {
    scanf("%f", &test_score);
    test_sum += test_score;
}
test_average = test_sum / (float)tests;

答案 2 :(得分:1)

发布的代码包含几个问题,包括

  1. 只能输入一个考试成绩
  2. intfloat以及double变量
  3. 的随机混合
  4. 调用scanf()
  5. 的格式字符串格式不正确
  6. 许多未使用的变量
  7. 未能检查对scanf()
  8. 的来电错误
  9. 差的变量命名。变量名称应指明内容或用法(或两者都更好)
  10. 调用fflush(stdin)在C标准中特别列为未定义的行为
  11. test_sum未在学生之间重新初始化
  12. 以下提议的代码修复了上述所有问题并完全编译

    #include <stdio.h>
    #include <stdlib.h>  // exit(),  EXIT_FAILURE
    
    // prototypes
    void flushStdin( void );
    
    int main( void )
    {
        int  numStudents = 0;
        char studentName[20];
        int  numTests;
    
        double test_score;
        double test_sum = 0.0;
        //char   letter_grade;
        double test_average;
    
        printf("Number of students: ");
        if( 1 != scanf("%d", &numStudents) )
        { // then scanf failed
            perror( "scanf for number of students failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, scanf successful
    
        flushStdin();
    
        for (int i = 0; i < numStudents; i++)
        {
            printf("\nStudent name %d: ", i + 1);
            if( 1 != scanf(" %s", studentName) )
            { // then scanf failed
                perror( "scanf for student name failed" );
                exit( EXIT_FAILURE );
            }
    
            // implied else, scanf successful
    
            flushStdin();
    
            printf("Number of test(s) for %s: ", studentName);
            if( 1 != scanf("%d", &numTests) )
            { // scanf failed
                perror( "scanf for number of tests failed" );
                exit( EXIT_FAILURE );
            }
    
            // implied else, scanf successful
    
            test_sum = 0.0;
            printf("Enter %d test score(s) for %s: ", numTests, studentName);
            for( int j=0; j<numTests; j++ )
            {
                if( 1 != scanf("%lf", &test_score) )
                { // then scanf failed
                    perror( "scanf for test score failed");
                    exit( EXIT_FAILURE );
                }
    
                // implied else, scanf successful
    
                flushStdin();
    
                test_sum += test_score;
            }
    
            test_average = test_sum / numTests;
            printf("Average test score: %.2lf", test_average);
        }
        return 0;
    } // end function: main
    
    
    void flushStdin()
    {
        int ch;
        while( (ch = getchar() ) != EOF && '\n' != ch);
    }
    
相关问题