我怎么能够显示成绩?

时间:2014-10-05 03:21:56

标签: c++

我真正需要做的就是显示成绩,但出于某种原因,我有一个大脑放屁,我不能为生活弄明白。对于那些必须知道的人,整个任务是给我一个带数字的文本文件。每行包含四个相当于一个学生的考试成绩。我必须计算每个学生的平均值并显示他们的成绩。我将如何继续使用我设置的代码?

以下是文本文件中出现的数字:

44 55 77 88
79 88 100 99
77 99 98 99
100 88 89 100
55 56 40 77
100 100 99 95
88 84 87 88
96 97 99 100
30 44 77 55
79 77 88 0
54 52 60 77
88 77 88 77
44 77 10 95

这是代码

// This program calculates a student's average test score, displays
// their grade and then reads the file back to the user.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    ifstream testscores; // Creating an object to output file.
    testscores.open("grades.txt"); // Opening the file.

    char grade; // A student's grade.
    int score1, score2, score3, score4; // Four test scores.
    double average; // The average test score for each student.

    cout << "These are the scores and grades for each student.\n\n";
    cout << "Averages           Grade";
    cout <<"\n--------           -----\n";

    if (!testscores) // Checking for errors otherwise.
        cerr << "Error opening file.\n";
    else
    {
        /*A loop that reads each number until it reaches the end of the file */
        while(testscores >> score1 >> score2 >> score3 >> score4)
        {
            // Calculate the average.
            average = (score1 + score2 +score3 +score4) / 4.0;
            cout << setw(2) << average << endl; // Display the average.
        }
        testscores.close(); // closing the file.
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您似乎只需要显示每个学生的成绩。为此,您可以在显示每个学生的if...else后立即使用average

if(average>=90)
grade='A';
if(average<90 && average >=80 )
grade='B';
if(average<80 && average >=70)
grade='C';
// and so on...then print:
cout<<"Grade="<<grade<<endl;