My program is having some hiccups involving my default statement. While going through the code, my cases seem to be skipped, resulting in output that looks like this instead of just the data from 'Account Number' down, without the error comments
Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Error! INVALID TRANSACTION CODE! Account Number: 467343 Beginning Balance: $23750.4 Ending Balance: $2075.4 Interest Paid: $150 Amount Deposited: $6000 Number of Deposits: 3 Amount Withdrawn: $21825 Number of Withdrawls: 5
The output is fine if I take out my default, but I still need it as a fail safe for if the transaction code is not a valid letter. How can this be fixed without taking out the default?
This is the file and a snippet of the code below.
467343 23750.40 W 250.00 D 1200 W 75.00 I 120.74 W 2500.00 W 3000 D 800 I 30.88 W 16000.00 D 4000.00
inFile >> transCode >> transAmount;
while (!inFile.eof())
{
switch (transCode)
{
case 'D':
case 'd':
acctBalance = endingBalance + transAmount;
amountDeposited = transAmount + amountDeposited;
numberOfDeposits++;
break;
case 'I':
case 'i':
endingBalance = endingBalance + transAmount;
interestPaid = interestPaid + transAmount;
break;
case 'W':
case 'w':
endingBalance = endingBalance - transAmount;
amountWithdrawn = amountWithdrawn + transAmount;
numberOfWithdrawls++;
if (endingBalance < minimumBalance && !isServiceChared)
{
endingBalance = endingBalance - serviceCharge;
isServiceChared = true;
}
break;
default:
cout << "Error! INVALID TRANSACTION CODE!" << endl;
break;
}
inFile >> transCode >> transAmount;
while (!inFile.eof())
is a code smell - where's the code inputtingtransCode
?while (!inFile.eof())
being used correctly.std::cout << *transcode * << transcode << ", transAmount " << transAmount << '\n';
in the loop?