0

I have a small problem. I have three files, OldMaster, Transaction and NewMaster. If the account numbers match between OldMaster and Transaction, I update the balance and write it to NewMaster. If they don't match, I write the original account info from OldMaster to NewMaster and display an error.

There are 4 accounts in file OldMaster and 4 account transactions in file Transactions. For some reason my program is not processing the fourth (last) account/transaction in each file and NewMaster is not receiving data.

int accountNumber
int accountNum;
string lastName;
string firstName;
float currentBalance;
float dollarAmount;

inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
inTransaction >> accountNum >> dollarAmount;

while ( !inOldMaster.eof() && !inTransaction.eof() )
{
    if ( accountNumber == accountNum )
    {
        currentBalance += dollarAmount;
        outNewMaster << accountNum << " " << firstName << " " << lastName << " " 
                     << currentBalance << endl;
    } 
    else if (accountNumber != accountNum)
    {
        outNewMaster << accountNumber << " " << firstName << " " << lastName << " " 
            << currentBalance << endl;
        cout << "Unmatched transaction record for account number: " << accountNum 
            << endl;
    }

    inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
    inTransaction >> accountNum >> dollarAmount;
}
1
  • For what reasons do you need second 'if' in the construction if ( accountNumber == accountNum )....else **if (accountNumber != accountNum)**? If accountNumber == accountNum is not true, then accountNumber != accountNum is true for sure, you don't need to make a check.
    – fogbit
    Nov 28, 2011 at 6:09

1 Answer 1

2

When you read the data from the input files, if end-of-file is reached in the loop, the loop will not continue and so not write out the newly read data.

If your case I would do something like this:

do
{
    inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
    inTransaction >> accountNum >> dollarAmount;

    if (inOldMaster.bad() || inTransaction.bad())
        break;

    // Your old if-statements

} while (inOldMaster.good() && inTransaction.good());

Your Answer

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

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