Skip to content

Instantly share code, notes, and snippets.

@jsgoller1
Last active February 27, 2019 06:59
Show Gist options
  • Save jsgoller1/bb7b502f17667bddad7fdbf2e3ccab6e to your computer and use it in GitHub Desktop.
Save jsgoller1/bb7b502f17667bddad7fdbf2e3ccab6e to your computer and use it in GitHub Desktop.
Accelerated C++ exercise
/*
3-5. Write a program that will keep track of grades for several students at
once. The program could keep two vectors in sync: The first should hold the
student's names, and the second the final grades that can be computed as input
is read. For now, you should assume a fixed number of homework grades. We'll see
in §4.1.3/56 how to handle a variable number of grades intermixed with student
names.
-------------------------
Again, I think we can just use a map - can we use a mapping of strings to
vectors?
*/
#include <iostream>
#include <map>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
int main() {
map<string, vector<int>> grades;
cout << "Enter a student name (or CTRL-D to stop): ";
string name;
while (cin >> name) {
int grade;
cout << "Enter a list of grades (or CTRL-D to stop): ";
while (cin >> grade) {
grades[name].push_back(grade);
}
cin.clear();
cout << "Enter another student name (or CTRL-D to stop): ";
}
cout << endl;
for (auto student_grade_pair : grades) {
cout << "Student: " << student_grade_pair.first << endl;
cout << "Grades: ";
for (auto grade : student_grade_pair.second) {
cout << grade << " ";
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment