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.
#include <iostream>
#include <vector>
#include <string>
//#include <list>
#include <deque>
usingnamespace std;
vector<int> nID; //vectors for adding employee
vector<double> nsalary;
vector<int> nhpw;
vector<string> nfname;
vector<string> nlname;
vector<string> nwork;
vector<int> nflag; //used to determine for printing
deque<int> lID;
deque<double> lsalary;
deque<int> lhpw;
deque<string> lfname;
deque<string> llname;
deque<string> 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;
}
elseif(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;
}
elseif(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;
}
elseif(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.
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?