-3

My topic may be confusing, this is my code in C++,how can i display the summary of number of student count for each Grade at the end of the program,can any body help in this?

#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) {

This is the output i want,can anybody helP?

This is my actual output [Output of the program][1]

edit: thanks for the responses, since im currently taking a required programming class, and while ive had literally 100% of the material before, I had it all in C++, so im not very good with the syntax yet, and we are told only with functions/loops/methods that have been discussed in lecture , looks like i still need to put more effort into it.

3 Answers3

0

I'm not sure if I understand the problem correctly, but you could increment specified counters each time you determined the grade in your if-else-cascade and then std::cout the results after the while loop.

Wum
  • 296
  • 1
  • 9
  • Since i already get the output file i want, but at the end of program, i want count that there is how many A,B,C and so on to to display it in the end of my program – Newbie In Programming Jul 12 '16 at 14:00
  • OK, then go, create 4 integer variables and use them as counters as I said. This seems to be pretty straightforward... – Wum Jul 12 '16 at 14:13
  • is it as the answer below? – Newbie In Programming Jul 12 '16 at 14:56
  • Yes, Polb implemented what I meant. But unfortunately this is like spoon-feeding. After thinking about it you would also have come to this solution... – Wum Jul 12 '16 at 15:11
  • Since it is hard for me to understand your solution in words, but Polb helps to implemented it in codes, it make me easier to understand your solution, thanks! – Newbie In Programming Jul 12 '16 at 15:25
0

You could try what Wum suggested, so the code might look like this:

#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;
}

I applied a bit of indentation to your code, try to use a single coding style. Also, you should read why the use of eof() is not recommended.

Polb
  • 588
  • 7
  • 20
  • Although this code may be help to solve the problem, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Jul 12 '16 at 14:30
  • Thanks alot man, its work fine for me, i will figure out how it works! – Newbie In Programming Jul 12 '16 at 14:44
  • @NeptuneHyper Are there any unclear parts? Feel free to ask now, there is nothing really special about the code, I just coded Wum's suggestion of incrementing a counter as soon as you find a specific grade. Each counter is initially set to 0, since there are no grades processed yet. Rahun Menon's solution looks more elegant, however my answer is more intuitive, a bit easier to understand and you could achieve the same result by using either method. – Polb Jul 12 '16 at 14:54
  • @Polb So did it mean every grade starts by 0, and than after everytime its calculate, the grade increase by 1,and than finally cout the final result of the grade,am i right? – Newbie In Programming Jul 12 '16 at 15:14
  • @NeptuneHyper Yes, you are right. So each grade counter is initially 0. I encounter a grade of 'A', so now all counters will be 0, except for the `numGradesA` counter, which is now 1. If we find another one, it will change its value from 1 to 2. As for the other grade counters, it is absolutely the same. – Polb Jul 12 '16 at 15:27
  • so the updated grade is actually stored in int numsStudents? Thanks Polb for your explanation, it helps me too much,I been searching the answer for few hours or maybe more, I'm very appreciated for your kindness !! – Newbie In Programming Jul 12 '16 at 15:46
  • Yes, the updated number of grades of a specific type (e.g. `numStudentsA` holds how many students have obtained an 'A' and so on). – Polb Jul 12 '16 at 15:49
0

use a array to count the number of students for each grade and print out the array at the end.[ look for inline comments ]

Using a enum to index into the array for easy readability.

Instead of an array you could use maps was one of the suggestions .

eof in the above case is fine as , The first read is outside the while loop and then the check is done at the start. Also the subsequent reads are at the end of the while so the program will loop back right after the read to check the eof conditions and exit (without processing an empty buffer).

The code below checks for 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
Rahul Menon
  • 766
  • 7
  • 12
  • Although this code may be help to solve the problem, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Jul 12 '16 at 14:30
  • there is inline comments in the code , hopefully that helps , added some more details now – Rahul Menon Jul 12 '16 at 14:33
  • Thanks alot, this code it also work fine for me,i will try to figure it out how it works, since your code is different with Polb code below. – Newbie In Programming Jul 12 '16 at 14:57
  • It fundamentally the same as Polb code , that code used discreet integer variables , the code above an array of integers . it count each time the condition is matched and stores the value in the array( or int ) and at the end prints out the counted values . hopefully comments in the code help – Rahul Menon Jul 12 '16 at 15:06
  • It this why you write grades[0] = 0 ; grades[1] = 0 because you use the enum at top of the code? But why in the while loop you changed it to grades[GRADE_A]? – Newbie In Programming Jul 12 '16 at 15:28
  • changed the code to make it uniform , I had done the edit but somehow it got lost . Now the code uniformly uses enum. Its just for ease of reading , otherwise GRADE_A equals 0 so functionally there is no difference – Rahul Menon Jul 12 '16 at 15:41
  • Okay, i get what you mean now, look like i had to start with learning the enum first. By the way, thanks for your help , solve my problem alot :D – Newbie In Programming Jul 12 '16 at 16:12
  • Good , it was of some help :) . – Rahul Menon Jul 12 '16 at 16:15