Thread: Help in reading text file into a class

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Wink Help in reading text file into a class

    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~!


    Code:
    #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;
    	} 
    }
    Code:
    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

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    And what exactly is the error ?
    Spidey out!

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's very important that you try to explain the error you receive. This is important information that helps you and others understand problems with your programming.

    Still:
    Code:
    private:
                  int CusID[10];
                  string F_name[10];
                  string L_name[10];
                  string CusAdd[10];
                  int CusPhone[10];
                  char CusType[10];
    These things are all arrays, and that is probably not what you wanted. If you don't match the type of a variable to the data, getting a good read is impossible.

    You have a CType as char in your file reading function, which is a lot less data as the array type char [10]. You probably also wanted to use a string here. If you are concerned about the size of the string, you should know that string manages its own memory as one of its advantages. There is also no reason to represent a phone number as an array of integers.

    Reading a file with eof() also presents a problem.
    Last edited by whiteflags; 10-29-2009 at 04:21 AM.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by Spidey View Post
    And what exactly is the error ?
    i not very sure because when i complied and run, the programe just crashed.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    It's very important that you try to explain the error you receive. This is important information that helps you and others understand problems with your programming.

    Still:
    Code:
    private:
                  int CusID[10];
                  string F_name[10];
                  string L_name[10];
                  string CusAdd[10];
                  int CusPhone[10];
                  char CusType[10];
    These things are all arrays, and that is probably not what you wanted. If you don't match the type of a variable to the data, getting a good read is impossible.

    You have a CType as char in your file reading function, which is a lot less data as the array type char [10]. You probably also wanted to use a string here. If you are concerned about the size of the string, you should know that string manages its own memory as one of its advantages. There is also no reason to represent a phone number as an array of integers.

    Reading a file with eof() also presents a problem.
    I see, well thanks, i will try again~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM