Jump to content
  • Advertisement
Sign in to follow this  
Enigma_

what's wrong with the loop?

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!