I'm trying to convert this program that's structure based to classes, but I've hit a wall and i don't know what's wrong. I need to make a class for Account and a class for Transaction. Here's a link to the original question (the one with structures), it's question #1.
#include <iostream> // HW 1: Transaction, Account structures
#include <iomanip>
#include "question1.h" //local
usingnamespace std;
int main ()
{
enum { MAX_TRANS = 10, MAX_ACCTS = 6 } ; // array sizes
Transaction data[MAX_TRANS]; Account accts[MAX_ACCTS]; // data arrays
accts[0].num = 40201; accts[0].balance=0; accts[0].count=0;
accts[1].num = 40205; accts[1].balance=0; accts[1].count=0;
accts[2].num = 40211; accts[2].balance=0; accts[2].count=0;
accts[3].num = 40217; accts[3].balance=0; accts[3].count=0;
int num_trans = 0, num_accts = 4;
do { // until end of data or overflow
long acc, number; double amount; // input data from user
Transaction tran; // Transaction to store input data
if (inputData(acc,number,amount) == 0) break; // quit when acc == -1
if (num_trans == MAX_TRANS) // test for overflow: array knowledge
{ cout << "\nOut of memory; input terminated\n" << endl;
break; }
tran.accnum = acc; tran.amount = amount;
tran.trannum = number; // store valid transaction
for (int i = 0; i < num_accts; i++) // find appropriate account
{ if (accts[i].num == tran.accnum)
{ data[num_trans++] = tran;
accts[i].balance += tran.amount; // accumulate amounts
accts[i].count++;
break; }
if (i == num_accts - 1) // not found
cout << "\nIncorrect account; data discarded\n" << endl;
} // end of the FOR loop
} while (true); // end of the DO loop
printTable(accts,num_accts);
while(1);
}
question1.h contains:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream> // Hw 1 solution: Transaction, Account structures
#include <iomanip>
struct Transaction
{
long trannum, accnum; // tran and acc numbers
double amount; } ; // transaction amount
struct Account
{
long num; // account number
double balance; // balance
int count; } ; // count of transactions
void printAccount(const Account &a);
int inputData(long& acc, long& number, double& amount);
void printTable(const Account data[], int num);
#include <iostream> // Hw 1 solution: Transaction, Account structures
#include <iomanip>
#include "question1.h" //local
usingnamespace std;
void printAccount(const Account &a)
// in: a; out: none
{ cout << setw(12) << a.num << setw(11) << a.balance
<< setw(7) << a.count << endl;
}
int inputData(long& acc, long& number, double& amount)
// in: none; out: acc, number, amount
{
cout << "Enter account number (or -1 to end input): ";
cin >> acc;
if (acc == -1) // test for end of data
{ cout <<"\nEnd of input\n\n";
return 0; }
cout << "Enter transaction number and amount: ";
cin >> number >> amount;
return 1;
}
void printTable(const Account data[], int num)
// in: data[], num; out: none
{
int count = 0;
for (int i = 0; i < num; i++) // go over data in array
if (data[i].count > 0) count++;
cout << count << " account(s) are processed\n";
if (count == 0) return; // no output if no input
cout << "\nAccount Number Amount Num of trans\n\n";
cout.setf(ios::fixed); // fixed format for double
cout.precision(2); // digits after decimal point
for (int j = 0; j < num; j++) // go over the data again
{ if (data[j].count > 0)
printAccount(data[j]); }
cout << endl;
}