Hey everyone,

I have this assignment for my C++ class and I need to sort some data
I have data in a file in this order:

some 10 digit int -the account id
some name -the account name
some balance -the account balance

basically I want to sort the data by the account id, however, It doesent seem to be working at all. My code has compiled and linked and even run through all its other process correctly but it wont sort

here is my code:

//Assignment #6 due 2-8-2010
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//global declarations
typedef char Name_t[15];
typedef fstream Infile_t;
typedef fstream Outfile_t;
typedef char Filename_t[50];
//*********************************************************************************************************
//classes
class cAccount
{
	private:
		int accountID;
		Name_t accountName;//(last,first)
		float accountBal;
	public:
		cAccount();
		int get_accountID(int &accountID)
		{ accountID=this->accountID; return accountID;};
		void read(Infile_t &Infile);
		void print(Outfile_t &Outfile);
		void display();
};//end of cAccount
class cAccountList
{
	private:
		static const int MAXARRAYSIZE=51;
		int counter;
		typedef cAccount AccountArray[MAXARRAYSIZE];
		AccountArray AccountList;
	public:
		cAccountList();
		void load_data(Infile_t &Infile);
		void print_to_outfile(Outfile_t &Outfile);
		void print_to_screen();
		void sort_by_ID();
};//end of cAccountList
//*********************************************************************************************************
//class cAccount functions
cAccount::cAccount()
{	
	accountID=0;
	accountBal=0;
};//default constructor	
void cAccount::read(Infile_t &Infile)
{ Infile>>accountID>>accountName>>accountBal; };
//end of cAccount::Read
void cAccount::print(Outfile_t &Outfile)
{ 
	Outfile<<"	"<<accountID<<"	"<<accountName<<"	"<<accountBal<<endl;
	Outfile<<"_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_"<<endl;
};//end of print
void cAccount::display()
{
	cout<<"	"<<accountID<<"	"<<accountName<<"	"<<accountBal<<endl;
	cout<<"***********************************************************"<<endl;
};//end of display
//*********************************************************************************************************
//class cAccountList functions  
cAccountList::cAccountList()
{ counter=0; };//default constructors
void cAccountList::load_data(Infile_t &Infile)
{
	Filename_t Filename;
	cout<<"Please enter the name of the datafile to be used:"<<endl;
	cin>>Filename;
	Infile.open(Filename,ios::in);
	AccountList[counter].read(Infile);
	while(!Infile.eof())
	{
		counter=counter+1;
		AccountList[counter].read(Infile);
	}//end while
	Infile.close();
	cout<<"Datafile read....SUCCESS!"<<endl;
};//end of read_data
void cAccountList::print_to_outfile(Outfile_t &Outfile)
{
	Filename_t Filename;
	cout<<"Please enter the name of the output file:"<<endl;
	cin>>Filename;
	Outfile.open(Filename,ios::out);
	Outfile<<"**************************************************************"<<endl;
	Outfile<<"	"<<"ACCOUNT_ID#"<<"	"<<"ACCOUNT_NAME"<<"	"<<"ACCOUNT_BALANCE"<<endl;
	Outfile<<"**************************************************************"<<endl<<endl;
	for(int I=0; I<counter; I++)
	{
		AccountList[I].print(Outfile);
	}//end for
	Outfile.close();
	cout<<"Outfile write...SUCCESS!"<<endl<<endl;
};//end print_to_outfile
void cAccountList::print_to_screen()
{
	cout<<"Generating preview of output....."<<endl<<endl;
	cout<<"**************************************************************"<<endl;
	cout<<"	"<<"ACCOUNT_ID#"<<"	"<<"ACCOUNT_NAME"<<"	"<<"ACCOUNT_BALANCE"<<endl;
	cout<<"**************************************************************"<<endl<<endl;
	for(int I=0; I<counter; I++)
	{
		AccountList[I].display();
	}//end for
}//end print_to_screen
void cAccountList::sort_by_ID()
{
	int I,Pass;
	int accountID;
	AccountArray temp;
	for(Pass=1; Pass<counter-1; Pass++)
	{
		for(I=0; I<counter-1; I++)
		{
		if(AccountList[I].get_accountID(accountID) < AccountList[I+1].get_accountID(accountID));
			{
				temp[I]=AccountList[I];
				AccountList[I]=AccountList[I+1];
				AccountList[I+1]=temp[I];
			}//end if
		}//end inside for
	}//end outside for
}//end sort_by_ID
//*********************************************************************************************************
//main
int main()
{
	Infile_t Infile;
	Outfile_t Outfile;
	cAccountList ACCOUNT;
	ACCOUNT.load_data(Infile);
	ACCOUNT.sort_by_ID();
	ACCOUNT.print_to_outfile(Outfile);
	ACCOUNT.print_to_screen();
	return 0;
}//end main
//*********************************************************************************************************

does anyone have any ideas?

I can't run it since I don't have the file, but the lack of an assignment operator seems possibly problematic to me, particularly since you are using C-style strings, which are just pointers. Put some debugging statements around lines 118 to 120 to verify that:

  1. There is an attempt to make a swap.
  2. The swap is actually swapping correctly.

There are many places in the code where pointer is expected, but you are passing the parameter by value; example: load_data(), get_accountID(), print_to_outfile() . The compiler should have given warning regarding this.

I found the answer to the issue of the swap not working and it is actually quite simple when I think about it.

On line 116 I ended my "if" statement with a ';'. Naturally that ; kills the process so the program never makes it to the swap logic.

Thanks for all of the help!!!

There are many places in the code where pointer is expected, but you are passing the parameter by value; example: load_data(), get_accountID(), print_to_outfile() . The compiler should have given warning regarding this.

i dont know if I would need any pointers because all the functions are members of the classes and so i dont think thats the problem, plus i compiled using vi and didnt get any errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.