Question: C++ using a data structure and array Create a program that uses a data structure and uses a student name and ID to correspond to
C++ using a data structure and array
Create a program that uses a data structure and uses a student name and ID to correspond to a set of courses. Assume that one student can take at most 99 courses. (You may add array or/and struct to meet the needs.) Design an interactive system which allows the user to query the system in the following way.
| User Input | Output |
| "L" or "l" | System asks for student name or ID. With either one of these two, the system responds with a listing of courses which have been completed by the student. At the end the GPA of the student is displayed. For example, if the user queries the information for Amy, the output should be like: *** 12546 Amy ************* CS1 4 81 3.0 CS2 4 90 4.0 CS3 3 90 4.0 ================== GPA: 3.46 |
| "A" or "a" | System lists information for all students. |
| "Q" or "q" | System quits. |
| Anything else | System responds with error message. |
The student info required must be read from an "input.txt" file.
The "input.txt" file will contain the following:
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
If any clarifcation is needed, feel free to ask. Thank you ahead of time for your help.
I have the following code but it has some errors that I cannot correct, you may use this code or create your own.
#include
#include
#include
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;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
