-
Notifications
You must be signed in to change notification settings - Fork 2
/
lab9.cpp
195 lines (152 loc) · 4.31 KB
/
lab9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Names: Kyle Gorinson
Matt Law
Shawn Harriott
Lab 9
Description: This program asks the user for a student name, and then asks for the amount of credits for a class (or several).
The program then prints out the student's (or students) GPA.
After that, the names and values are assigned to a map. The user has an option to search for a specific
GPA in a map. The last part is where the GPA's in the map are sorted in chronological order, no matter the
order they were inputted.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <utility>
#include <vector>
using namespace std;
void typeCheck(int);
//student class
class StudentTranscript{
private:
string name;
double credits;
double cumulative;
//converts char input to appropriate GPA
double convertGrade(char g)
{
switch(g)
{
case 'A':
case 'a':
return 4;
break;
case 'B':
case 'b':
return 3;
break;
case 'C':
case 'c':
return 2;
break;
case 'D':
case 'd':
return 1;
break;
case 'E':
case 'e':
return 0;
break;
}
}
public:
//default constructor
StudentTranscript(){
name="none";
credits=0;
cumulative=0;
}
//constructor taking name
StudentTranscript(string nm){
name = nm;
credits = 0;
cumulative = 0;
}
//add grades and credits to GPA
void insert(char grd, int crd){
credits += crd;
cumulative += (convertGrade(grd)*crd);
}
double getGPA(){
//cast to int and back to truncate all decimal values past 2 places
int truncIt=100*(cumulative/credits);
return truncIt/100.0;
}
string getName(){
return name;
}
};
int main()
{
multimap<double, string> Students;
int credits;
char grade, inner, outer;
double GPA;
string name;
vector <StudentTranscript> Student;
do{
cout << "What is the Student's name? ";
cin >> name;
StudentTranscript temp(name);
do
{
cout << "How many credits is the class?" << endl;
cin >> credits;
typeCheck(credits);
cout << "What is the student's grade for the class?" << endl;
cout << "Enter A, B, C, D or E" << endl;
cin >> grade;
temp.insert(grade,credits);
cout << "Press n to quit or any other letter to enter another grade: ";
cin >> inner;
}while(inner!='n');
Student.push_back(temp);
cout << "Press n to quit or any other letter to enter another student: ";
cin >> outer;
}while(outer!='n');
for(int i=0; i<Student.size(); i++)
{
cout<<setprecision(2)<<fixed<<showpoint<<"\nStudent: "<<Student[i].getName()<<" GPA: "<<Student[i].getGPA() << endl;
}
/////////////////Separating non-map part from map part
//assignment Student elements to the map
for (int i = 0; i < Student.size(); i++){
Students.insert(pair<double, string>(Student[i].getGPA(), Student[i].getName()));
}
cout << "Map size: " << Students.size() << endl << endl;
while (GPA != -1){
cout << "Enter a GPA to search for or -1 to quit: ";
cin >> GPA;
if (GPA == -1){
break;
}
if (Students.count(GPA)==1){
cout << Students.count(GPA) << " student has a GPA of " << GPA <<
fixed << showpoint << setprecision(2) << endl << endl; //if statement for grammar
}
else{
cout << Students.count(GPA) << " students have a GPA of " <<
fixed << showpoint << setprecision(2) << GPA << endl << endl;
}
}
cout << "Here are GPA's of students in chronological order: " << endl << endl;
map<double, string>::iterator i;
for(i=Students.begin(); i!=Students.end(); ++i)
{
cout << fixed << showpoint << setprecision(2) << (*i).second << " has a GPA of: " << (*i).first << endl;
}
system("pause");
return 0;
}
void typeCheck(int num){
if (typeid(num) != typeid(int)){
cout << "Enter a GPA to search for or -1 to quit: ";
cin >> num;
}
else
if (!num){
cout << "Enter a GPA to search for or -1 to quit: ";
cin >> num;
}
}