Hi all.
This regards "Accelerated C++" Exercise 7-2.
I'm trying to write a program that uses a map to put students grades into grade bands and print which band each student falls into.
The grade bands are (A: 90-100, B: 80-89.99, C: 70-79.99, D: 60-69.99, F: <60).
I'm getting into a bit of a mess with it and I can't see where or why exactly?
My program is as follows:
#include "Student_info.h"
#include <vector>
#include <map>
using std::cin; using std::vector; using std::cout;
using std::endl; using std::map; using std::string;
int main()
{
vector<Student_info> students;
Student_info student;
char A, B, C, D, E, F;
map<char, vector<string> > bands;
while(read(cin, student))
{
students.push_back(student);
}
cout << endl;
// Move to seperate function
for(vector<Student_info>::const_iterator it = students.begin(); it != students.end(); ++it)
{
if(it->grade >= 90 && it->grade <= 100)
bands[A].push_back(it->name);
else if(it->grade >= 80 && it->grade <= 89.99)
bands[B].push_back(it->name);
else if(it->grade >= 70 && it->grade <= 79.99)
bands[C].push_back(it->name);
else if(it->grade >= 60 && it->grade <= 69.99)
bands[D].push_back(it->name);
else if(it->grade < 60)
bands[F].push_back(it->name);
}
for(map<char, vector<string> >::const_iterator it = bands.begin(); it != bands.end(); ++it)
{
cout << it->first << ": ";
vector<string>::const_iterator stud_it = it->second.begin();
cout << *stud_it;
++stud_it;
while(stud_it != it->second.end())
{
cout << ", " << *stud_it;
++stud_it;
}
cout << endl;
}
return 0;
}
Student_info.cpp
#include "Student_info.h"
#include <vector>
using std::istream; using std::vector;
istream& read(istream& is, Student_info& s)
{
is >> s.name >> s.grade;
return is;
}
Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info
// Student_info header file
#include <iostream>
#include <string>
struct Student_info
{
std::string name;
double grade;
};
std::istream& read(std::istream&, Student_info&);
#endif
My input is:
student1
98
student2
87
student3
45
student4
67
student5
56
student6
72
My ouput is:
: student1, student6
3: student2
m: student4
w: student3, student5
I want it to be:
A: student1
B: student2
C: student6
D: student4
F: student3, student5
Where am I going wrong?
It's my first time using maps. I figured I'm screwing up with the key but I don't know what I'm doing wrong.
Thanks in advance for any help offered.