How to stop a loop before it gets to the last one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nuken
    New Member
    • Oct 2010
    • 16

    How to stop a loop before it gets to the last one

    This loop goes through all the lines in the file but there are only six lines and it has seven outputs
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Well, try using a loop counter in addition to your eof condition:
    Code:
    int count = 1;
    while ( (count < 7) && !cin.eof() )
    {
      count ++;
    . . .

    Comment

    • nuken
      New Member
      • Oct 2010
      • 16

      #3
      that would work but there could be more or less than 7 in the file.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        So what is the alternate terminiation condition?

        If you know how to detect the state, you can either detect at the top of the file, or you can insert this code following line 25 of your example:
        Code:
             if (cin.eof())
                break;
        Luck!

        Comment

        • nuken
          New Member
          • Oct 2010
          • 16

          #5
          Parse error on line 55: break statements are only allowed within switch statements;
          breaking out of a loop is not supported.

          I added that and it gave me this error.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            You're using C#, aren't you?

            Ok, so you need to build a guard around the while loop at line 26. You can do it the easy way and write:
            Code:
                    while ( (!cin.eof()) && (count <= scores_in_section) )
            Or you can guard the whole thing with an "if" statement:
            Code:
                    if (!cin.eof())
                    {
                      while ( count <= scores_in_section ) 
                      {
                      . . .
                      }
                    }

            Comment

            • nuken
              New Member
              • Oct 2010
              • 16

              #7
              Actually im using C++, but heres the whole code because i think the problem is the first if statement is set to 0at the end and thats why it outputs that seventh one:

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                After line 37, try inserting
                Code:
                if (!cin.eof()) {
                And after line 106 insert
                Code:
                }
                That should effectively mask out the section logic when EOF occurs on input.

                Comment

                • nuken
                  New Member
                  • Oct 2010
                  • 16

                  #9
                  Dude thankyou soo much!!! You are awesome

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    No worries, nuken. Just be sure to help others when you can.

                    Luck!

                    Comment

                    Working...