我该如何显示成绩

How I would I be able to display the grades?

本文关键字:显示 何显示      更新时间:2023-10-16

我真正需要做的就是显示成绩,但由于某种原因,我脑子里有个屁,我一辈子都想不出来。对于那些必须知道的人来说,整个任务是给我一个带数字的文本文件。每一行将包括四个考试分数,相当于一个学生。我必须计算每个学生的平均成绩并显示他们的成绩。我将如何继续使用我设置的代码?

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

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.nn";
    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;
}

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

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;