#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
#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;
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


1st error: You close the class before the gpacalculation function so that’s why code shows the error.
2nd error: you close the class before int main() and also close the class before gpaclaculation() that’s why it shows this ‘{‘ error.
I have closed the class after writetofile() function this resolved both errors.
Step by step
Solved in 2 steps with 1 images









