Loops

This is a discussion on Loops within the C++ Programming forums, part of the General Programming Boards category; I made a program that reads golf scores and pars from a file. It then assigns the appropriate comment depending ...

  1. #1
    Unregistered
    Guest

    Loops

    I made a program that reads golf scores and pars from a file.
    It then assigns the appropriate comment depending on the score.
    Everything works fine, but I also need the sum of all the scores.
    How do I use a loop to read the file again and add the scores and return their sum.
    Any help would be appreciated
    Thankyou

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Without looking at the source it's a little difficult to help you out. Here is the syntax for a basic while loop.

    Code:
    while( condition ) // example: while( bDone == false )
    {
         // Execute
         // These
         // Statements
    }
    Alternatively, if you know the exact number of iterations try a for loop.
    Code:
    for( variable x; condition; increment )
    {
         // Do this
    }
    If you knew you wanted to loop 5 times you could do this. All this does is says while i is less than 5 keep executing code, and i++ simply increments the variable i AFTER the last line of code. If you want i to increment first, simply change i++ to ++i.

    Code:
    for( int i = 0; i < 5; i++ )
    {
         // Foo
    }
    Hope this helped a little.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 01:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21