### Sorting Problem

I got a project that is suppose to take in info about an employee from both a file and user input then print that data in an organized format.(ex: from 1-n or n-1 for ID, alphabetical order, etc.)

I got everything to work so far except for sorting the info.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349`` ``````#include #include #include //#include #include using namespace std; vector nID; //vectors for adding employee vector nsalary; vector nhpw; vector nfname; vector nlname; vector nwork; vector nflag; //used to determine for printing deque lID; deque lsalary; deque lhpw; deque lfname; deque llname; deque lwork; void Addemployee() { int i = 0; int ID; int hpw; //hours per week double salary; string fname; //first name string lname; //last name string work; //work position cout << "Please enter in the employee data for: " << endl; cout << "ID: "; cin >> ID; while (i < nID.size()) //checks if ID already exists { if(ID == nID[i]) //if it does exist, prints a warning { //message and ends program cout << "ID already exists." << endl << endl; return; } i++; } cout << "First name: "; cin >> fname; cout << "Last name: "; cin >> lname; cout << "Salary: "; cin >> salary; cout << "Hours per week: "; cin >> hpw; cout << "Work position: "; cin >> work; //putting the values into the vectors at the end of the vector nID.push_back(ID); nfname.push_back(fname); nlname.push_back(lname); nsalary.push_back(salary); nhpw.push_back(hpw); nwork.push_back(work); nflag.push_back(1); cout << endl; } void Editemployee() { int i = 0; int ID; int hpw; //hours per week double salary; string fname; //first name string lname; //last name string work; //work position if(nID.size() == 0) { cout << "There is no list to compare to." << endl << endl; return; } cout << "Enter ID: "; cin >> ID; while(i < nID.size()) { if(ID == nID[i]) { cout << "Please re-enter in the employee data for: " << endl; cout << "First name: "; cin >> fname; cout << "Last name: "; cin >> lname; cout << "Salary: "; cin >> salary; cout << "Hours per week: "; cin >> hpw; cout << "Work position: "; cin >> work; nfname[i] = fname; nlname[i] = lname; nsalary[i] = salary; nhpw[i] = hpw; nwork[i] = work; cout << endl; return; } else if(i == (nID.size() - 1)) { cout << "ID does not exist." << endl << endl; return; } i++; } } void Removeemployee() { int i = 0; int ID; char c; if(nID.size() == 0) { cout << "There is no data to remove." << endl << endl; return; } cout << "Enter ID: "; cin >> ID; while(i < nID.size()) { if(ID == nID[i]) { cout << "Are you sure you want to remove: "; cout << nfname[i] << " " << nlname[i] << "?" << endl; cout << "(y for yes, n for no)" << endl; cin >> c; switch (c) { case 'y': nID.erase(nID.begin() + i); nfname.erase(nfname.begin() + i); nlname.erase(nlname.begin() + i); nsalary.erase(nsalary.begin() + i); nhpw.erase(nhpw.begin() + i); nwork.erase(nwork.begin() + i); nflag.erase(nflag.begin() + i); cout << " Data removed" << endl << endl; break; case 'n': cout << "Data not removed." << endl << endl; break; default: cout << "Invalid enter. Data not removed." << endl << endl; break; } return; } else if(i == (nID.size() - 1)) { cout << "ID does not exist." << endl; return; } i++; } } void SortList() { char ch; //char a; int i; int j; cout << "Enter which piece of data to sort by: "; cin >> ch; switch (ch) //which data to order { case 'i': lID.push_back(nID[0]); lsalary.push_back(nsalary[0]); lhpw.push_back(nhpw[0]); lfname.push_back(nfname[0]); llname.push_back(nlname[0]); lwork.push_back(nwork[0]); for(i = 1; i < nID.size(); i++) { for(j = 0; j < lID.size(); j++) { if((nID[i]) < (lID[j])) { lID.insert(lID.begin() + j, nID[i]); lsalary.insert(lsalary.begin() + j, nsalary[i]); lhpw.insert(lhpw.begin() + j, nhpw[i]); lfname.insert(lfname.begin() + j, nfname[i]); llname.insert(llname.begin() + j, nlname[i]); lwork.insert(lwork.begin() + j, nwork[i]); break; } else if(j == lID.size()) { lID.push_back(nID[i]); lsalary.push_back(nsalary[i]); lhpw.push_back(nhpw[i]); lfname.push_back(nfname[i]); llname.push_back(nlname[i]); lwork.push_back(nwork[i]); } } } break; default: break; } cout << "Enter oder: "; cin >> ch; switch(ch) { case 'l': for(i = 0; i < lID.size(); i++) { cout << "ID: " << lID[i] << endl; cout << "First name: " << lfname[i] << endl; cout << "Last name: " << llname[i] << endl; cout << "Salary per hour: " << lsalary[i] << endl; cout << "Hours per week: " << lhpw[i] << endl; cout << "Working position: " << lwork[i] << endl << endl; } break; default: break; } lID.clear(); lsalary.clear(); lhpw.clear(); lfname.clear(); llname.clear(); lwork.clear(); } int main() { char c; int i; cout << "Enter a function: "; cin >> c; /* statement to activate the functions: q to quit a to add e to edit r to remove p to print */ while(c != 'q') { switch (c) { case 'a': Addemployee(); break; case 'e': Editemployee(); break; case 'l': SortList(); break; case 'p': if(nID.size() != 0) { for(i = 0; i <= (nID.size() - 1); i++) { if(nflag[i] == 1) { cout << "ID: " << nID[i] << endl; cout << "First name: " << nfname[i] << endl; cout << "Last name: " << nlname[i] << endl; cout << "Salary per hour: " << nsalary[i] << endl; cout << "Hours per week: " << nhpw[i] << endl; cout << "Working position: " << nwork[i] << endl << endl; } } } else { cout << "Nothing to print." << endl << endl; } // cout << lID[0] << endl; break; case 'r': Removeemployee(); break; default: cout << "Invalid function. Try again. " << endl << endl; break; } cout << "Enter a function: "; cin >> c; } return 0; } ``````

The output would either be just one piece of info, a forever loop of the last piece of data, or core dumped. Can someone help me figure out how to sort this info out? I'm just working on sorting by ID and will be able to figure it out once that works. All data is put into different vectors and deques. If there is a way to put all data in one 2d vector and deque then sort it that way, I would appreciate to know how to do it that way.

Thanks.
Last edited on
 If there is a way to put all data in one 2d vector
 ``123456789101112131415161718`` ``````struct Employee { int nID; double nsalary; int nhpw; string nfname; string nlname; string nwork; int nflag; }; vector staff; //Example of working with struct: Employee lisa; lisa.nID = 10; lisa.nsalary = 100500; if(lisa.nID < 25) //... ``````

and you do know about std::sort() function, do you?
Thank you for the reply on how to put all data together. I will use this info. I know the sort() function somewhat, but if I put all data in a struct like that, will it keep all data together like it should be? For ex: info for one employee: ID:1 fname: Bob lname: Smith hpw: 40 salary: 10.25 work: employee. Then once you go to use the sort function, will it keep all info for that one employee together?
 ``1234`` ``````bool operator<(Employee& left, Employee\$ right) { return left.nID < right.nID; }``````
 ``123456789101112131415161718192021`` ``````bool lessID(Employee& left, Employee\$ right) { return left.nID < right.nID; } bool lessSalary(Employee& left, Employee\$ right) { return left.nsalary < right.nsalary; } bool lessFname(Employee& left, Employee\$ right) { return left.nfname < right.nfname; } //And other functions //... vector staff; //... std::sort(staff.begin(), staff.end(), lessFname);//Sort by family name std::sort(staff.begin(), staff.end(), lessSalary);//Sort by salary ``````