2016-07-12 2 views
-3

トピックが混乱する可能性があります。これは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) { 

これは私が望む出力です。

これは私の実際の出力 [プログラムの出力]で、[1]

編集:レスポンスのおかげで、IVEは前の材料の文字通り100%を持っていた、としながら、イムので、現在必要なプログラミングクラスを取ります私はC++でそれをすべて持っていたので、まだ構文にはあまりよくなく、講義で議論された関数/ループ/メソッドだけで、もっと努力する必要があるように見えます。

+1

をチェックし、以下のコードあなたは、予想される出力(あなたが「欲しい」出力)を示しているが、実際の* *出力は何ですか?また、予想される(そして実際の)出力を生成する入力を表示してください。 –

+0

また、[なぜループ状態の中のiostream :: eofが間違っていると思われますか?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) 。 –

+0

大丈夫まず最初にお読みになります –

答えて

0

私は問題を正しく理解しているかわかりませんが、if-else-cascadeでグレードを決定し、whileループ後に結果をstd :: coutするたびに、指定したカウンタをインクリメントすることができます。

+0

私はすでに出力ファイルを取得しているので、プログラムの最後に、プログラムの最後に表示するA、B、Cなどの数がいくつあるかを確認したい。 –

+0

OK、 4つの整数変数を作成し、私が言ったようにそれらをカウンターとして使用します。これはかなり簡単なようです... – Wum

+0

それは以下の答えですか? –

0

あなたはかご細工が提案何試みることができるので、コードは次のようになります。

#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()の使用を推奨しない理由をお読みください。

+0

このコードは問題の解決に役立つかもしれませんが、 _why_および/または_how_に関する追加のコンテキストを提供すると、 はその質問に答えて、 長期値を大幅に改善します。あなたの答えを編集して の説明を追加してください。 –

+0

ありがとうたくさんの男、私のためにはうまく働いて、私はそれがどのように動作するか把握します! –

+0

@NeptuneHyper不明な部分はありますか?今すぐ質問してみてください。実際にコードについて特別なものは何もありません。私はWumが特定のグレードを見つけたらすぐにカウンターを増やすという提案を書いたばかりです。まだ処理されていないグレードはないため、各カウンタは最初に0に設定されます。 Rahun Menonのソリューションはよりエレガントに見えますが、私の答えはより直感的で分かりやすく、どちらの方法でも同じ結果が得られます。 – Polb

0

各学年の生徒の数をカウントし、最後に、アレイをプリントアウトするために配列を使用します。[インラインコメントを見る]

を簡単に読みやすくするための配列のインデックスに列挙型を使用します。

マップの代わりにマップを使用することもできます。

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 
+0

このコードは問題を解決するのに役立つかもしれませんが、 _why_および/または_how_に関する追加のコンテキストを提供しています。 はその質問に答えて、 長期値を大幅に改善します。あなたの答えを編集して の説明を追加してください。 –

+0

コードにインラインでコメントがあり、うまくいけばもう少し詳細を追加しました –

+0

ありがとう、このコードは私にとってはうまくいきます、あなたのコードはPolbコードとは異なるので、以下。 –

関連する問題