Thread: Sorting Help?!?!?!

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    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

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    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.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Wow...Oh my gosh....Thanks lol
    That was a very stupid mistake on my part...Wow....Haha! xD
    THANKS SO MUCH!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

Tags for this Thread