Class error while working with structures
May 15, 2015 at 6:00pm UTC
I'm getting an error saying that my expression must have a class type and I'm not sure why. Looks like the error is coming from within the displayTotal function.
Why is my first structure working but the second one isn't?
Contents of .dat file
2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800
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 151 152 153 154 155 156 157 158 159 160 161 162 163
// Author:
// Source file:
// Description:
// Compiler used:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct transHolder
{
int transCode;
double transAmt;
};
struct counter
{
double creditTotal;
double debitTotal;
double atmTotal;
double serviceCharge;
int transTotal;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream& in_stream);
void displayBal(double );
transHolder getData(ifstream& in_stream);
double processCheck(double , double );
double processDeposit(double , double );
double processATM(double , double );
double processSvcChg(double );
void displayTotal();
//Global Constants
const double CHARGE = 10,
ATMFEE = 2;
int main()
{
//Variable Declarations
double balance;
transHolder trans;
counter totals;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
ifstream inFile;
inFile.open("C:\\CSIT214\\checkIn.dat" );
if (inFile.fail())
{
cout << "\n Input file opening failed.\n" ;
system("pause" );
exit(1);
}
displayTitle();
balance = getBegBal(inFile);
displayBal(balance);
while (!inFile.eof())
{
trans = getData(inFile);
switch (trans.transCode)
{
case 1: balance = processCheck(balance, trans.transAmt);
totals.debitTotal = totals.debitTotal + trans.transAmt;
totals.transTotal++;
break ;
case 2: balance = processDeposit(balance, trans.transAmt);
totals.creditTotal = totals.creditTotal + trans.transAmt;
totals.transTotal++;
break ;
case 3: balance = processATM(balance, trans.transAmt);
totals.atmTotal = totals.atmTotal + trans.transAmt;
totals.serviceCharge = totals.serviceCharge + ATMFEE;
totals.transTotal++;
break ;
}
if (balance < 0)
{
processSvcChg(balance);
totals.serviceCharge = totals.serviceCharge + CHARGE;
}
}
displayBal(balance);
displayTotal();
system("pause" );
return 0;
}
void displayTitle()
{
cout << "\n Check Register\n\n" ;
}
double getBegBal(ifstream& in_stream)
{
double bal;
in_stream >> bal;
cout << " CHECK: " << setw(12) << bal;
return bal;
}
void displayBal(double x)
{
cout << "\t\tBalance = $" << setw(10) << x << endl << endl;
}
transHolder getData(ifstream& in_stream)
{
transHolder temp;
in_stream >> temp.transCode >> temp.transAmt;
return temp;
}
double processCheck(double bal, double amt)
{
cout << "\n Check = " << setw(10) << amt;
return (bal - amt);
}
double processDeposit(double bal, double amt)
{
cout << "\n Deposit = " << setw(10) << amt;
return (bal + amt);
}
double processATM(double bal, double amt)
{
cout << "\n ATM = " << setw(10) << amt;
bal = bal - amt;
displayBal(bal);
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(bal);
return (bal);
}
void displayTotal()
{
cout << "\n\n Total Credits = " << setw(18) << totals.creditTotal << endl;
double deductTotal = totals.debitTotal + totals.atmTotal;
cout << " Total Debits = " << setw(19) << deductTotal << endl;
cout << " Total Service Charges = " << setw(10) << totals.serviceTotal << endl << endl;
}
Last edited on May 15, 2015 at 6:00pm UTC
May 15, 2015 at 11:07pm UTC
'totals' is defined as a local variable inside main, so 'displayTotal' cannot access it.
Try either defining 'totals' as a global variable or pass a parameter containing 'totals'.
1 2 3 4 5 6
struct counter
{
...
} totals;
...
counter totals;
or
(This is preferred)
1 2 3
displayTotal(totals);
...
void displayTotal(counter& totals)
Last edited on May 15, 2015 at 11:09pm UTC
Topic archived. No new replies allowed.