Hey guys, I need some help...the HW description is basically that I have to allow the following program to print letter grades for each student following a certain criteria, and then it has to print print the number of A's, B's, (etc.)...
ok so im having problems with the 2nd part of the description (print the # of A's, B's, etc)
all it does is output the name of the student, letter grade, and numeric grade, and underneath that it says "Grade Distribution:" and program stops there...
here is my code:
main function
Code:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"
#include <map>
using std::cin; using std::setprecision;
using std::cout; using std::sort;
using std::domain_error; using std::streamsize;
using std::endl; using std::string;
using std::max; using std::vector;
using std::map;
int main()
{
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0; // the length of the longest
// name
map<char, int> counters; // container that stores letter
// grades and keeps track of how many
// times it shows up
// read and store all the students' data.
// Invariant: students contains all the student records read so far
// maxlen contains the length of the longest name in students
while (read(cin, record)) {
// find length of longest name
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
// alphabetize the student records
sort(students.begin(), students.end(), compare);
// write the names and grades
for (vector<Student_info>::size_type i = 0;
i != students.size(); ++i) {
// write the name, padded on the right to maxlen + 1 characters
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(), ' ');
// compute and write the grade
try {
double final_grade = grade(students[i]);
cout << grade(final_grade) << "\t";
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade
<< setprecision(prec);
// read input and keep track of each letter and how many times we see it
char c = grade(final_grade);
while (cin >> c)
++counters[c];
} catch (domain_error e) {
cout << e.what();
}
cout << endl;
}
cout << endl;
cout << "Grade Distribution:" << endl;
cout << endl;
// print letters and how many times each one shows up
for (map<char, int>::const_iterator it = counters.begin();
it != counters.end(); ++it) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}
implementation file for functions:
Code:
mpute a student's overall grade from midterm and final exam
// grades and homework grade
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because median does so for us.
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
double grade(const Student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
char grade(double num_grade)
{
if (num_grade < 0 || num_grade > 100)
throw domain_error("numerical grade outside legal range [0, 100]");
if (num_grade >= 90 && num_grade <= 100)
return 'A';
if (num_grade >= 80)
return 'B';
if (num_grade >= 70)
return 'C';
if (num_grade >= 60)
return 'D';
if (num_grade >= 0 && num_grade < 60)
return 'F';
}
i think the problem lies within in the end of my main function...can you guys please review it and see if u can find some errors? thanks