Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I just need part G: Sort List by Last Name, and H: Sort List By GPA. Thanks in a

ID: 3742327 • Letter: I

Question

I just need part G: Sort List by Last Name, and H: Sort List By GPA. Thanks in advance :-)

Program Summary

Write a C++ object-oriented program to manage a file system for students.

First Name – string

Last Name – string

Student ID – unsigned integer

Email – string

GPA - float

The program will manipulate the student list based on students from the file students.dat

Class requirements

Create a student class or struct based on the student structure

Create a students class which contains three private members – an array of students, the size of the array, and a function to print the contents of a process. The processes class will also provide the following functionality.

Load data from a data file into a student list

Retrieve and print a student from the student list

Insert a student into the student list

Delete a student from the student list

Print the contents of a student record

Print the contents for a list of students

Sort the student list by last Name

Sort the student list by GPA

Processing requirements

Load the students file students.dat

Create a menu to carry out the given operations

Implement input validation to avoid erroneous program errors

Structure of the file

students.dat – First Name, Last Name, Student ID, Email, GPA

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

*********************************MY CODE:

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct studentInfo

{

string firstName;

string lastName;

unsigned int studentID;

string email;

float gpa;

};

class studentsProc

{

public:

void studentList();

void loadList();

void retrieveStudent();

void insertStudent();

void deleteStudent();

void printStudent();

void displayStudent();

void sortLastName();

void sortGpa();

studentsProc();

private:

studentInfo students[40];

int size;

void printList() const;

};

studentsProc::studentsProc()

{

size = 0;

}

void studentsProc::studentList()

{

cout << "0. Smith, Jefferey" << endl;

cout << "1. Lee, Jackson" << endl;

cout << "2. Gonzales, Mariana" << endl;

cout << "3. Jones, Mike" << endl;

cout << "4. Wiliams, Anita" << endl;

cout << "5. Ronsinson, Taylor" << endl;

cout << "6. Clark, Allen" << endl;

cout << "7. Turner, Tavon" << endl;

cout << "8. Jenkins, Nelson" << endl << endl;

}

void studentsProc::loadList()

{

ifstream infile;

infile.open("students.dat");

size = 0;

while (!infile.eof())

{

infile >> students[size].lastName;

infile >> students[size].firstName;

infile >> students[size].studentID;

infile >> students[size].email;

infile >> students[size].gpa;

size++;

}

infile.close();

}

void studentsProc::retrieveStudent()

{

int number;

studentsProc::studentList();

cout << "Please Enter the Position of the Student you Wish to Retrieve " << endl;

cin >> number;

{

cout << "Student Name: " << students[number].firstName << " " << students[number].lastName << endl;

}

}

void studentsProc::insertStudent()

{

int position;

cout << "Enter the Position of the New Student you Would Like to Add: " << endl;

cin >> position;

cout << endl;

cout << "Enter the Following Information About The Student: " << endl;

cout << "First Name: "; cin >> students[position].firstName;

cout << "Last Name: "; cin >> students[position].lastName;

cout << "Student ID: "; cin >> students[position].studentID;

cout << "E-Mail: "; cin >> students[position].email;

cout << "GPA: "; cin >> students[position].gpa;

cout << "The New List Is: " << endl;

studentsProc::displayStudent();

}

void studentsProc::deleteStudent()

{

}

void studentsProc::printStudent()

{

int number;

studentsProc::studentList();

cout << "Please Enter the Number of the Student's Record You Wish to View " << endl;

cin >> number;

{

cout << "First Name: " << students[number].firstName << " || "

<< "Last Name: " << students[number].lastName << " || "

<< "Student ID: " << students[number].studentID << " || "

<< "Student E-mail: " << students[number].email << " || "

<< "Student GPA: " << students[number].gpa << endl;

}

}

void studentsProc::displayStudent()

{

studentsProc::printList();

}

void studentsProc::printList() const

{

cout << "Student List" << endl;

for (int i = 0; i < size; i++)

{

cout << "Last Name: " << students[i].lastName << " || "

<< "First Name: " << students[i].firstName << " || "

<< "Student ID: " << students[i].studentID << " || "

<< "Student E-mail: " << students[i].email << " || "

<< "Student GPA: " << students[i].gpa << endl;

}

}

void studentsProc::sortLastName()

{

}

void studentsProc::sortGpa()

{

}

int main()

{

studentsProc student1;

student1.loadList();

int choice;

do

{

cout << "Please Choose an Option from this Menu" << endl << endl;

cout << "1. Retrieve and Print a Student from the Student List " << endl;

cout << "2. Insert a Student into the Student List " << endl;

cout << "3. Delete a Student from the Student List " << endl;

cout << "4. Print the Contents of a Student Record " << endl;

cout << "5. Print the Contents for a List of Students " << endl;

cout << "6. View the Students List Sorted by Last Name " << endl;

cout << "7. View the Students List Sorted by GPA " << endl;

cin >> choice;

cout << endl;

  

switch (choice)

{

case 1: student1.retrieveStudent();

break;

case 2: student1.insertStudent();

break;

case 3: student1.deleteStudent();

break;

case 4: student1.printStudent();

break;

case 5: student1.displayStudent();

break;

case 6: student1.sortLastName();

break;

case 7: student1.sortGpa();

break;   

default: cout << "Please enter a value 1-7" << endl;

}//end switch

cout << endl << endl;  

} while (choice != 7);

return 0;

}

Explanation / Answer

UPDATED PROGRAM ( I Just Implement both sorting methods)

#include<iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct studentInfo

{

string firstName;

string lastName;

unsigned int studentID;

string email;

float gpa;

};

class studentsProc

{

public:

void studentList();

void loadList();

void retrieveStudent();

void insertStudent();

void deleteStudent();

void printStudent();

void displayStudent();

void sortLastName();

void sortGpa();

studentsProc();

private:

studentInfo students[40];

int size;

void printList() const;

};

studentsProc::studentsProc()

{

size = 0;

}

void studentsProc::studentList()

{

cout << "0. Smith, Jefferey" << endl;

cout << "1. Lee, Jackson" << endl;

cout << "2. Gonzales, Mariana" << endl;

cout << "3. Jones, Mike" << endl;

cout << "4. Wiliams, Anita" << endl;

cout << "5. Ronsinson, Taylor" << endl;

cout << "6. Clark, Allen" << endl;

cout << "7. Turner, Tavon" << endl;

cout << "8. Jenkins, Nelson" << endl << endl;

}

void studentsProc::loadList()

{

ifstream infile;

infile.open("f:\students.dat");

size = 0;

while (!infile.eof())

{

infile >> students[size].lastName;

infile >> students[size].firstName;

infile >> students[size].studentID;

infile >> students[size].email;

infile >> students[size].gpa;

size++;

}

infile.close();

}

void studentsProc::retrieveStudent()

{

int number;

studentsProc::studentList();

cout << "Please Enter the Position of the Student you Wish to Retrieve " << endl;

cin >> number;

{

cout << "Student Name: " << students[number].firstName << " " << students[number].lastName << endl;

}

}

void studentsProc::insertStudent()

{

int position;

cout << "Enter the Position of the New Student you Would Like to Add: " << endl;

cin >> position;

cout << endl;

cout << "Enter the Following Information About The Student: " << endl;

cout << "First Name: "; cin >> students[position].firstName;

cout << "Last Name: "; cin >> students[position].lastName;

cout << "Student ID: "; cin >> students[position].studentID;

cout << "E-Mail: "; cin >> students[position].email;

cout << "GPA: "; cin >> students[position].gpa;

cout << "The New List Is: " << endl;

studentsProc::displayStudent();

}

void studentsProc::deleteStudent()

{

}

void studentsProc::printStudent()

{

int number;

studentsProc::studentList();

cout << "Please Enter the Number of the Student's Record You Wish to View " << endl;

cin >> number;

{

cout << "First Name: " << students[number].firstName << " || "

<< "Last Name: " << students[number].lastName << " || "

<< "Student ID: " << students[number].studentID << " || "

<< "Student E-mail: " << students[number].email << " || "

<< "Student GPA: " << students[number].gpa << endl;

}

}

void studentsProc::displayStudent()

{

studentsProc::printList();

}

void studentsProc::printList() const

{

cout << "Student List" << endl;

for (int i = 0; i < size; i++)

{

cout << "Last Name: " << students[i].lastName << " || "

<< "First Name: " << students[i].firstName << " || "

<< "Student ID: " << students[i].studentID << " || "

<< "Student E-mail: " << students[i].email << " || "

<< "Student GPA: " << students[i].gpa << endl;

}

}

// implement swap() function for interchange all records in the structure

void swap(studentInfo s1,studentInfo s2)

{

// declare temporary variables

string temp_fname,temp_lname,temp_email;

unsigned int temp_studentID;

float temp_gpa;

// assign each structure elements into temporary variables

temp_fname=s2.firstName;

temp_lname=s2.lastName;

temp_email=s2.email;

temp_studentID=s2.studentID;

temp_gpa=s2.gpa;

//swapping of each elements

s2.firstName=s1.firstName;

s2.lastName =s1.lastName ;

s2.email =s1.email;

s2.studentID =s1.studentID;

s2.gpa =s1.gpa;

s1.firstName=temp_fname;

s1.lastName =temp_lname;

s1.email =temp_email;

s1.studentID =temp_studentID;

s1.gpa =temp_gpa;

}

// implement sorting of student records interms of last name

void studentsProc::sortLastName()

{

for(int i=0;i<size-1;i++)

{

for(int j=0;j<size-i-1;j++)

{

if(students[j].lastName>students[j+1].lastName)

{

swap(students[j].lastName,students[j+1].lastName);

swap(students[j].firstName,students[j+1].firstName);

swap(students[j].email,students[j+1].email);

swap(students[j].studentID,students[j+1].studentID);

swap(students[j].gpa,students[j+1].gpa);

}

}

}

printList();

}

// implement sorting of student records interms of GPA

void studentsProc::sortGpa()

{

for(int i=0;i<size-1;i++)

{

for(int j=0;j<size-i-1;j++)

{

if(students[j].gpa>students[j+1].gpa)

{

swap(students[j].lastName,students[j+1].lastName);

swap(students[j].firstName,students[j+1].firstName);

swap(students[j].email,students[j+1].email);

swap(students[j].studentID,students[j+1].studentID);

swap(students[j].gpa,students[j+1].gpa);

}

}

}

printList();

}

int main()

{

studentsProc student1;

student1.loadList();

int choice;

do

{

cout << "Please Choose an Option from this Menu" << endl << endl;

cout << "1. Retrieve and Print a Student from the Student List " << endl;

cout << "2. Insert a Student into the Student List " << endl;

cout << "3. Delete a Student from the Student List " << endl;

cout << "4. Print the Contents of a Student Record " << endl;

cout << "5. Print the Contents for a List of Students " << endl;

cout << "6. View the Students List Sorted by Last Name " << endl;

cout << "7. View the Students List Sorted by GPA " << endl;

cin >> choice;

cout << endl;

  

switch (choice)

{

case 1: student1.retrieveStudent();

break;

case 2: student1.insertStudent();

break;

case 3: student1.deleteStudent();

break;

case 4: student1.printStudent();

break;

case 5: student1.displayStudent();

break;

case 6: student1.sortLastName();

break;

case 7: student1.sortGpa();

break;

default: cout << "Please enter a value 1-7" << endl;

}//end switch

cout << endl << endl;  

} while (choice != 7);

return 0;

}

OUTPUT

Please Choose an Option from this Menu

1. Retrieve and Print a Student from the Student List
2. Insert a Student into the Student List
3. Delete a Student from the Student List
4. Print the Contents of a Student Record
5. Print the Contents for a List of Students
6. View the Students List Sorted by Last Name
7. View the Students List Sorted by GPA

1

0. Smith, Jefferey
1. Lee, Jackson
2. Gonzales, Mariana
3. Jones, Mike
4. Wiliams, Anita
5. Ronsinson, Taylor
6. Clark, Allen
7. Turner, Tavon
8. Jenkins, Nelson

Please Enter the Position of the Student you Wish to Retrieve
2
Student Name: Mariana Gonzales


Please Choose an Option from this Menu

1. Retrieve and Print a Student from the Student List
2. Insert a Student into the Student List
3. Delete a Student from the Student List
4. Print the Contents of a Student Record
5. Print the Contents for a List of Students
6. View the Students List Sorted by Last Name
7. View the Students List Sorted by GPA
6

Student List
Last Name: Clark || First Name: Allen || Student ID: 1094567 || Student E-mail: a.clark@spartans.nsu.edu || Student GPA: 3.48
Last Name: Gonzales || First Name: Mariana || Student ID: 168790 || Student E-mail: m.gonzales18@spartans.nsu.edu || Student GPA: 4
Last Name: Jenkins || First Name: Nelson || Student ID: 289563 || Student E-mail: n.jenkins@spartans.nsu.edu || Student GPA: 3
Last Name: Jones || First Name: Mike || Student ID: 8973125 || Student E-mail: m.jones143@spartans.nsu.edu || Student GPA: 3.1
Last Name: Lee || First Name: Jackson || Student ID: 3678902 || Student E-mail: j.lee@spartans.nsu.edu || Student GPA: 3.66
Last Name: Ronsinson || First Name: Taylor || Student ID: 3278976 || Student E-mail: t.robinson@spartans.nsu.edu || Student GPA: 3.55
Last Name: Smith || First Name: Jefferey || Student ID: 891789 || Student E-mail: j.smith@spartans.nsu.edu || Student GPA: 3.75
Last Name: Turner || First Name: Tavon || Student ID: 318796 || Student E-mail: t.turner@spartans.nsu.edu || Student GPA: 3.2
Last Name: Williams || First Name: Anita || Student ID: 2985465 || Student E-mail: a.williams@spartans.nsu.edu || Student GPA: 3.64


Please Choose an Option from this Menu

1. Retrieve and Print a Student from the Student List
2. Insert a Student into the Student List
3. Delete a Student from the Student List
4. Print the Contents of a Student Record
5. Print the Contents for a List of Students
6. View the Students List Sorted by Last Name
7. View the Students List Sorted by GPA
7

Student List
Last Name: Jenkins || First Name: Nelson || Student ID: 289563 || Student E-mail: n.jenkins@spartans.nsu.edu || Student GPA: 3
Last Name: Jones || First Name: Mike || Student ID: 8973125 || Student E-mail: m.jones143@spartans.nsu.edu || Student GPA: 3.1
Last Name: Turner || First Name: Tavon || Student ID: 318796 || Student E-mail: t.turner@spartans.nsu.edu || Student GPA: 3.2
Last Name: Clark || First Name: Allen || Student ID: 1094567 || Student E-mail: a.clark@spartans.nsu.edu || Student GPA: 3.48
Last Name: Ronsinson || First Name: Taylor || Student ID: 3278976 || Student E-mail: t.robinson@spartans.nsu.edu || Student GPA: 3.55
Last Name: Williams || First Name: Anita || Student ID: 2985465 || Student E-mail: a.williams@spartans.nsu.edu || Student GPA: 3.64
Last Name: Lee || First Name: Jackson || Student ID: 3678902 || Student E-mail: j.lee@spartans.nsu.edu || Student GPA: 3.66
Last Name: Smith || First Name: Jefferey || Student ID: 891789 || Student E-mail: j.smith@spartans.nsu.edu || Student GPA: 3.75
Last Name: Gonzales || First Name: Mariana || Student ID: 168790 || Student E-mail: m.gonzales18@spartans.nsu.edu || Student GPA: 4