what's wrong with the loop?

Started by
3 comments, last by Enigma_ 14 years, 4 months ago
hello, I wrote up this simple code. void matchRecord (fstream & masterRecord, fstream & transRecord) { ofstream NewMaster ("newmast.dat", ios::out); // store output char firstName[15]; // store first name char lastName[15]; // store last name int accMaster; // account number from master file int accTrans; // account number from transaction file double balanceMaster; // balance from master file double balanceTrans; // balance from master file double balance; bool match = false; while (masterRecord >> accMaster >> firstName >> lastName >> balanceMaster) { transRecord.seekg(0); while (transRecord >> accTrans >> balanceTrans) { if (accTrans == accMaster) { balance = balanceMaster + balanceTrans; NewMaster << setw(10) << accMaster << setw(15) << firstName << setw(15) << lastName << setw(10) << balance << endl; match = true; } else match = false; } if (match == false) cout << "\n\nUnmatched transaction record for account number " << accTrans << ".\n\n"; NewMaster << setw(10) << accMaster << setw(15) << firstName << setw(15) << lastName << setw(10) << balanceMaster << endl; } } The inner while loop works when the outer while loops for the first time. But after that, it doesn't work anymore. any idea why this might be happening? thanks a lot. Appreciate any help :)
Advertisement
Your loop will always return false.

The only case it returns true, when accTrans == accMaster is true and the inner while is in the last loop.
You don't need that else match = false;
umm, the value of the boolean variable match does not matter here does it?

the loop is determined by
while (masterRecord >> accMaster >> firstName >> lastName >> balanceMaster)

and

while (transRecord >> accTrans >> balanceTrans)

isnt it?

My question is why the inner loop does not continue after the outer part loops through 1 time.
Try to use [ source ][ /source ] because it's very hard to read the code.
And how do you know that the inner loop does not continue after the outer part loops through 1 time?

Did you try that I told you?
hey szecs,

thanks man, but I figured out what mistake I was doing.

To test whether the loops were working correctly, I put a statement like

cout << accMaster;
cout << accTrans;

in to the second loop. so if it didnt print, it would mean something was not right. What I added into my code is:

transRecord.clear();
transRecord.seekg(0);

I put that in the between the first and second while loops. It seems to be working fine now.

I appreciate your help, thanks man :)

This topic is closed to new replies.

Advertisement