Thread: Loops again

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Loops again

    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
    Here is the source code:

    #include <iostream.h>
    #include <fstream.h>



    int main()
    {

    int count = 0;


    float par = 0;
    int score = 0;
    float hole = 00.0;

    fstream infile;
    infile.open( "c:\\golf.txt", ios::in );

    infile >>par>>score;


    cout <<"| Scorecard |"<<endl;
    cout <<"----------------------------------"<<endl;
    cout <<"| Hole" <<"|"<<" Par"<<" |"<<" Score"<<"| "<<"Result |"<<endl;
    cout <<"----------------------------------"<<endl;


    while( !infile.eof() )
    {

    hole += hole;
    count++;


    if (count <10)
    cout<<"| "<<count <<" "<<" | "<<par<<" | "<< score<<" |";
    else if (count >9)
    cout<<"| "<< count <<" "<<" | "<< par <<" | "<< score<<" |";

    if ((score) == par)
    cout <<" Par | "<<endl;
    else if(score == 1)
    cout <<" Hole in One!|"<<endl;
    else if (score +1 == par)
    cout <<" Birdie |"<<endl;
    else if (score +2 == par)
    cout <<" Eagle | "<<endl;
    else if (score +3 == par)
    cout <<" Dbl. eagle |"<<endl;
    else if (score -1 == par)
    cout <<" Bogey | "<<endl;
    else if (score -2 == par)
    cout <<" Dbl. bogey |"<<endl;
    else if (score -3 == par)
    cout <<" Triple bogey|"<<endl;
    infile >> par >> score;
    }

    cout <<"----------------------------------"<<endl;



    return 0;


    }

  2. #2
    Unregistered
    Guest
    might want to put some more thought into it before you post next time, this is not remotely difficult, but I'm going to help you out anyways.

    Add a new var, float I guess, to your main..we'll call it sum, remember to initialize it to 0.0


    Then, somewhere in your while loop (I'm not going to tell you where, you should be able to figure out atleast that much on your own), you'll need to increment the sum by the score just read in.

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, 12: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, 02:44 AM