Question
I'm trying to solve this problem. I can make a bubble sort, but I don't know about the pointer part in C++... Can you show
I'm trying to solve this problem. I can make a bubble sort, but I don't know about the pointer part in C++... Can you show me in code? The Problem,my code and, input data in below.
Problem:
Modify Lab 2 Part I by adding sort options.
Add menu options to sort by various fields: first name, last name, gpa, id , and email.
Write one function, that can sort by any field using array of pointers. Do not copy and paste sort code five times into the same function. Tip: use bubble sort. It is easier to modify.
My code:
#include
using namespace std;
//class Records for on person class Records { private: string Firstname, Lastname, Email, ID; double GPA;
public: void setRecord(string Firstname, string Lastname, string ID, string Email, double GPA);
string getFname() const { return Firstname; } string getLname() const { return Lastname; } string getEmail() const { return Email; } string getID() const { return ID; } double getGPA() const { return GPA; } void show() const; };
void Records::setRecord(string f, string l, string i, string e, double g) { Firstname = f; Lastname = l; ID = i; Email = e; GPA = g; }
void Records::show() const { cout << setw(15) << getFname() << setw(15) << getLname() << setw(7) << getGPA() << setw(15) << getID() << setw(30) << getEmail() << endl; }
//Function prototype void inputData(Records RecArr[], int &numRecs); void UnsortedPrint(Records StudentRc[], int numRecs); int IDsearchRecord(Records StudentRc[], int numRecs, string ID); void showserchData(Records RecArr[], int numRecs);
int main() { const int SIZE = 100; Records RecArr[SIZE]; Records Rc; int numRecs, pos; bool cont = true; char Option = 'x'; inputData(RecArr, numRecs); //An Option Menual while (toupper(Option !='Q')) { cout << " \tA. Print records that is unsorted" << endl; cout << "\tB. Search student records by studentID" << endl; cout << "\tQ. Quit the progream" << endl; cout << "\tOption : "; cin >> Option; switch (Option) { case 'A': case 'a': UnsortedPrint(RecArr, numRecs); break; case 'B': case 'b': showserchData(RecArr, numRecs); break;
default: cout << " \tYou entered a wrong option. Please enter the option in the Menu "; break; } } return 0; }
//input data from the file void inputData(Records RecArr[], int &numRecs) { fstream inFile; inFile.open("C:\\Users\\JONGMIN\\Desktop\\input.txt");
string f, l, e, i; double g; int count = 0;
while (inFile >> f >> l >> g >> i >> e) { RecArr[count].setRecord(f, l, i, e, g); count++; } numRecs = count;
inFile.close(); }
//Print all the records that is not sorted void UnsortedPrint(Records StudentRc[], int numRecs) { cout << setw(15) << "First Name" << setw(15) << "LastName" << setw(7) << "GPA" << setw(15) << "ID" << setw(30) << "Email" << endl; for (int i = 0; i < numRecs; i++) { StudentRc[i].show(); } }
//Search the studnet record by entering ID int IDsearchRecord(Records StudentRc[], int numRecs, string ID) { bool found = false; int i = 0; for (; i < numRecs && !found; i++) { if (StudentRc[i].getID() == ID) { found = true; } }
if (!found) return -1; else return (i - 1); }
void showserchData(Records RecArr[], int numRecs) { int pos = 0; string ID; cout << " \tEnter student ID: "; cin >> ID; pos = IDsearchRecord(RecArr, numRecs, ID); if (pos == -1) cout << " \tThe ID data is not found." << endl; else { cout << setw(15) << "First Name" << setw(15) << "LastName" << setw(7) << "GPA" << setw(15) << "ID" << setw(30) << "Email" << endl; RecArr[pos].show(); cout << "\t The student record postion is " << pos << endl; } }
input Data:
Jongmin Choi 3.42 900092029 Hoycoi124@naver.com Hony Jang 3.92 900092030 joeychoi9624@naver.com Amy Hges 3.22 900092031 amydaei724@naver.com Sunny Awda 3.98 900092032 sunnyaghwi1114@naver.com Jhon Llikea 2.84 900092033 ceiwn2871@naver.com Piter Harke 3.11 900092034 bijii100000@naver.com
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started