qSort with objects

This is a discussion on qSort with objects within the C++ Programming forums, part of the General Programming Boards category; There is a lot of code...so i removed the parts that aren't important...unless of course i should post over 550 ...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    qSort with objects

    There is a lot of code...so i removed the parts that aren't important...unless of course i should post over 550 lines of code .

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sys\timeb.h>
    using namespace std;
    int nMAXAccountNum = 100;
    class cACCOUNT {
    	private:	int nAccountNum;
    				string sAccountName;
    				float fTotalAmount;
    	public:	cACCOUNT() { nAccountNum = 0;  sAccountName = ""; fTotalAmount = 0; }
    			cACCOUNT(int Num, string Name, float Tot) { 
    				nAccountNum = Num;  sAccountName = Name; fTotalAmount = Tot;    
    			}
    			bool operator < (cACCOUNT & rhs) {
    				return nAccountNum < rhs.nAccountNum;
    			}
    			bool operator == (cACCOUNT & rhs) {
    				return nAccountNum == rhs.nAccountNum;
    			}
    /* all values are read in within here from file or user imput*/
    };
    int compare(const void *var1, const void *var2)
    {
    	if ( (cACCOUNT* )var1 < (cACCOUNT* )var2 ) return -1;
    	if ( (cACCOUNT* )var1 == (cACCOUNT* )var2 ) return 0;
    	else return 1;
    }
    class cTRANS {
    	private:	short nTransYear;
    				short nTransMonth;
    				short nTransDay;
    				float fTransAmount;
    				int nTransAccountNum;
    				char * psTransDescription;
    	public:	cTRANS () { nTransYear = 2006; nTransMonth = 1; nTransDay = 1;
    				fTransAmount = 0; nTransAccountNum = 0; psTransDescription = NULL;
    			}
    			cTRANS(cTRANS & obj) {
    				nTransAccountNum = obj.nTransAccountNum;
    				nTransYear = obj.nTransYear;
    				nTransMonth = obj.nTransMonth;
    				nTransDay = obj.nTransDay;
    				fTransAmount = obj.fTransAmount;
    				psTransDescription = new char [strlen(obj.psTransDescription) + 1];
    				strcpy(psTransDescription, obj.psTransDescription);
    			}
    			~cTRANS () { if (psTransDescription != NULL) delete [] psTransDescription; }
    			bool operator = (cTRANS & obj) {
    				nTransAccountNum = obj.nTransAccountNum;
    				nTransYear = obj.nTransYear;
    				nTransMonth = obj.nTransMonth;
    				nTransDay = obj.nTransDay;
    				fTransAmount = obj.fTransAmount;
    				if (psTransDescription != NULL) delete [] psTransDescription;
    				psTransDescription = new char [strlen(obj.psTransDescription) + 1];
    				strcpy(psTransDescription, obj.psTransDescription);
    				return true;
    			}
    			bool operator > (cTRANS & obj) {
    				if (nTransAccountNum > obj.nTransAccountNum)
    					return true;
    				else
    					if (nTransAccountNum < obj.nTransAccountNum)
    						return false;
    					else 
    				//They're equal go to Year
    				if (nTransYear > obj.nTransYear)
    					return true;
    				else
    					if (nTransYear < obj.nTransYear)
    						return false;
    					else
    				//They're equal go to Month
    				if (nTransMonth > obj.nTransMonth)
    					return true;
    				else
    					if (nTransMonth < obj.nTransMonth)
    						return false;
    					else
    				//They're equal go to Day
    				if (nTransDay > obj.nTransDay)
    					return true;
    				else
    					return false;
    			}
    			bool operator < (cTRANS &obj)
    			{
    				if (nTransAccountNum < obj.nTransAccountNum)
    					return true;
    				else
    					if (nTransAccountNum > obj.nTransAccountNum)
    						return false;
    					else 
    				//They're equal go to Year
    				if (nTransYear < obj.nTransYear)
    					return true;
    				else
    					if (nTransYear > obj.nTransYear)
    						return false;
    					else
    				//They're equal go to Month
    				if (nTransMonth < obj.nTransMonth)
    					return true;
    				else
    					if (nTransMonth > obj.nTransMonth)
    						return false;
    					else
    				//They're equal go to Day
    				if (nTransDay < obj.nTransDay)
    					return true;
    				else
    					return false;
    			}
    			bool operator == (cTRANS & obj)
    			{
    				if ((nTransAccountNum == obj.nTransAccountNum) && 
    						        (nTransYear == obj.nTransYear) &&
    							  (nTransMonth == obj.nTransMonth) && (nTransDay == obj.nTransDay))
    					return true;
    				else
    					return false;
    			}	
    			bool ReadFromFile (ifstream & fIn) {
    				char temp[1000];
    				fIn >> nTransAccountNum >> nTransYear >> nTransMonth >> nTransDay 
    										>> fTransAmount >> temp;
    				if (psTransDescription != NULL)   
    					delete [] psTransDescription;
    				psTransDescription = new char[strlen(temp) + 1];
    				strcpy (psTransDescription, temp);
    				if (fIn.fail() || nTransAccountNum < 0 || 
    						nTransAccountNum > nMAXAccountNum || 
    						nTransMonth < 1 || nTransMonth > 12 || 
    						nTransDay < 1 || nTransDay > 31) 
    					return false;
    				else return true;
    			}
    };
    class cCOMPANY {
    	private:  string sCompanyName;
    			  cACCOUNT arAccounts[100];
    			  cTRANS * parTransactions;
    			  int nNumAccounts;
    			  int nNumTransactions;
    			  float fBankBalance;
    	public: cCOMPANY () { sCompanyName = ""; parTransactions = NULL; 
    						  fBankBalance = 0.0f; nNumAccounts = 0; nNumTransactions = 0;
    			}
    			~cCOMPANY () { if (parTransactions != NULL)  delete [] parTransactions;
    			}
                            void SortAccounts() {
    				qsort(arAccounts,nNumAccounts,sizeof(cACCOUNT), compare);
    
    			}
    };
    From what I can gather...the program seems to know the operators are overloaded in the cACCOUNT class...but i must have typed something wrong within the compare method.

    I just can't seem to get the qsort to work. I've been searching on the net for ways to solve it but i have been unable to.

    Thanks for any help you all can give ,
    Dan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Um, have you considered using std::sort()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Well for my class the teacher wants us to practice using different types of sorts. This is one of the ones we have to do, so I'm trying to get it to work...but I'm having troubles with it.

    Dan
    Last edited by Dan17; 10-03-2006 at 11:52 AM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Your code formatting could do with a examination, looks very out of sorts... I hope it doesn't look like that on your editior. Make sure you indent to 3-4 spaces. Do not use tabs, they are a real pain in my oppionon

  5. #5
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Well I wasnt asking for my code formatting to be examined...but it's just from cutting and pasting that everything got messed up.

    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 11:09 AM
  2. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21