0

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;
7
  • 2
    while (!inFile.eof()) is a code smell - where's the code inputting transCode? Mar 25, 2015 at 4:39
  • 2
    @Tony: This is a rare example of while (!inFile.eof()) being used correctly. Mar 25, 2015 at 14:38
  • 1
    @LightnessRacesinOrbit: almost - it will only work correctly if the last line is terminated by some whitespace, which decent editors enforce (in the form of a newline) but can be a nasty thing to debug. It also fails to check badbit, so parsing may have failed. Mar 25, 2015 at 14:44
  • 1
    @user3656280 have you tried adding std::cout << *transcode * << transcode << ", transAmount " << transAmount << '\n'; in the loop? Mar 25, 2015 at 14:45
  • 1
    Please provide a MCVE or this question should be closed. Mar 25, 2015 at 14:54

1 Answer 1

0

I assume transCode is a char, so on the first line of the file (467343 23750.40) it gets set to 4 then transAmount gets set to the rest of the word, 67343. Then on the next loop transCode gets set to the next character, which is the 2 at the start of 23570.40, and transAmount gets set to 3570.40

You need to do something like read the first word on the line into a string then, see if it is a valid code or a number. Or you could read transCode and check it before reading transAmount.

4
  • I'd worked through the same possibility too, but it doesn't explain so many copies of the "default" message. Mar 25, 2015 at 14:50
  • @TonyD, won't the stream's failbit be set when trying to read W as a double, and not update transCode after that, so it just loops on the same error? In any case, inFile >> transCode >> transAmount is very fragile and can't handle unexpected numbers of fields in a line, or fields in unexpected formats. Mar 25, 2015 at 14:52
  • 1
    My thinking was that if transCode is a char and transAmount a double or float, then the first line's broken up as 4 / 67343, then 2 / 3750.40, then the other lines are parsed successfully too... badbit is never set and the eof is reached. Anyway, your comment about MCVE is on the money. Mar 25, 2015 at 14:57
  • @TonyD, you're right ... you thought it through further than me :) Mar 25, 2015 at 14:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.