Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please alter this code to include a class, and have the class be inside a header file. Thanks, nothing too fancy. #include #include #include #include

Please alter this code to include a class, and have the class be inside a header file.

Thanks, nothing too fancy.

#include

#include

#include

#include

#include

using namespace std;

const int NUM_STUDENTS = 25;

struct student

{

string firstname, lastname, major, city;

int studid;

float gpa;

long long phonenumber;

};

int displayMenu()

{

// Display menu

int choice;

cout << "1. Sort Data by First Name "

<< "2. Sort data by Student ID "

<< "3. Sort data by Home city "

<< "4. Sort data by GPA "

<< "5. Display Data "

<< "6. Quit" << endl;

// Ask user to enter choice

cout << "Enter choice: ";

cin >> choice;

// Return choice

return choice;

}

string convertLower(string s)

{

string loweredString = "";

// Iterate over each character of string

// Convert it into lower case

// Return loweredString

for (int i = 0; i

{

loweredString += tolower(s[i]);

}

return loweredString;

}

// Sort by name

void sortByName(student studentArray[], int size)

{

for (int i = 0; i

{

for (int j = i + 1; j

{

// Converts name at index i and j to lowers case

string s1 = convertLower(studentArray[i].firstname);

string s2 = convertLower(studentArray[j].firstname);

// Compare them, if > 0

if ((s1.compare(s2))>0)

{

// Swap the students at i and j

student temp = studentArray[i];

studentArray[i] = studentArray[j];

studentArray[j] = temp;

}

}

}

}

// Sort by city

void sortByCity(student studentArray[], int size)

{

for (int i = 0; i

{

for (int j = i + 1; j

{

// Convert city at index i and j to lower case

string s1 = convertLower(studentArray[i].city);

string s2 = convertLower(studentArray[j].city);

// Compare them

if ((s1.compare(s2))>0)

{

// Swap the students at i and j

student temp = studentArray[i];

studentArray[i] = studentArray[j];

studentArray[j] = temp;

}

}

}

}

// Sort by student ID

void sortByID(student studentArray[], int size)

{

// Sort array using bubble sort

for (int i = 0; i

{

for (int j = i + 1; j

{

// If id at i > j, swap students at i and j

if (studentArray[i].studid>studentArray[j].studid)

{

student temp = studentArray[i];

studentArray[i] = studentArray[j];

studentArray[j] = temp;

}

}

}

}

// Sort by GPA

void sortByGPA(student studentArray[], int size)

{

// Sort array using bubble sort

for (int i = 0; i

{

for (int j = i + 1; j

{

// If gpa at i > j, swap students at i and j

if (studentArray[i].gpa>studentArray[j].gpa)

{

student temp = studentArray[i];

studentArray[i] = studentArray[j];

studentArray[j] = temp;

}

}

}

}

// Displays the student list

void displayData(student studentArray[], int size)

{

cout << endl;

cout << left << setw(15) << "First Name" << setw(15) << "Last Name" << setw(15) << "Major" << setw(15) << "ID" << setw(15) << "GPA" << setw(15) << "Home City" << endl;

cout << "---------------------------------------------------------------------------------------------" << endl;

// Iterates loop size times and print first name, last name, major id, GPA and home city of student

for (int i = 0; i

{

cout << left << setw(15) << studentArray[i].firstname << setw(15) << studentArray[i].lastname << setw(15) << studentArray[i].major << setw(15) << studentArray[i].studid << setw(15) << studentArray[i].gpa << setw(15) << studentArray[i].city << endl;

}

}

// Number of students

void DropClass() // drops the class for students that request

{

char oldname[] = "C:\\test\\temp.txt";

char newname[] = "C:\\test\\studentRecords.txt";

int result;

string line, name;

cout << "Please Enter the student ID: ";

cin >> name;

ifstream myfile;

myfile.open("studentRecords.txt");

ofstream temp;

temp.open("temp.txt");

while (getline(myfile, line))

{

if (line.substr(0, name.size()) != name)

temp << line << endl;

}

cout << "The record with the name " << name << " has been deleted if it existed" << endl;

myfile.close();

temp.close();

remove("studentRecords.txt");

rename(oldname, newname);

}

int main()

{

ifstream ifs("studentRecords.txt");

struct student records[25];

int studid;

float gpa;

long long phonenumber;

string path = "studentRecords.txt";

string firstname, lastname, city, major;

if (ifs.fail()) {

cout << "Error opening student records file (studentRecords.txt)" << endl;

exit(1);

}

// student s;

int i = 0;

while (!ifs.eof())

{

ifs >> studid >> firstname >> lastname >> major >> gpa >> phonenumber >> city;

records[i].studid = studid;

records[i].firstname = firstname;

records[i].lastname = lastname;

records[i].major = major;

records[i].gpa = gpa;

records[i].phonenumber = phonenumber;

records[i].city = city;

i++;

}

cout << "Displaying Information," << endl;

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

{

cout << records[j].studid << " ";

cout << records[j].firstname << " ";

cout << records[j].lastname << " ";

cout << records[j].major << " ";

cout << records[j].gpa << " ";

cout << records[j].phonenumber << " ";

cout << records[j].city << endl;

}

cout << endl;

ifs.close();

DropClass();

char c;

student studList[25];

//object of if stream to read from file

std::ifstream openfile;

//string content

std::string getcontent;

//do while loop

do {

//ask if user wants to continue

cout << "Do you want to exit (y/n)?";

cin >> c;

// if file opens read the file initialize student object add it to list and print it's content

if (openfile.is_open())

{

while (!openfile.eof())

{

student *s = new student();

getline(openfile, getcontent);

cout << getcontent << endl;

}

}

//throw error if file does not open

else {

cout << "There was an error opening file. ";

exit(0);

}

} while (c != 'y');

return 0;

}

void RegisterClass()

{

student *ds;

int n,i;

cout << "Amount of student to be entered: ";

cin >> n;

ds = new student[n + 1];

if (ds == NULL) {

cout << "Error!!!" << endl;

exit(1);

}

//iput data

for (i = 0; i < n; i++) {

cout << "Student #" << i << ": " << endl;

cout << "First Name: ";

cin.get(ds[i].firstname,20);

cout << endl << "Last Name: ";

cin.get(ds[i].lastname,3);

cout << endl << "Major: ";

cin.get(ds[i].major,5);

cout << endl << "StudentId: ";

cin.get(ds[i].studentid,5);

cout << endl << "gpa: ";

cin.get(ds[i].gpa,5);

cout << endl << "phoneNumber: ";

cin.get(ds[i].phonenumber,5);

cout << endl << "Home: " << endl;

cin >> ds[i].Home;

}

for (i = 0; i < n - 1; i++)

for (int j = i + 1; j< n; j++)

if (ds[i].aver_grade < ds[j].aver_grade) {

StudentData temp = ds[i];

ds[i] = ds[j];

ds[j] = temp;

}

cout << setiosflags(ios::showpoint) << setprecision(1);

//display data

for (i = 0; i < n; i++)

cout << ds[i].MSV << setw(3) << ds[i].name << setw(5) << ds[i].name_class << endl

<< "Average Grade: " << ds[i].aver_grade;

delete ds;

return 0;

}

function sortbyfname{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), comparefirstname);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbylname{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), comparelastname);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbymajor{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), compareMajor);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbyid{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), comparestudentId);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbygpa{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), comparegpa);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbyphone{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), comparephoneNumber);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

function sortbyhome{

StudentData *ds;

int n,i;

for (i = 0; i < 100; ++i)

cout("%s\t%s\t%s\t%d\t%d\t%s\t%s ", ds[i].FirstName, ds[i].LastName, ds[i].Major, ds[i].StudentId, ds[i].gpa, ds[i].phoneNumber, ds[i].Home);

qsort(StudentData, 100, sizeof(struct StudentData), compareHome);

for (i = 0; i < 3; ++i)

cin("%s\t%s\t%s\t%d\t%d\t%s\t%s ", info[i].id, info[i].gender, info[i].name);

}

int comparefirstname(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int FirstNamecompare = strcmp(e1->FirstName, e2->FirstName);

if (FirstNamecompare == 0) /* same FirstName so sort by Studentid */

return e1->StudentId - e2->StudentId;

else

return -FirstNamecompare;

}

int comparelastname(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int lastNamecompare = strcmp(e1->LastName, e2->LastName);

if (LastNamecompare == 0) /* same LastName so sort by FirstName */

return e1->FirstName - e2->FirstName;

else

return -lastNamecompare;

}

int comparestudentId(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int studentidcompare = strcmp(e1->StudentId, e2->StudentId);

if (studentidcompare == 0) /* same student id so sort by FirstName */

return e1->FirstName - e2->FirstName;

else

return -studentidcompare;

}

int comparegpa(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int gpacompare = strcmp(e1->gpa, e2->gpa);

if (gpacompare == 0) /* same gpa so sort by FirstName */

return e1->Major - e2->Major;

else

return -gpacompare;

}

int compareMajor(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int majorcompare = strcmp(e1->Major, e2->Major);

if (majorcompare == 0) /* same major id so sort by FirstName */

return e1->Major - e2->Major;

else

return -majorcompare;

}

int comparephoneNumber(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int phonecompare = strcmp(e1->phoneNumber, e2->phoneNumber);

if (phonecompare == 0) /* same phone so sort by FirstName */

return e1->Major - e2->Major;

else

return -phonecompare;

}

int compareHome(const void *s1, const void *s2)

{

struct StudentData *e1 = (struct StudentData *)s1;

struct StudentData *e2 = (struct StudentData *)s2;

int homecompare = strcmp(e1->Home, e2->Home);

if (homecompare == 0) /* same home so sort by FirstName */

return e1->Major - e2->Major;

else

return -homecompare;

}

//Insert of data

cout << ds[i].MSV << setw(3) << ds[i].FirstName << setw(5) << ds[i].LastName << setw(5) << ds[i].Major << setw(5) << ds[i].StudentId << setw(5) << ds[i].gpa << setw(5) << ds[i].phoneNumber << setw(5) << ds[i].Home << endl;

delete ds;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. Develop a persuasive topic and thesis

Answered: 1 week ago

Question

1. Define the goals of persuasive speaking

Answered: 1 week ago