本文介绍了我向我的%f提供的输入没有保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等级1和等级2不保存我给出的数字。所以我不能得到平均值,因为,总是以(0+0)/2的除法结束,有人能帮我吗?

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int students;
    char name[50];
    double gradeOne, secondGrade, classAverage = 0, average = 0;

    for(students = 1;students <= 15; students++){
        printf("Tell me Your name:
");
        fflush(stdin);
        scanf("%[^
]s", &name);
        //saves name
        printf("Tell me your first grade:
");
        scanf("%f", &gradeOne);
        printf("Tell me your second grade:
");
        scanf("%f", &secondGrade);
        //saves grade


        printf("
========================================
");

        printf("Student: %s
", name);
        printf("First Grade: %3.2f
Second Grade: %3.2f
", gradeOne, secondGrade);
        printf("%f", average);

        printf("
========================================
");

        average = (gradeOne + secondGrade)/2;
        //creates average
        printf("Average of the %d student: %3.2f",students, average);

        classAverage += average;
        //creates class average

    }
        classAverage = classAverage / (students - 1);
        printf("The class average was: %3.2f", classAverage);

    return 0;
}

推荐答案

%finscanf()用于阅读float。您应该使用%lf来阅读double

请注意,您应该在printf()中使用%f打印double。较新的规范允许%lf用于printf(),但%f应更好地兼容。

这篇关于我向我的%f提供的输入没有保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:49