Question: Modify code but make it the same output. The output will print the students, first name, last name, email, id and gpa #include #include #include
Modify code but make it the same output. The output will print the students, first name, last name, email, id and gpa
#include
#include
#include
using namespace std;
struct student
{
string lName;
string fName;
int stuID;
string email;
float gpa;
student *next;
};
class StudentClass
{
public:
StudentClass();
void getData();
void printAll();
void printStudent(int);
void addStudent();
void deleteStudent(int);
void sortName();
void sortGPA();
void Swap(student *& a, student *& b, student*& c);
private:
int size;
student *head;
student *stuPtr;
student *last;
};
int main()
{
StudentClass stu;
stu.getData();
stu.printAll();
int user = 0;
while (user != 7)
{
cout << " Main Menu ";
cout << "Enter 1 to print the data for all students. ";
cout << "Enter 2 to print the data for a single student. ";
cout << "Enter 3 to add a student. ";
cout << "Enter 4 to delete a student. ";
cout << "Enter 5 to sort the students by last name. ";
cout << "Enter 6 to sort the students by GPA. ";
cout << "Enter 7 to exit the program. ";
cin >> user;
switch (user)
{
case 1:
stu.printAll();
break;
case 2:
int id;
cout << "Enter the ID of the student you want to print. ";
cin >> id;
stu.printStudent(id);
break;
case 3:
stu.addStudent();
break;
case 4:
int index;
cout << "Enter the ID of the student you want to delete. ";
cin >> index;
stu.deleteStudent(index);
break;
case 5:
stu.sortName();
break;
case 6:
stu.sortGPA();
break;
case 7:
return 0;
}
}
}
StudentClass::StudentClass()
{
size = 1;
head = new student;
stuPtr = new student;
last = new student;
}
void StudentClass::getData()
{
ifstream infile;
infile.open("students.dat");
stuPtr = new student;
while (!infile.eof())
{
infile >> stuPtr->lName;
infile >> stuPtr->fName;
infile >> stuPtr->stuID;
infile >> stuPtr->email;
infile >> stuPtr->gpa;
if (size == 1)
{
head = stuPtr;
}
if (infile.eof())
{
continue;
}
stuPtr->next = new student;
stuPtr = stuPtr->next;
size++;
}
last = stuPtr;
last->next = NULL;
infile.close();
}
void StudentClass::printAll()
{
stuPtr = head;
for (int i = 0; i < size; i++)
{
cout << "Name: " << stuPtr->fName;
cout << " " << stuPtr->lName << endl;
cout << "Student ID: " << stuPtr->stuID << endl;
cout << "Email: " << stuPtr->email << endl;
cout << "GPA: " << stuPtr->gpa << endl;
cout << endl;
stuPtr = stuPtr->next;
}
}
void StudentClass::printStudent(int id)
{
stuPtr = head;
bool print = false;
for (int i = 0; i < size; i++)
{
if (stuPtr->stuID == id)
{
cout << "Name: " << stuPtr->fName;
cout << " " << stuPtr->lName << endl;
cout << "Student ID: " << stuPtr->stuID << endl;
cout << "Email: " << stuPtr->email << endl;
cout << "GPA: " << stuPtr->gpa << endl;
cout << endl;
print = true;
}
stuPtr = stuPtr->next;
}
if (print == false)
{
cout << "Student ID not found... ";
}
}
void StudentClass::addStudent()
{
last->next = new student;
stuPtr = last->next;
cout << "Enter the first and last name of the student. ";
cin >> stuPtr->fName;
cin >> stuPtr->lName;
cout << "Enter the student's student ID. ";
cin >> stuPtr->stuID;
cout << "Enter the students email. ";
cin >> stuPtr->email;
cout << "Enter the students GPA. ";
cin >> stuPtr->gpa;
cout << endl;
last = stuPtr;
last->next = NULL;
size++;
}
void StudentClass::deleteStudent(int id)
{
student *prev = new student;
prev = head;
stuPtr = head->next;
bool deleteStu = false;
if (id == 0)
{
head = head->next;
deleteStu = true;
size--;
}
else
{
for (int i = 1; i < size; i++)
{
if (stuPtr->stuID == id)
{
student *temp = new student;
temp = stuPtr;
prev->next = temp->next;
delete temp;
deleteStu = true;
size--;
continue;
}
prev = prev->next;
stuPtr = stuPtr->next;
}
}
if (deleteStu == false)
{
cout << " Student ID not found... ";
}
}
void StudentClass::sortName()
{
student *nodePtr1 = new student;
student *nodePtr2 = new student;
stuPtr = head;
nodePtr1 = head;
nodePtr2 = head;
for (int i = 0; i < size; i++)
{
for (int j = i; j < size; j++)
{
if (nodePtr1->lName.compare(nodePtr2->lName) > 0)
{
student *temp = new student;
Swap(nodePtr1, nodePtr2, temp);
}
nodePtr2 = nodePtr2->next;
}
if (stuPtr->next == NULL)
continue;
stuPtr = stuPtr->next;
nodePtr1 = stuPtr;
nodePtr2 = stuPtr;
}
last = stuPtr;
cout << "The list has been sorted by last name. ";
}
void StudentClass::sortGPA()
{
student *nodePtr1 = new student;
student *nodePtr2 = new student;
stuPtr = head;
nodePtr1 = head;
nodePtr2 = head;
for (int i = 0; i < size; i++)
{
for (int j = i; j < size; j++)
{
if (nodePtr1->gpa < nodePtr2->gpa)
{
student *temp = new student;
Swap(nodePtr1, nodePtr2, temp);
}
nodePtr2 = nodePtr2->next;
}
if (stuPtr->next == NULL)
continue;
stuPtr = stuPtr->next;
nodePtr1 = stuPtr;
nodePtr2 = stuPtr;
}
cout << "The list has been sorted by GPA. ";
}
void StudentClass::Swap(student*& a, student*& b, student*& c)
{
c->fName = b->fName;
c->lName = b->lName;
c->stuID = b->stuID;
c->email = b->email;
c->gpa = b->gpa;
b->fName = a->fName;
b->lName = a->lName;
b->stuID = a->stuID;
b->email = a->email;
b->gpa = a->gpa;
a->fName = c->fName;
a->lName = c->lName;
a->stuID = c->stuID;
a->email = c->email;
a->gpa = c->gpa;
}
------------------------------------
students.dat
-----------------------------
Smith Jefferey 891789 j.smith@spartans.nsu.edu 3.75 Lee Jackson 3678902 j.lee@spartans.nsu.edu 3.66 Gonzales Mariana, 168790 m.gonzales18@spartans.nsu.edu 4.0 Jones Mike 8973125 m.jones143@spartans.nsu.edu 3.1 Williams Anita 2985465 a.williams@spartans.nsu.edu 3.64 Ronsinson Taylor 3278976 t.robinson@spartans.nsu.edu 3.55 Clark Allen 1094567 a.clark@spartans.nsu.edu 3.48 Turner Tavon 318796 t.turner@spartans.nsu.edu 3.2 Jenkins Nelson 289563 n.jenkins@spartans.nsu.edu 3.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
