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