-
Sorting Help?!?!?!
Okay, so i am trying to sort my data by the department field. And when i got to start my program without debugging, the program seems to act like it's in an endless loop and won't update the out file. HELP!!!
Code:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int maxp = 9;
struct whole
{
string first;
string mi;
string last;
string department;
double salary;
};
const whole initrec = {"Howl","s","Moving","Castle",0.0};
void initem(whole p[])
{
int i;
for (i=0; i<maxp; i++) p[i] = initrec;
}
void readem(ifstream &inf, whole p[])
{
int i = 0;
while (!inf.eof())
{
getline(inf,p[i].first,'|');
getline(inf,p[i].mi,'|');
getline(inf,p[i].last,'|');
getline(inf,p[i].department,'|');
inf >> p[i].salary >> ws;
i++;
}
}
void swapem(whole &a, whole &b)
{
whole temp;
temp = a;
a = b;
b = temp;
}
void sortem(whole p[])
{
int i,j;
for (j=0;j<maxp-1;i++)
for (i=0;i<maxp-1;i++) if (p[i].department > p[i+1].department) swapem(p[i],p[i+1]);
}
void printtitles(ofstream &outf)
{
outf << left << setw(9) << "First" << setw(8) << "Middle" << setw(11) << "Last"
<< setw(11) << "Salary" << setw(20) << "Department" <<endl;
outf << left << setw(9) << "_______" << setw(8) << "______" << setw(11) << "_________"
<< setw(11) << "_________" << setw(20) << "________________" << endl;
}
void printem(ofstream &outf, whole p[])
{
int i;
for (i=0;i<maxp;i++)
{
outf << left << setw(9) << p[i].first << setw(8) << p[i].mi << setw(11) << p[i].last
<< setw(11) << p[i].salary << setw(20) << p[i].department << endl;
}
}
void main()
{
whole p[maxp];
ifstream inf;
inf.open("program6.dat");
ofstream outf;
outf.open("program6.out");
outf.setf(ios::fixed);
outf.precision(2);
readem(inf,p);
printtitles(outf);
sortem(p);
printem(outf,p);
}
Here is my data
Code:
John|Q|Smith|English|48000
Sally|T|Handle|Geology|43500
Jorge|G|Gonzales|Biology|38750
Luke|X|Skywalker|Astronomy|100000
Antonio|M|Juarez|History|40200
Han|Z|Solo|Criminal Justice|45700
Anna|L|Henderson|Mathematics|43133
Old|M|McDonald|Agriculture|35000
Zaria|Q|Anderson|Computer Science|57220
-
Code:
void sortem(whole p[])
{
int i,j;
for (j=0;j<maxp-1;i++)
for (i=0;i<maxp-1;i++) if (p[i].department > p[i+1].department) swapem(p[i],p[i+1]);
}
Are you really sure you mean that? Also, indenting your code properly doesn't hurt, and you most likely meant for main to return int? Not sure if there's anything else wrong.
-
Wow...Oh my gosh....Thanks lol
That was a very stupid mistake on my part...Wow....Haha! xD
THANKS SO MUCH!