我该如何显示成绩
How I would I be able to display the grades?
我真正需要做的就是显示成绩,但由于某种原因,我脑子里有个屁,我一辈子都想不出来。对于那些必须知道的人来说,整个任务是给我一个带数字的文本文件。每一行将包括四个考试分数,相当于一个学生。我必须计算每个学生的平均成绩并显示他们的成绩。我将如何继续使用我设置的代码?
以下是出现在文本文件中的数字:
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;
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 当数据比C 中的数组更多时显示如何显示
- 显示未显示QT其他类的文本框
- 链表C++,输出未显示在显示功能中
- Windows窗体显示与显示对话框