using constructor to initialize data

This is a discussion on using constructor to initialize data within the C++ Programming forums, part of the General Programming Boards category; Heres some info on the problem. the program has to read from a file (file contains information on individuals bank ...

  1. #1
    Unregistered
    Guest

    Question using constructor to initialize data

    Heres some info on the problem.
    the program has to read from a file (file contains information on
    individuals bank accounts) and "As each data line is read (each line is a
    persons bank account information) you should allocate a new record and
    initialize the data". I want to store these lines of data from the file as
    elements in an array. Hopefully you understand what I'm getting at. How can
    I initialize the data im getting from the file without an array? Or how can
    I do it with an array? Any ideas welcome. Some lines of data will require me to use a different constructor to initialize only 2 fields or a different constructor to initialize 3 fields.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Code:
    class BankRecord
    {
       public:   
          char *name;
          int accountNum;
          int balance;
       protected:
          BankRecord();
          void printInfo();
    };
    
    BankRecord::BankRecord()
    {
       name = 0;
       accountNum = 0;
       balance = 0;
    }
    
    void BankRecord::printInfo()
    {
       cout << "Name: " << name << endl
               << "Account #: " << accountNum << endl
               << "Balance: " << balance << endl;
    }
    
    BankRecord array[20];

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Biosx, i dont think you were reading it right, he asked how do you read it from a file, not how do you output it (not flaming you just saying read more carfully ). Anyways you dont need to have an array to hold what you are reading from the file. First id have to say you shouldnt have your constructor open and read the file, you can but you cant reutrn a value (if an error occured) from a constructor. If you go that way you have to add a variable for wheter or not the reading was successful. There are 2 ways I can think of right now that you can do this, 1) pass an istream object to the class's constructor, or just make the istream object part of the class and hardcode the file into the class. I prefer the first method since this is kinda cleaner (i dont like hardcoding things unless i have to). Heres a simle way to read:
    Code:
    bool opened = false; // initialy the file didnt open
    string  strLine;     // will hold 1 line of text from the file
    
    istream in("accounts.txt");
    if(in.open())
    {   opened = true;
        while(!in.eof())
        {  getline(in,strLine);
           // parse the string here, since i dont know how your storing
           // your file ill leave this for you to figure out
        }
    }
    This works particuarly well but you have to remember each time through the loop strLine will change so make sure you parse the string inside the loop! Good luck
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    class account
    {
      public:
        int id;
        char name[30];
        account();
    }
    
    account::account()
    {
      int = 0;
      strcpy(name, "none");
    }
    
    int main()
    {
       account accounts[10]
       int index = 0;
       ifstream fin("myFile.txt");
       fin >> accounts[index].id;
       while(!fin.eof() && index < 10)
       {
          fin.ignore();
          fin.getline(accounts[index++].name; 30);
          fin >> accounts[index].id;
        }
    
        for(int i = 0; i < index; i++)
        {
           cout << accounts[i].id << ' ' << accounts[i].name << endl;
        }
    return 0;
    }
    Example of code to do the task but is uncompiled code and lazy style. Ideally would use private variables, accessor and mutator functions. May be best to use a list or vector if you don't know how many customers are going to be in file. If using home grown list will need to use dynamic memory to declare each new account, and then be sure to delete memory when list is no longer needed. Should also always test that file opened, and adequate memory available if you are doing memory management.

  5. #5
    Unregistered
    Guest

    Here is what I'm getting at....

    My professor gave me this project and he has special rules that go along with it. I'm trying to initialize private members but there are no public methods of doing so. Only constructors can be used to initialize the data. There are 7 people that I have to keep bank accounts for. I have to read in the file and as I read each line of data (some lines contain and extra parameter like the balance left in an accout) I have to figure out how many parameters there are and judging by that.......I have to call the correct constructor to initialize the private members of a class called BankAccount. I'm trying to somehow use an array. Even if I create 7 objects.....i still have to initialize them right away and I cant since I didnt have a chance to read in the file. Any help would be appreciated.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you must intialize data in a constructor only you try using a multi-argument constructor, something like this:

    account::account(int Id, char * Name)
    {
    id = Id;
    strcpy(name, Name);
    //next = NULL; //to make this a node for a list, see below
    }

    and in main() read in data from file to dummy variables, passing the dummy variables to the (appropriate) constructor, and then assigning the "new" account to the array

    int main()
    {
    int id;
    char name[30];
    account accounts[7];
    ifstream fin("file.txt");
    int index = 0;
    fin >> id;
    fin.ignore();
    while(!fin.eof() && index < 7)
    {
    fin.getline(name, 30);
    account temp(id, name);
    account[index++] = temp;
    }
    //etc;


    although it might be more appropriate to use the multi-argument constructor with the new operator to create a new node for a linked list of accounts doing something like this:

    int main()
    {
    int id;
    char name[30];
    account accounts[7];
    ifstream fin("file.txt");
    int index = 0;
    fin >> id;
    fin.ignore();
    while(!fin.eof() && index < 7)
    {
    fin.getline(name, 30);
    account * temp = new account(id, name);
    accountList.add(temp);
    fin >> id;
    fin.ignore();
    }

    You might need multiple, multi-argument constructors to cover all possibilities. Then you would probably need to read the entire line in at once using getline(), parse the line into approapriate variables, and then pass the variables to an appropriate constructor, although that seems like an over elaborate project, and I would reread your instructions to be sure you understand them correctly.

  7. #7
    Unregistered
    Guest

    Thank you guys

    You guys just sent me back on the right track. I think i just figured it out and so far it worked on my little test program I made. I'm going to use a temporary Bankaccount to hold the values from the file and then set the bankaccount after proper initialization to an element in the array. Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

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