Classes, functions, and manipulating ifstream data

I've got a task whereby I need to take information from a file using a function within a class, then depending on the nature of the information pass it to one of the functions in another class, which will in turn modify data in an array of classes. It's meant to represent a simplified banking system.

The data in the file follows this form:
C
0001
123
D
0002
123
I
0003
0.5

Based on this, my first function will decide that in the record with the ID 0001 the current 'balance' value needs to have 123 added, in 0002 the current 'balance' value needs 123 subtracted, and in 0003 the current 'interest' value needs to be set to 0.5. It then passes the data to the relevant function in a different class, which will in turn effect the changes in the array of the third class.

The problem is, I know only the absolute basics about classes and arrays of classes, and I'm only just learning ifstream code now. And there are functions everywhere in the classes I've been given, including some which I can't see the point of. So any help working through this would be much appreciated. My mess of a code so far will follow shortly.
Last edited on
Okay here's the code. Be aware that some of the stuff that's wrong I probably already know is wrong, it's just so messed up that I haven't got back to that bit. Also, I'm modifying some of the function prototypes as I go because the originals the task gave me seem inconvenient. And in some places I've used variables that obviously won't work, I need to change them to the right ones and start passing them correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>

using namespace std;

class SavingsAccount
{
 public:
        void setUpAccount(savingsAccount)
        {
             savingsAccount accnts[10] = 
             {{123456,0.0,0.05},
             {246890,0.0,0.05},
             {293567,0.0,0.05},
             {337761,0.0,0.05},
             {846579,0.0,0.05},
             {917355,0.0,0.05},
             {937568,0.0,0.05}};
        }
        
        void addDeposit(float, float)
        {
             float deposit;
             float balance;
             
             balance = (balance + deposit);
        }
        
        void subWithdraw(float)
        {
             float withdraw;
             float balance;
             
             balance = (balance - withdraw);
        }
        
        void addInterest()
        {
             intRate = line3;
        }

        float getBalance()
        {
             cout << balance << endl;
        }

        bool matchAccount(int)
        {
             while (accNo != accountNo)
             return false;
        }

 private:
         int accNo;
         int balance;
         float intRate;
};

class Customer
{
 public:
        void createCust(string, string, SavingsAccount)
        {
             cout << "This routine allows staff to enter new customer data." << endl;
             
             cout << "Enter the customer details as follows:" << endl;
             cout << "first name, last name [Return] address [Return] account number [Return]." << endl;
             cin >> name;
             cin >> address;
             cin >> saveAcntNo;
        }
        
        bool searchAccounts(int)
        {
             while (accountNo != accNo)
             return false;
        }
        
        void applyTrans(char, float)
        {
             switch(transType)
               {
                case 'C':
                  SavingsAccount::addDeposit();
                  break;
                case 'D':
                  SavingsAccount::subWithdraw();
                  break;
                case 'I':
                  SavingsAccount::addInterest();
                  break;
               }
        }
        
        void accessAccount(char)
        {
             //statements;
        };

 private:
         string name;
         string address;
         int saveAcntNo;
};

class Transaction
{
 public:
        void readRecord(ifstream)
        {
             float line1;
             float line2;
             char line3;
             while ((inFile.line1) !/0)
                   {
                         inFile >> line1;
                         inFile >> line2;
                         inFile >> line3;
                   }
        }
        
        int findAccount(saveAcntNo, int)
        {
             if (accountNo == saveAcntNo)
                Transaction::process()
             else
                (accountNo++; Transaction::findAccount());
        }
        
        void process(saveAcntNo)
        {
             //statements;
        }

 private:
         int accountNo;
         float amount;
         char transType;
};

int main()
{
      ifstream inFile;
      inFile.open("trans.dat");
      
      Customer.createCust();
      
      system("pause");
      
}
Last edited on
in 0002 the current 'balance' value needs 123 added,
IS that added or subtracted?

The problem is, I know only the absolute basics about classes ...
Okay here's the code. Be aware that some of the stuff that's wrong I probably already know is wrong
What are you asking for exactly?

You seem to have a SavingsAccount, Customer and Transaction class. But if you can have different customers with different accounts, what account does the transaction apply to?

It seems that from what you have there's only one transaction file that applies to one account. In which case, you don't need to worry about different accounts (as there's only one) or customers (it's implied it's the customer's account).

The problems doesn't seem clearly stated.
Yes it was meant to be subtracted. I've fixed the original post now.

Basically what I'm asking for is general help. There's so much that's wrong with my work that if someone just picks a problem near the top and helps me follow it through it should help a lot.

The task states one Customer has one SavingsAccount, and a Transaction acts upon the SavingsAccount. So no, I don't see the point of the Customer class, but it's supposed to be there despite being irrelevant.

The file ("trans.dat") has transactions applying to 7 accounts, so I've tried to set up an array of 10 in case I need to expand a little. Although I need to work out how to fill indexes 7, 8, and 9 with null values. Another thing I don't understand is that the task I have set for me clearly places the function for setting up accounts within the SavingsAccount class. Can I actually do that?
What's missing is the account number in the transaction. There's nothing to bind the transaction to a given account. That's crutial. Is the ID the account number?

If you're going to hold accounts, you need to hold them in something. Typically, a container class will do. But which one? That depends on exactly you're going to do.

There is a problem with SavingsAccount::setUpAccount. But of course you know that. I think you just want a function initialises a container with SavingAccount instances. In which case, your setUpAccount thing is sort of ok (with a little work), but it shouldn't be a member of SavingsAccount. It's what we call a Factory or Creator. But there's not point creating accounts with account numbers that don't match the transaction file.
Okay, I was being a bit lazy when I typed that. The transaction file does in fact use the account numbers I initialised. I've just been working on this for hours and couldn't be bothered when there were actual problems to deal with.

So the setUpAccount function can't be a part of SavingsAccount? That's what I thought, but that's what the task tells me to do. I'd prefer it as a standalone function. This program isn't meant to require user input, so I can just run the function before I start opening the file and calling the functions in the classes.
setUpAccount can be a part of SavingsAccout. But you need to think of it as a quick way of instantiating some account is a little app. In a more realisting scenario, the accounts would persist in a datastore of some kind and you'd ask for one by account number. So setUpAccount really isn't part of SavingsAccount, it's an artifact of your program.

This program isn't meant to require user input, so I can just run the function before I start opening the file and calling the functions in the classes.
That's right. And if setUpAccount were a member of SavingsAccount, it'll be a static member.
Thanks, I think I get it now.

My major intellectual problems at this point are writing the ifstream code to get the lines from the file, and then throwing data around between all the various functions.

So, in lines 108-120 I have my initial readRecord function. A slightly updated version is below. This seems to make sense but I'm not sure. I need to put in a line to return these...
int accountNo
float amount
char transType
... so that they can start bouncing through the functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
void readRecord(ifstream)
        {
             float line1;
             float line2;
             char line3;
             while ((inFile.line1) !/0)
                   {
                         inFile >> line1;
                         inFile >> line2;
                         inFile >> line3;
                   }
             return line1; line2; line3;
        }



Hopefully that's easily fixed. Now, if I want to start passing references between all these functions I get a little lost. So for example I have this function from lines 47-51 above. How do I achieve the purpose here, which is to take the account number returned by the readRecord function and match it to the right account number in the savingsAccount array? Is it achieved by referring to member variables in the same way as functions, eg savingsAccount.saveAcntNo?

1
2
3
4
5
bool matchAccount(int)
        {
             while (accNo != accountNo)
             return false;
        }
Last edited on
Another function question, with regard to the applyTrans function. This function switches to one of three others, and once the chosen function completes, the whole cycle is done. So above I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void applyTrans(char, float)
        {
             switch(transType)
               {
                case 'C':
                  SavingsAccount::addDeposit();
                  break;
                case 'D':
                  SavingsAccount::subWithdraw();
                  break;
                case 'I':
                  SavingsAccount::addInterest();
                  break;
               }
        }


If I changed it to (assuming that a clear cycle is already established):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void applyTrans(char, float)
        {
             switch(transType)
               {
                case 'C':
                  SavingsAccount::addDeposit();
                  Transaction.readRecord();
                  break;
                case 'D':
                  SavingsAccount::subWithdraw();
                  Transaction.readRecord();
                  break;
                case 'I':
                  SavingsAccount::addInterest();
                  Transaction.readRecord();
                  break;
               }
        }


Would that then have the program repeat until the file is finished?
You should apply the transaction to a savings account, so the operation would look like:
1
2
3
4
5
6
7
8
9
10
11
void applyTrans(SavingsAccount &account, Transaction &transaction)
{
    switch (transaction.transType) // or a method that returns this value
    {
    case 'C':
        account.addDeposit();
        break;

    // and so on
    }
}
Thanks, that helped. I've made a few bigger changes so I'll mark this as solved and put up a new thread for the new program. Thanks again for your help.
Topic archived. No new replies allowed.