# Using Associative Arrays..

• 03-22-2009
jewelz
Using Associative Arrays..
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
• 03-22-2009
jewelz
anyone?
• 03-22-2009
jewelz
sorry I don't want to be bothersome, but this homework is due pretty soon, and I really am not sure what seems to be the problem...
• 03-22-2009
CornedBee
Heh, a forum isn't express help. You posted a lot of code, so it takes a while for someone to find enough time to look at it.

As a style question, one overload of the grade function has a completely different meaning than the other three. That's very bad - sets of overloads should always have the same meaning.
In fact, the first overload also has a different meaning from number two and three, and the second is pretty much redundant.

This bit makes no sense:
Code:

```            char c = grade(final_grade);             while (cin >> c)                 ++counters[c];```
Why do you do any input here?
• 03-22-2009
jewelz
You're right..i deleted that portion and it still works fine...the final part, however, still doesn't seem to work..
• 03-23-2009
CornedBee
Just to be clear, the problem is that it doesn't print anything after "Grade Distribution"?

Can you post the updated code?
• 03-23-2009
jewelz
Quote:

Originally Posted by CornedBee
Just to be clear, the problem is that it doesn't print anything after "Grade Distribution"?

Can you post the updated code?

yeah it doesn't print anything after Grade Distribution..and here is the updated code:

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);                                 } 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; }```
• 03-23-2009
CornedBee
You removed too much. It was only the while() line that didn't make sense. Now you're not filling the map.
• 03-23-2009
jewelz
hey, thanks for your help, I figured it out. here is the final update:

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     char c;                        // letter grade             // 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);             char c = grade(final_grade);             ++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; }```