2 Replies - 3939 Views - Last Post: 29 October 2009 - 05:22 AM Rate Topic: -----

#1 xiaolim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-July 08

Help in reading text file into a class

Post icon  Posted 29 October 2009 - 01:56 AM

Hi there,

i having problem when i tried to read content from a text file into the class i've created.
it keeps on poping me with the same error and i have no idea what when went wrong there.

Anyone can help me with this i will be greatly appreciated.

below are the source code and the test text file

Thanks~!


#include <iostream>
#include <fstream>

using namespace std;

class CustomersAccount
{
	  public:
			 CustomersAccount(){};
			 
			 void setFields(int, string, string, string, int, char, int);
			 void DisplayCustomer(int);
			 static int numCustomers;
			 
	  private:
			  int CusID[10];
			  string F_name[10];
			  string L_name[10];
			  string CusAdd[10];
			  int CusPhone[10];
			  char CusType[10];
	  };
	  
int CustomersAccount::numCustomers = 0;

void CustomersAccount::setFields(int ID, string Fname, string Lname, string Add, int Phone, char Type, int index)
{
	CusID[index] = ID; 
	F_name[index] = Fname;
	L_name[index] = Lname;
	CusAdd[index] = Add;
	CusPhone[index] = Phone;
	CusType[index] = Type;
	}

void CustomersAccount::DisplayCustomer(int index)
{
	 cout << "ID: " << CusID[index] << endl;
	 cout << "First Name: " << F_name[index] << endl;
	 cout << "Last Name: " << L_name[index] << endl;
	 cout << "Address: " << CusAdd[index] << endl;
	 cout << "Telephone: " << CusPhone[index] << endl;
	 cout << "Account Type: " << CusType[index] << endl;
	 }

class BankAccount:public CustomersAccount
{
	  
	  };
	  
class CustomersInvestment:public CustomersAccount
{
	  
	  };

void readFile(CustomersAccount& c);
void displayClass(CustomersAccount c);

/*-------------Timer--------------*/
void Timer(int timeInterval)
{
	 int startTime, currentTime, difference;
	 
	 startTime = time(NULL);
	 
	 do
	 {
			   currentTime = time(NULL);
			   difference = currentTime - startTime;
			   }
			   while(difference < timeInterval);
	 }
/*-------------Timer--------------*/

int main()
{
	cout << "*---------------------------------------------*\n";
	cout << "| Assignment 2 task 1, To produce a piece of  |\n";
	cout << "| code called SIMBank.cpp. SIM Bank keeps	 |\n"; 
	cout << "| track of customers’ portfolios, which	   |\n";
	cout << "| include several bank accounts and		   |\n"; 
	cout << "| investments.								|\n";
	cout << "|											 |\n";
	cout << "| I am taking credit for this.  This will be  |\n";
	cout << "| submited to UOW for grading.				|\n";
	cout << "*---------------------------------------------*\n\n";
	
	cout << " " << endl;
	cout << "*----------------- S.I.M Bank ----------------*\n" << endl;
	
	CustomersAccount Customers;
	
	cout << "Reading all customer's data from file" << endl;
	cout << " " << endl;
	readFile(Customers);
	
	displayClass(Customers);
	//Timer(2);
	system("Pause");
	return 0;
	}

void readFile(CustomersAccount& c)
{
	string CFname, CLname, CAdd; 
	int CID, CPhone, num = 0;
	char CType;
	
	ifstream fin;
	
	fin.open("customers.txt");
	if (!fin.good())
	{
		cout << "File not found" << endl;
		exit(1);
		}
		
	while(!fin.eof())
	{
					 
					 fin >> CID >> CFname >> CLname >> CAdd >> CPhone >> CType; //ERROR
					 c.setFields(CID, CFname, CLname, CAdd, CPhone, CType, num);
					 num++;
					 CustomersAccount::numCustomers++;
					 }
	
	fin.close();
	
	}

void displayClass(CustomersAccount c)
{
	int num = CustomersAccount::numCustomers;
	cout << "Total number of students: " << num << endl;
	
	for (int i = 0; i < num; i++) 
	{
		c.DisplayCustomer(i);
		cout << endl;
	} 
}




10001	Mickey	Mouse	Sengkang	91111111	Gold
50020	Humpty	Dumpty	JurongWest	94444444	NORmal
20001	Felix	CT	cLEMENTI	98888888	Platinum
10007	Snow	White	AngMoKio	92222222	SiLVer
1001	Ben	Jerry	JurongEast	93333333	Normal
10010	Lion	King	JurongWest	93333333	NORMAL
10012	Tweety	Bird	HougangSouth	9007897411	Silver
30031	Pink	Panther	BedokSouth	955555qq	Platinum
210a8	Scoopy	Dog	Woodland	96767676	GOLD
10015	Face	Book	BukitMerah	93333333	NorM



Is This A Good Question/Topic? 0
  • +

Replies To: Help in reading text file into a class

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,199
  • Joined: 14-February 08

Re: Help in reading text file into a class

Posted 29 October 2009 - 02:54 AM

What error are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: Help in reading text file into a class

Posted 29 October 2009 - 05:22 AM

You're trying to read the last field, CType, as a char rather than the string it is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1