各学年の生徒の数をカウントし、最後に、アレイをプリントアウトするために配列を使用します。[インラインコメントを見る]
を簡単に読みやすくするための配列のインデックスに列挙型を使用します。
マップの代わりにマップを使用することもできます。
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
をチェックし、以下のコードあなたは、予想される出力(あなたが「欲しい」出力)を示しているが、実際の* *出力は何ですか?また、予想される(そして実際の)出力を生成する入力を表示してください。 –
また、[なぜループ状態の中のiostream :: eofが間違っていると思われますか?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) 。 –
大丈夫まず最初にお読みになります –