Thread: do loop repeats one time too many

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    32

    do loop repeats one time too many

    I use do{}while; loops alot in my programs, so I don't have to repeat steps again and again, my problem is, I call information from a flat file and set my condition to kill the do loop when it reaches the end of the file or my string comparisons don't match, however, all my programs seem to loop one more time after it meets the terminate loop condition and this is causing bad output from my program, it repeats the same output twice, the output is correct but repeated.... This is frustrating the heck out of me,

    is there a better looping procedure that I should use or a better way of flagging the termination point.

    I should add, the input from flat files is written by other programs using standard ifstream principles.....

    Any help would be appreciated .... Thanks
    I'd like to put Murphy and his laws in headlock sometimes!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    Try using a while loop

    Code:
    while ( condition)
    {
        //code to loop
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you need to know exactly how the file is set up and then tailor your loop accordingly.

    One of the causes for the type of behaviour you experience is some form of white space after the last data input and the EOF marker in the file.

    The other common cause is using a loop that doesn't stop until reading beyond EOF, not at EOF. This can be further broken down into cases where the conditional is inappropriate and where the loop "body" is inappropriate.

    Depending on the situation you may have experienced all the above type problems. Hard to say which one, if any, you have without seeing some examples.

    BTW: I use do/while only when I know for sure I have to do something at least once. In your case, what if the file is empty? You probably wouldn't want to do much, depending on the circumstances of course. Yet, with a do/while the body of the loop must be done at least once, which may cause a problem. Just a word of caution.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    LOOP NOTES:

    A do-while-loop tests the condition at the end.
    A while-loop tests at the start
    A for-loop tests at the start


    You can also use if() with the following:

    break; //will immediately exit the loop
    continue: //will immediatly start the loop over
    return; //immediatly exit the function (and the loop).

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>all my programs seem to loop one more time after it meets the terminate loop condition
    You've probably fallen into this trap (the example there is C, but the principle still remains)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    Ah ha, I will have to concentrate on the while loop aspect, many of the replies, which I appreciate greatly, hit on a good point, if I happen to have an empty file a do loop makes for poor logic.

    Also, DougDBug
    you mentioned some interesting functions that could be used in with the if() condition, return I am familiar with using, but continue and break were new, Have you had any cases where the while loop used similar functions?

    Hammer,
    you hit the nail right on the head with your trap link, I read that, it was pretty much exactly my problem, and I am trying to manipulate my ctt code with similar corrections using the while loop, bare with me, still a bit green....
    I'd like to put Murphy and his laws in headlock sometimes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  2. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM