Hi, I need help with a Golf Stats Program I am trying to complete. It is a dat file. My averaging functions are not working. Can someone suggest what I can do to correct the problems with both averaging functions?
main
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::ios;
using std::showpoint;
#include <fstream> // file stream
using std::ifstream; // input file stream
#include <iomanip>
using std::setw;
using std::setprecision;
#include <string>
using std::string;
#include <cstdlib>
using std::exit; // exit function prototype
#include "golfers.h"
enum RequestType{LEGAL_SCORE=1, SCORE_PAR,AVG_HAND,AVG_SCORE,AT_HANDI,WORSE_HANDI,BETTER_HANDI,ILL_VALUE, END};
int main()
{
golfers g;
// ifstream constructor opens the file
ifstream inGolfersFile( "golf.dat" );
// exit program if ifstream could not open file
if ( !inGolfersFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
int request;
int handi;
int par;
int score;
int count;
request = g.getRequest();
while(request !=END)
{
switch(request)
{
case LEGAL_SCORE:
cout << "\nThe number of legal golf scores above par: \n";
break;
case SCORE_PAR:
cout << "\nThe number of legal golf scores par or lower: \n";
break;
case AVG_HAND:
cout << "\nThe average of all handicaps for the golfers: \n";
g.calcAvgHandi(handi, count);
break;
case AVG_SCORE:
cout << "\nThe average of all scores for the golfers: \n" ;
g.calcAvgScore(score, count);
break;
case AT_HANDI:
cout << "\nThe number of golfers who scored at their handicap: \n";
break;
case WORSE_HANDI:
cout << "\nThe number of golfers who scored worse than their handicap: \n";
break;
case BETTER_HANDI:
cout << "\nThe number of golfers who scored better than their handicap: \n";
break;
case ILL_VALUE:
cout << "\nThe number of illegal values found: \n";
break;
while(!inGolfersFile.eof())
//display info
if(g.getRequest())
g.golfInfo( handi, par, score );
inGolfersFile >> handi >> par >> score;
}
inGolfersFile.clear(); // reset
inGolfersFile.seekg(0);
request=g.getRequest();
}
return 0;
} // end of main
.h file
class golfers
{
public:
golfers(void);
void golfInfo( float, int, float ); // prototype
bool golfDisplay(int);
int getRequest();
float calcAvgHandi(int[],int);
float calcAvgScore(int,int);
private:
int count;
int handi;
int par;
int score;
int request;
};
class golfers.cpp file
#include "golfers.h"
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;
#include <fstream> // file stream
using std::ifstream; // input file stream
#include <iomanip>
using std::setw;
using std::setprecision;
#include <string>
using std::string;
#include <cstdlib>
using std::exit; // exit function prototype
enum RequestType{LEGAL_SCORE=1, SCORE_PAR,AVG_HAND,AVG_SCORE,AT_HANDI,WORSE_HANDI,BETTER_HANDI,ILL_VALUE, END};
golfers::golfers(void)
{
}
// display golfers information file
void golfers::golfInfo( float handi, int par, float score )
{
cout << right << setw( 10 ) << setprecision(1) << handi << setw( 13 ) << par
<< setw( 13 ) << setprecision( 2 ) << right << score << endl;
} // end function golfInfo
int golfers::getRequest()
{
int request;
cout << "\nEnter request" << endl
<< " 1 - List the number of legal golf score above par" << endl
<< " 2 - List the number of legal golf score par or lower" << endl
<< " 3 - Lists tha average of all handicaps for the golfers" << endl
<< " 4 - Lists tha average of all scores for the golfers" << endl
<< " 5 - Lists the number of golfers who scored at their handicap" << endl
<< " 6 - Lists the number of golfers who scored worse than their handicap" << endl
<< " 7 - Lists the number of golfers who scored better than their handicap" << endl
<< " 8 - Lists the number of illegal values found" << endl
<< " 9 - END" << fixed << showpoint;
do
{
cout << "\nEnter: ";
cin >> request;
}
while (request < LEGAL_SCORE && request > END);
return request;
} // end function get Request
bool golfers::golfDisplay(int request)
{
if(request==LEGAL_SCORE)
return true;
if(request==SCORE_PAR)
return true;
if(request==AVG_HAND)
return true;
if(request==AVG_SCORE)
return true;
if(request==AT_HANDI)
return true;
if(request==WORSE_HANDI)
return true;
if(request==BETTER_HANDI)
return true;
if(request==ILL_VALUE)
return true;
return false;
}
float golfers::calcAvgHandi(int handi [], int count)
{
int sum=0;
count = 0;
float average;
for (int i = 0; i < count; i++)
sum = sum + handi[i];
average = float(sum)/count;
return average;
}
float golfers::calcAvgScore(int score[],int count)
{
int sum=0;
count = 0;
float average;
for (int i = 0; i < count; i++)
sum = sum + score[i];
average = float(sum)/count;
return average;
}
errors
>.\.cpp(69) : error C2664: 'golfers::calcAvgHandi' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>.\.cpp(91) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>.\.cpp(91) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>Generating Code...
1>Build log was saved at "file://c:\Users\moporho\Documents\Visual Studio 2005\Projects\\Debug\BuildLog.htm"
1> - 2 error(s), 2 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
dat file
12 72 87
11 71 81
6 70 78
19 70 89
38 71 109
35 72 110
1 72 73
8 72 81
9 72 83
5 71 76
4 72 76
14 71 88
20 72 90
22 72 97
31 70 103
34 72 108
22 72 96
23 73 96
21 71 92
17 72 90
16 72 85
15 71 85
12 71 83
11 70 83
10 71 84
9 72 81
8 69 77
0 72 71
25 72 98
24 72 96
29 71 100
33 66 99
31 71 104
1 62 60
0 62 59
14 72 85
14 72 85
14 72 83
14 72 84
14 72 89
15 72 90
15 72 91
16 72 90
16 72 89
14 72 79
14 72 78
15 72 85
Thanks,
M