Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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

expand button
Expert Solution
Check Mark
Step 1

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.

 

 

Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education