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;
}
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());