如何使之成为可能

How to make it possible

本文关键字:何使之      更新时间:2023-10-16

我的话题可能令人困惑,这是我在C++中的代码,如何在程序结束时显示每个年级的学生人数摘要,任何人都可以提供帮助吗?

#include <iostream>
#include <fstream>
#include <time.h> 
#include <iomanip>
#include <algorithm>    
#include <vector>       
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {

这是我想要的输出,任何人都可以helP吗?

这是我的实际输出[程序输出][1]

编辑:感谢您的回复,因为我目前正在上必修的编程课,虽然我之前有 100% 的材料,但我C++都有了,所以我对语法还不是很好,我们只被告知在讲座中讨论过的函数/循环/方法,看起来我仍然需要付出更多的努力。

我不确定我是否正确理解了这个问题,但是每次在if-else级联中确定等级时,您都可以增加指定的计数器,然后在while循环后std::cout结果。

你可以尝试 Wum 的建议,所以代码可能看起来像这样:

#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
    char date[9];
    ifstream in;
    ofstream out;
    string name;
    char grade;
    double asg1, asg2, test, quiz, exam, coursework, overallScore, numbergrade;
    int numStudentsA = 0;
    int numStudentsB = 0;
    int numStudentsC = 0;
    int numStudentsD = 0;
    int numStudentsF = 0;
    in.open("Student.txt");
    out.open("Result.txt");
    in >> name >> asg1 >> asg2 >> test >> quiz >> exam;
    //to keep reading the data in input file
    while (!in.eof()) {
        coursework = asg1 + asg2 + test + quiz;
        overallScore = coursework + exam;
        if (overallScore >= 70 ) {
            grade = 'A'; 
            numStudentsA++;
        }
        else if (overallScore >= 60) {
            grade = 'B'; 
            numStudentsB++;
        }
        else if (overallScore >= 50) {
            grade = 'C'; 
            numStudentsC++;
        }
        else if (overallScore >= 40) {
            grade = 'D'; 
            numStudentsD++;
        }
        else if (overallScore >= 0) {
            grade =  'F'; 
            numStudentsF++;
        }// grade
        out << left << setw(15) << name ;
        out << left << setw(3) <<coursework ; //coursework
        out << left << setw(3) << exam ; //exam
        out << left << setw(4) << overallScore ; //overall score
        out << grade ;
        out << endl;
        in >> name >> asg1 >> asg2 >> test >> quiz >> exam;
    }
    cout << "Result Summary   Date: " << date << endl;
    cout << "Subeject: Programming Methodology" << endl;
    cout << "Grade" << setw(10) << "Student" << endl;
    cout << "A" << setw(10) << numStudentsA << endl;
    cout << "B" << setw(10) << numStudentsB << endl;
    cout << "C" << setw(10) << numStudentsC << endl;
    cout << "D" << setw(10) << numStudentsD << endl;
    cout << "F" << setw(10) << numStudentsF << endl;
    cout << "Total Student = 10" << endl;
    return 0;
}

我对您的代码应用了一些缩进,尝试使用单一的编码风格。另外,您应该阅读为什么不建议使用eof()

使用数组来计算每个年级的学生人数,并在最后打印出数组。

使用枚举索引到数组中以便于阅读。

建议之一,而不是数组,您可以使用地图。

eof在上面的情况下很好,因为第一次读取是在while循环之外,然后在开始时完成检查。此外,后续读取在 while 结束时,因此程序将在读取后立即循环回来以检查 eof 条件并退出(不处理空缓冲区)。

下面的代码检查while (in>>name>>asg1>>asg2>>test>>quiz>>exam)

#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <vector>
enum { GRADE_A = 0 , GRADE_B = 1, GRADE_C = 2 , GRADE_D = 3 ,GRADE_F = 4}; // use an enum to index into the array for each grade 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {

   char date[9];
    _strdate(date);
    ifstream in;
    ofstream out;
     int grades[5]; // to store the grades count

    string name;
    char grade;
    double asg1,asg2,test,quiz,exam,coursework,overallScore,numbergrade;


    in.open("Student.txt");
    out.open("Result.txt");

     grades[GRADE_A] = 0 ; grades[GRADE_B] = 0 ; grades[GRADE_C] = 0 ; grades[GRADE_D] = 0 ; grades[GRADE_F] = 0 ; // initialize array 
   while (in>>name>>asg1>>asg2>>test>>quiz>>exam)       //to keep reading the data in input file
 {

    coursework = asg1 + asg2 + test + quiz;
    overallScore = coursework + exam;

    if (overallScore >= 70 )
    {grade = 'A'  ;grades[GRADE_A]++;} // increment count for each grade 
    else if (overallScore >= 60)
    {grade = 'B'  ;grades[GRADE_B]++;}
    else if (overallScore >= 50)
    {grade = 'C'  ;grades[GRADE_C]++;}
    else if (overallScore >= 40)
    {grade = 'D' ;grades[GRADE_D]++;}
    else if (overallScore >= 0)
    {grade =  'F'  ;grades[GRADE_F]++;};   // grade

    out<< left << setw(15) << name ;
    out<< left << setw(3) <<coursework ; //coursework
    out<< left << setw(3) << exam ; //exam
    out<< left << setw(4) << overallScore ; //overall score
    out<< grade ;
    out<< endl;

}
cout<<"Result Summary   Date: " << date << endl;
cout<<"Subeject: Programming Methodology"<<endl;
cout<< "Grade"<< setw(10) << "Student" <<endl;
cout<< "A" <<setw(10)<<grades[GRADE_A]<<endl; // output grade count
cout<< "B" <<setw(10)<<grades[GRADE_B]<<endl;
cout<< "C" <<setw(10)<<grades[GRADE_C]<<endl;
cout<< "D" <<setw(10)<<grades[GRADE_D]<<endl;
cout<< "F" <<setw(10)<<grades[GRADE_F]<<endl;
cout<<setw(0)<<endl;
cout<<"Total Student = 10"<<endl;
//At the end of the program, display the summary of number of student count for each Grade