Mar 29, 2013 at 8:15pm Mar 29, 2013 at 8:15pm UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#include <iostream>
#include <vector>
#include <string>
//#include <list>
#include <deque>
using namespace 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 ;
}
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 Mar 30, 2013 at 4:09am Mar 30, 2013 at 4:09am UTC
Mar 30, 2013 at 6:45am Mar 30, 2013 at 6:45am UTC
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?
Mar 30, 2013 at 7:52am Mar 30, 2013 at 7:52am UTC
* You can overload operator< for your struct:
1 2 3 4
bool operator <(Employee& left, Employee$ right)
{
return left.nID < right.nID;
}
* You can create multiple comparsion functions and use whichever you need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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<Employee> staff;
//...
std::sort(staff.begin(), staff.end(), lessFname);//Sort by family name
std::sort(staff.begin(), staff.end(), lessSalary);//Sort by salary
Last edited on Mar 30, 2013 at 7:53am Mar 30, 2013 at 7:53am UTC
Mar 31, 2013 at 2:51am Mar 31, 2013 at 2:51am UTC
Thank you, this info will be very helpful =D