( this is our txt file 12546 Amy CS1 4 81 13455 Bill CS1 4 76 14328 Jim CS1 4 64
ID: 3746176 • Letter: #
Question
( this is our txt file
12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71
12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69 )
/*
Learn how to use struct to load and manipulate data. The input file is named StudentRecords.txt. (The file is also uploaded.)
Step 1: In the while loop of the program instantiate a student struct object. In each iteration, the system should read 5 pieces
of data, which are student ID, name, course, credit and score. Save the data in the Student object. Use cout to display
the values of the object. Carefully examine the output and you can see that there is an extra line of output at the bottom.
(That is, the last two lines are the same, or almost the same.) What cause the problem to occur? Use the debugger to trace
the program and figure out what causes the bug.
Step 2: Let us assume the maximum amount of the students is 999. Since there are hundreds of students, we should use another
data structure to store the student records, so that we can manipulate the data. What data structure could be a nice choice
to use so that all data can be properly stored?
Step 3: After storing the data in the data structures, use the following table to calculate the GPA of each student.
Range Grade
======== =====
90 -- 100 ---> 4.0
80 -- 89 ---> 3.0
70 -- 79 ---> 2.0
60 -- 69 ---> 1.0
0 -- 59 ---> 0.0
We should take the credit of the course into account. For example, the following equation is how we calculate Amy's GPA.
(3.0 * 4 + 4.0 * 4 + 4.0 *3)/(4 + 4 + 3) = 3.64
Step 4: Course listing and GPA for a student should consist of three parts:
(1) ID and name,
(2) A listing of all courses, and
(3) The calculated GPA
For example, Amy's report should look like this:
12546 Amy
CS1 4 81 3.0
CS2 4 90 4.0
CS3 3 90 4.0
======================
GPA 3.64
The last column is the converted grades calculated from the scores.
Step 5: The output should display all students' courses and GPAs.
*/
#include <iostream>
#include <fstream>
using namespace std;
struct Student
{
int ID;
string Name;
string Course;
int Credit;
int Score;
}
int main()
{
fstream inputFile;
string fileName = "StudentRecords.txt";
string token;
inputFile.open(fileName.c_str(), ios::in);
if (inputFile.is_open())
{
while(!inputFile.eof())
{
inputFile >> token;
cout << token << endl;
}
inputFile.close();
}
else
cout << "File cannot be opened.";
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student
{
protected:
int social;
string name;
string *NamePtr;
public:
Student (); // Default constructor
Student(int ssn, string StudentName);
void setSSN(int);
int getSSN();
void setName(string);
string getName();
virtual double calcGPA();
virtual void setQualityPts(int) =0;
};
Student ::Student()
{
}
Student::Student(int ssn, string StudentName)
{
social = ssn;
name = StudentName;
return;
}
void Student::setSSN(int SSN)
{
social = SSN;
return;
}
int Student::getSSN()
{
return (social);
}
void Student::setName(string name2)
{
name = name2;
return;
}
string Student::getName()
{
return name;
}
double Student::calcGPA()
{
cout << "Student calcGPA" << endl;
return (0);
}
class GradStudent : public Student
{
private:
char Status;
public:
GradStudent (char = 'A');
virtual double calcGPA();
void SetStatus(char);
};
GradStudent::GradStudent(char grade)
{
Status = grade;
}
double GradStudent::calcGPA()
{
double GPA = 0.0;
if (Status == 'A' || Status == 'a')
GPA = 4.0;
cout << "GradStudent calcGPA";
return (GPA);
}
void GradStudent::SetStatus (char stat)
{
Status = stat;
return;
}
class UnderGradStudent : public Student
{
private:
double CreditHrs;
double QualityPts;
public:
UnderGradStudent ( double = 0, double = 0);
virtual double calcGPA ();
void setCreditHrs(int);
virtual void setQualityPts(int) =0;
};
UnderGradStudent::UnderGradStudent(double Hours, double Points)
{
CreditHrs = Hours;
QualityPts = Points;
if (Hours < 0)
CreditHrs = 0;
if (Points < 0)
QualityPts = 0;
}
double UnderGradStudent::calcGPA ()
{
double GPA = 0.0;
if (CreditHrs > 0)
GPA = QualityPts / CreditHrs;
cout << "UnderGraduateStudent calcGPA";
return (GPA);
}
void UnderGradStudent::setCreditHrs (int C)
{
CreditHrs = C;
if (C < 0)
CreditHrs = 0;
return;
}
void UnderGradStudent::setQualityPts (int Q)
{
QualityPts = Q;
if (Q < 0)
QualityPts = 0;
return;
}
int main()
{
char ans = 'n', grade, Status, ug;
string StudentName;
int StudentSSN;
int code, CreditHrs, points;
Student* a = NULL;
do
{
cout << "Enter 1 for undergrad. Enter 2 for grad." << endl << "-->";
cin >> code;
if (code == 1)
{
a = new UnderGradStudent;
cin.ignore();
cout << "Enter Student name: ";
getline(cin, StudentName);
a->setName (StudentName);
cout << "Enter Student SSN: ";
cin >> StudentSSN;
a->setSSN(StudentSSN);
cout << "Enter credit hours: ";
cin >> CreditHrs;
a->setCreditHrs(CreditHrs);
cout << "Enter quality points: ";
cin >> points;
a->setQualityPts(points);
cout << endl;
}
else if (code == 2)
{
a = new GradStudent;
cin.ignore();
cout << "Enter Student name: ";
getline(cin, StudentName);
a->setName(StudentName);
cout << "Enter Student SSN: ";
cin >> StudentSSN;
a->setSSN(StudentSSN);
cout << "Enter Status: ";
cin >> Status;
a->setStatus(Status);
cout << endl;
}
cout << " Name: " << a->getName() << " SSN: " << a->getSSN() << " GPA: "
<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << a->calcGPA() << endl;
delete a;
a = NULL;
cout << "Continue?: ";
cin >> ans;
cin.ignore();
cout << endl;
}
while (toupper(ans) == 'Y');
return 0;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.