我有一个小问题。我有三个文件,OldMaster,Transaction和NewMaster。如果帐号在OldMaster和Transaction之间匹配,我会更新余额并将其写入NewMaster。如果它们不匹配,我会将原始帐户信息从OldMaster写入NewMaster并显示错误。
文件OldMaster中有4个帐户,文件Transactions中有4个帐户事务。出于某种原因,我的程序没有处理每个文件中的第四个(最后)帐户/事务,而NewMaster没有接收数据。
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;
}
答案 0 :(得分:2)
当您从输入文件中读取数据时,如果在循环中到达文件结尾,则循环将不会继续,因此不会写出新读取的数据。
如果你的情况我会做这样的事情:
do
{
inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
inTransaction >> accountNum >> dollarAmount;
if (inOldMaster.bad() || inTransaction.bad())
break;
// Your old if-statements
} while (inOldMaster.good() && inTransaction.good());