-
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;
}
-
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.