#include #include #include using namespace std; struct Student{ string firstName; string lastName; string studentId; double gpa; }; class MyClass{ public: unordered_map classHoursInfo; vector students; unordered_map creditValue = {{"A", 4}, {"B", 3}, {"C", 2}, {"D", 1}, {"F", 0}}; void readStudentData(string filepath){ cout << "reading student data \n"; ifstream inpStream(filepath); string text; string tmp[4]; while (getline (inpStream, text)) { string str = "", prevKey = ""; int j= 0; unordered_map curStudentClassInfo; for(int i=0; i3){ if(prevKey.length()==0){ curStudentClassInfo[str] = 0; prevKey = str; str.clear(); } else{ curStudentClassInfo[prevKey] = creditValue[str]; prevKey = ""; str.clear(); } } else{ tmp[j++] = str; str.clear(); } } else{ str = str+text[i]; } } if(str.length() != 0){ curStudentClassInfo[prevKey] = creditValue[str]; } double curGPA = gpacalculation(curStudentClassInfo, classHoursInfo); students.push_back({tmp[0], tmp[1], tmp[2], curGPA}); } inpStream.close(); } };   double gpacalculation(unordered_map < string,int> studentData, unordered_map creditData);{ cout << "calculating GPA of a student \n" int sumOfCreditHours = 0; int prod = 0; for (auto &x : studentData) { sumOfCreditHours += creditData[,first]; prod += ((x.second)*(creditData[x,first])); } return prod/(sumOfCreditHours+0.0); } void readCreditData(string filepath){ cout << "reading credit data \n"; ifstream inpStream(filepath); string text; string tmp[2]; while (getline (inpStream, text)) { string str = ""; int j= 0; for(int i=0; i

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question

#include <bits/stdc++.h>
#include<unordered_map>
#include <string>

using namespace std;

struct Student{
string firstName;
string lastName;
string studentId;
double gpa;
};

class MyClass{
public:
unordered_map<string, int>
classHoursInfo;
vector<Student> students;
unordered_map<string, int> creditValue = {{"A", 4}, {"B", 3}, {"C", 2}, {"D", 1}, {"F", 0}};

void readStudentData(string filepath){
cout << "reading student data \n";
ifstream inpStream(filepath);
string text;
string tmp[4];
while (getline (inpStream, text)) {
string str = "", prevKey = ""; int j= 0;
unordered_map<string, int> curStudentClassInfo;
for(int i=0; i<text.length(); i++){
if(isspace(text[i])){
if(j>3){
if(prevKey.length()==0){
curStudentClassInfo[str] = 0;
prevKey = str;
str.clear();
}
else{
curStudentClassInfo[prevKey] = creditValue[str];
prevKey = "";
str.clear();
}
}
else{
tmp[j++] = str;
str.clear();
}
}
else{
str = str+text[i];
}
}
if(str.length() != 0){
curStudentClassInfo[prevKey] = creditValue[str];
}
double curGPA = gpacalculation(curStudentClassInfo, classHoursInfo);
students.push_back({tmp[0], tmp[1], tmp[2], curGPA});
}
inpStream.close();
}
};

 

double gpacalculation(unordered_map < string,int> studentData, unordered_map<string,int> creditData);{
cout << "calculating GPA of a student \n"
int sumOfCreditHours = 0;
int prod = 0;
for (auto &x : studentData) {
sumOfCreditHours += creditData[,first];
prod += ((x.second)*(creditData[x,first]));
}
return prod/(sumOfCreditHours+0.0);
}

void readCreditData(string filepath){
cout << "reading credit data \n";
ifstream inpStream(filepath);
string text;
string tmp[2];
while (getline (inpStream, text)) {
string str = ""; int j= 0;
for(int i=0; i<text.length(); i++){
if(isspace(text[i])){
tmp[j++] = str;
str.clear();
}
else{
str = str+text[i];
}
}
if(j!=2) tmp[1] = str;
classHoursInfo[tmp(0)] = stoi(tmp(1));
}
inpStream.close();
}

void writeToFile(string filepath){
cout << "writing students GPA to ouput file and printing in console \n";
ofstream outStream(filepath);
for(Student s: students){
string data = s.firstName+" "+s.lastName+" "+s.studentId+" "+to_string(s.gpa)+"\n";
outStream<<data;
cout<<data;
}
outStream.close();
}
};

int main() {
MyClass c;
c.readCreditData("Credit.txt");
c.readStudentData("Student.txt");
c.writeToFile("StudentsGPA.txt");
}

 

Above is the code i have written but i am having 2 error which i have posted the images for it

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
User Defined DataType
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning