Need Help with a Sequential File

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 69
Reputation: Moporho is an unknown quantity at this point 
Solved Threads: 0
Moporho Moporho is offline Offline
Junior Poster in Training

Need Help with a Sequential File

 
0
  #1
Apr 30th, 2008
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

  1. #include <iostream>
  2. using std::cerr;
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. using std::fixed;
  7. using std::ios;
  8. using std::showpoint;
  9.  
  10. #include <fstream> // file stream
  11. using std::ifstream; // input file stream
  12. #include <iomanip>
  13. using std::setw;
  14. using std::setprecision;
  15.  
  16. #include <string>
  17. using std::string;
  18.  
  19. #include <cstdlib>
  20. using std::exit; // exit function prototype
  21. #include "golfers.h"
  22.  
  23. enum RequestType{LEGAL_SCORE=1, SCORE_PAR,AVG_HAND,AVG_SCORE,AT_HANDI,WORSE_HANDI,BETTER_HANDI,ILL_VALUE, END};
  24.  
  25. int main()
  26. {
  27. golfers g;
  28. // ifstream constructor opens the file
  29. ifstream inGolfersFile( "golf.dat" );
  30.  
  31. // exit program if ifstream could not open file
  32. if ( !inGolfersFile )
  33. {
  34. cerr << "File could not be opened" << endl;
  35. exit( 1 );
  36. } // end if
  37.  
  38. int request;
  39. int handi;
  40. int par;
  41. int score;
  42. int count;
  43.  
  44. request = g.getRequest();
  45. while(request !=END)
  46. {
  47. switch(request)
  48. {
  49. case LEGAL_SCORE:
  50. cout << "\nThe number of legal golf scores above par: \n";
  51. break;
  52. case SCORE_PAR:
  53. cout << "\nThe number of legal golf scores par or lower: \n";
  54. break;
  55. case AVG_HAND:
  56. cout << "\nThe average of all handicaps for the golfers: \n";
  57. g.calcAvgHandi(handi, count);
  58. break;
  59. case AVG_SCORE:
  60. cout << "\nThe average of all scores for the golfers: \n" ;
  61. g.calcAvgScore(score, count);
  62. break;
  63. case AT_HANDI:
  64. cout << "\nThe number of golfers who scored at their handicap: \n";
  65. break;
  66. case WORSE_HANDI:
  67. cout << "\nThe number of golfers who scored worse than their handicap: \n";
  68. break;
  69. case BETTER_HANDI:
  70. cout << "\nThe number of golfers who scored better than their handicap: \n";
  71. break;
  72. case ILL_VALUE:
  73. cout << "\nThe number of illegal values found: \n";
  74. break;
  75.  
  76. while(!inGolfersFile.eof())
  77. //display info
  78. if(g.getRequest())
  79. g.golfInfo( handi, par, score );
  80. inGolfersFile >> handi >> par >> score;
  81. }
  82. inGolfersFile.clear(); // reset
  83. inGolfersFile.seekg(0);
  84. request=g.getRequest();
  85. }
  86. return 0;
  87. } // end of main

  1. .h file
  2. class golfers
  3. {
  4. public:
  5. golfers(void);
  6. void golfInfo( float, int, float ); // prototype
  7. bool golfDisplay(int);
  8. int getRequest();
  9. float calcAvgHandi(int[],int);
  10. float calcAvgScore(int,int);
  11.  
  12.  
  13.  
  14.  
  15. private:
  16. int count;
  17. int handi;
  18. int par;
  19. int score;
  20. int request;
  21.  
  22. };

class golfers.cpp file

  1. #include "golfers.h"
  2. #include <iostream>
  3. using std::cerr;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::fixed;
  8. using std::ios;
  9. using std::left;
  10. using std::right;
  11. using std::showpoint;
  12.  
  13. #include <fstream> // file stream
  14. using std::ifstream; // input file stream
  15. #include <iomanip>
  16. using std::setw;
  17. using std::setprecision;
  18.  
  19. #include <string>
  20. using std::string;
  21.  
  22. #include <cstdlib>
  23. using std::exit; // exit function prototype
  24.  
  25. enum RequestType{LEGAL_SCORE=1, SCORE_PAR,AVG_HAND,AVG_SCORE,AT_HANDI,WORSE_HANDI,BETTER_HANDI,ILL_VALUE, END};
  26.  
  27.  
  28. golfers::golfers(void)
  29. {
  30. }
  31.  
  32. // display golfers information file
  33. void golfers::golfInfo( float handi, int par, float score )
  34. {
  35. cout << right << setw( 10 ) << setprecision(1) << handi << setw( 13 ) << par
  36. << setw( 13 ) << setprecision( 2 ) << right << score << endl;
  37. } // end function golfInfo
  38.  
  39. int golfers::getRequest()
  40. {
  41. int request;
  42.  
  43. cout << "\nEnter request" << endl
  44. << " 1 - List the number of legal golf score above par" << endl
  45. << " 2 - List the number of legal golf score par or lower" << endl
  46. << " 3 - Lists tha average of all handicaps for the golfers" << endl
  47. << " 4 - Lists tha average of all scores for the golfers" << endl
  48. << " 5 - Lists the number of golfers who scored at their handicap" << endl
  49. << " 6 - Lists the number of golfers who scored worse than their handicap" << endl
  50. << " 7 - Lists the number of golfers who scored better than their handicap" << endl
  51. << " 8 - Lists the number of illegal values found" << endl
  52. << " 9 - END" << fixed << showpoint;
  53.  
  54. do
  55. {
  56. cout << "\nEnter: ";
  57. cin >> request;
  58. }
  59. while (request < LEGAL_SCORE && request > END);
  60. return request;
  61. } // end function get Request
  62.  
  63.  
  64. bool golfers::golfDisplay(int request)
  65. {
  66. if(request==LEGAL_SCORE)
  67. return true;
  68. if(request==SCORE_PAR)
  69. return true;
  70. if(request==AVG_HAND)
  71. return true;
  72. if(request==AVG_SCORE)
  73. return true;
  74. if(request==AT_HANDI)
  75. return true;
  76. if(request==WORSE_HANDI)
  77. return true;
  78. if(request==BETTER_HANDI)
  79. return true;
  80. if(request==ILL_VALUE)
  81. return true;
  82.  
  83.  
  84. return false;
  85. }
  86.  
  87. float golfers::calcAvgHandi(int handi [], int count)
  88. {
  89. int sum=0;
  90. count = 0;
  91. float average;
  92. for (int i = 0; i < count; i++)
  93. sum = sum + handi[i];
  94. average = float(sum)/count;
  95. return average;
  96. }
  97. float golfers::calcAvgScore(int score[],int count)
  98. {
  99. int sum=0;
  100. count = 0;
  101. float average;
  102. for (int i = 0; i < count; i++)
  103. sum = sum + score[i];
  104. average = float(sum)/count;
  105. return average;
  106. }

errors
  1. >.\.cpp(69) : error C2664: 'golfers::calcAvgHandi' : cannot convert parameter 1 from 'int' to 'int []'
  2. 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
  3. 1>.\.cpp(91) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
  4. 1>.\.cpp(91) : warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
  5. 1>Generating Code...
  6. 1>Build log was saved at "file://c:\Users\moporho\Documents\Visual Studio 2005\Projects\\Debug\BuildLog.htm"
  7. 1> - 2 error(s), 2 warning(s)
  8. ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

dat file
  1. 12 72 87
  2. 11 71 81
  3. 6 70 78
  4. 19 70 89
  5. 38 71 109
  6. 35 72 110
  7. 1 72 73
  8. 8 72 81
  9. 9 72 83
  10. 5 71 76
  11. 4 72 76
  12. 14 71 88
  13. 20 72 90
  14. 22 72 97
  15. 31 70 103
  16. 34 72 108
  17. 22 72 96
  18. 23 73 96
  19. 21 71 92
  20. 17 72 90
  21. 16 72 85
  22. 15 71 85
  23. 12 71 83
  24. 11 70 83
  25. 10 71 84
  26. 9 72 81
  27. 8 69 77
  28. 0 72 71
  29. 25 72 98
  30. 24 72 96
  31. 29 71 100
  32. 33 66 99
  33. 31 71 104
  34. 1 62 60
  35. 0 62 59
  36. 14 72 85
  37. 14 72 85
  38. 14 72 83
  39. 14 72 84
  40. 14 72 89
  41. 15 72 90
  42. 15 72 91
  43. 16 72 90
  44. 16 72 89
  45. 14 72 79
  46. 14 72 78
  47. 15 72 85

Thanks,
M
Last edited by Ancient Dragon; Apr 30th, 2008 at 5:58 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help with a Sequential File

 
0
  #2
Apr 30th, 2008
line 57 main.cpp: g.calcAvgHandi(handi, count);
handi is an int, not an array. Similar problems with the other lines.
Last edited by Ancient Dragon; Apr 30th, 2008 at 6:46 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 69
Reputation: Moporho is an unknown quantity at this point 
Solved Threads: 0
Moporho Moporho is offline Offline
Junior Poster in Training

Re: Need Help with a Sequential File

 
0
  #3
Apr 30th, 2008
Help! This is my last project in C++ and I am so lost!!

My data is not being read and all my functions are not computing correctly. Oh heck, my functions are a total mess.

Please someone have a look and help me get this thing working correctly.
  1. 11 #include <iostream>
  2. 12 #include <iomanip>
  3. 13 using std::setw;
  4. 14 using std::setprecision;
  5. 15 #include <fstream> // access to file related functions and types
  6. 16 #include <cstdlib>
  7. 17
  8. 18 //assert contains routines that allow the programmer to make
  9. 19 //assertions that must be true for program execution to continue
  10. 20 #include <cassert>
  11. 21
  12. 22 using namespace std;
  13. 23
  14. 24 const int MAX_GOLFERS = 47; //1 student's scores are stored in a row
  15. 25 const int STATS = 3; //Each row has this many columns (scores)
  16. 26
  17. 27
  18. 28 void seeStats(int table[][STATS],
  19. 29 int &number_of_golfers,
  20. 30 int &handicaps,
  21. 31 int &par,
  22. 32 int &scores); //read data into a table
  23. 33
  24. 34 void print_a_row(int row[], int columns_to_print);
  25. 35
  26. 36
  27. 37 void PrintStats(int table[][STATS],
  28. 38
  29. 39
  30. 40
  31. 41 int scoresAbovePar(const int STATS[47], int handicaps, int scores, int par, int count);
  32. 42 int scoresAtOrPar(const int STATS[47], int handicaps, int scores, int par, int count);
  33. 43 float avgHand(const int STATS[], int handicaps, int count);
  34. 44 float avgScore(int table[], int scores, int count);
  35. 45 int scoreAtHand(const int STATS[47], int handicaps, int scores, int par, int count);
  36. 46 int scoreWorse(const int STATS[47], int handicaps, int scores, int par, int count);
  37. 47 int scoreBetter(const int STATS[47], int handicaps, int scores, int par, int count);
  38. 48 int illValue(const int STATS[47], int handicaps, int scores, int par, int count);
  39. 49
  40. 50 int main(void)
  41. 51 {
  42. 52 int table[MAX_GOLFERS][STATS]; // stats table
  43. 53 int number_of_golfers,
  44. 54 handicaps,
  45. 55 par,
  46. 56 scores;
  47. 57 int rows_to_print=47;
  48. 58 int columns_to_print=3;
  49. 59
  50. 60 // read stats
  51. 61 seeStats(table, number_of_golfers, handicaps, par, scores);
  52. 62 cout << "Golfer's Stats: \n\n";
  53. 63
  54. 64 PrintStats(table, rows_to_print,columns_to_print);
  55. 65 cout << "\nPress ENTER key to continue: \n";
  56. 66 cin.get();
  57. 67
  58. 68 cout << "1. - The number of legal golf scores above par is " << scoresAbovePar << endl << endl;
  59. 69
  60. 70 cout << "2 - The number of legal golf scores at or below par is " << scoresAtOrPar << endl << endl;
  61. 71
  62. 72 cout << "3. - The average of all handicaps for the golfers is " << setprecision(1) << avgHand << endl << endl;
  63. 73
  64. 74 cout << "4. - The average of all scores for the golfers is " << setprecision(2) << avgScore << endl << endl;
  65. 75
  66. 76 cout << "5. - The number of golfer who scored at their handicap is " << scoreAtHand << endl << endl;
  67. 77
  68. 78 cout << "6. - The number of golfers who scored worse than their handicap is " << scoreWorse << endl << endl;
  69. 79
  70. 80 cout << "7. - The number of golfers who scored better than their handicap is " << scoreBetter << endl << endl;
  71. 81
  72. 82 cout << "8. - The number of illegal values found is " << illValue << endl << endl;
  73. 83
  74. 84
  75. 85
  76. 86 return 0;
  77. 87 } // end of main
  78. 88
  79. 89
  80. 90 // function seeStats
  81. 91 void seeStats(int table[][STATS],
  82. 92 int &number_of_golfers,
  83. 93 int &handicaps,
  84. 94 int &par,
  85. 95 int &scores)
  86. 96 {
  87. 97 //Open external data file for reading
  88. 98 ifstream infile("golf.dat");
  89. 99 if (!infile.is_open()) {
  90. 100 cerr << "File could not be opened" << endl;
  91. 101 exit(1); // function in cstdlib
  92. 102 }
  93. 103 // read data into table
  94. 104 int row=0;
  95. 105 int column=0;
  96. 106
  97. 107 while (infile >> table[row][column]) {
  98. 108 if (infile.peek() != '\n') {
  99. 109 column++; //more scores exist on line, advance column
  100. 110 }
  101. 111 else { //end of line detected...
  102. 112 handicaps = column + 1; // set actual number of columns
  103. 113 column = 0; //start at beginning of next row
  104. 114 row++;
  105. 115 }
  106. 116 } // end of while
  107. 117 number_of_golfers = row; // set actual number of rows
  108. 118 } // end of seeStats
  109. 119
  110. 120 //print one row of the table.
  111. 121 void print_a_row(int row[], int columns_to_print)
  112. 122 {
  113. 123 for (int j = 0; j < columns_to_print; j++)
  114. 124 cout << setw(4) << row[j];
  115. 125 }
  116. 126
  117. 127 //function PrintStats
  118. 128 void PrintStats(int table[][STATS],
  119. 129 int rows_to_print,
  120. 130 int columns_to_print)
  121. 131 {
  122. 132 for (int i = 0; i < rows_to_print; i++) {
  123. 133 print_a_row(table[i], columns_to_print);
  124. 134 cout << endl;
  125. 135 }
  126. 136 } // end of PrintStats
  127. 137
  128. 138 int scoresAbovePar(const int STATS[47], int handicaps, int scores, int par, int count)
  129. 139 {
  130. 140 int i;
  131. 141 i = scores - handicaps;
  132. 142 i = i- par;
  133. 143
  134. 144 if (i >= handicaps)
  135. 145 count++;
  136. 146
  137. 147 return count;
  138. 148 }
  139. 149 int scoresAtOrPar(const int STATS[47], int handicaps, int scores, int par, int count)
  140. 150 {
  141. 151 int i;
  142. 152 i = scores - handicaps;
  143. 153 i = i- par;
  144. 154
  145. 155 if (i <= handicaps)
  146. 156 count++;
  147. 157
  148. 158 return count;
  149. 159 }
  150. 160
  151. 161 float avgHand(const int STATS[47], int handicaps, int count)
  152. 162 {
  153. 163 int sum=0;
  154. 164 float avg;
  155. 165
  156. 166 for(int i=0; i < count; i++)
  157. 167 sum = sum + STATS[i];
  158. 168 avg = float(sum)/count;
  159. 169
  160. 170 return avg;
  161. 171 } // end avgHand
  162. 172
  163. 173 float avgScore(int table[], int scores, int count)
  164. 174 {
  165. 175 int sum=0;
  166. 176 float avg;
  167. 177
  168. 178 for(int i=0; i < count; i++)
  169. 179 sum=sum+table[i];
  170. 180 avg = float(sum)/count;
  171. 181
  172. 182 return avg; // end avgScore
  173. 183 }
  174. 184
  175. 185 int scoreAtHand(const int STATS[47], int handicaps, int scores, int par, int count)
  176. 186 {
  177. 187
  178. 188 if (scores == scores-handicaps)
  179. 189 count ++;
  180. 190 return count;
  181. 191 }
  182. 192 int scoreWorse(const int STATS[47], int handicaps, int scores, int par, int count)
  183. 193 {
  184. 194
  185. 195 if (scores < scores-handicaps)
  186. 196 count ++;
  187. 197 return count;
  188. 198 }
  189. 199 int scoreBetter(const int STATS[47], int handicaps, int scores, int par, int count)
  190. 200 {
  191. 201
  192. 202 if (scores > scores-handicaps)
  193. 203 count ++;
  194. 204 return count;
  195. 205 }
  196. 206
  197. 207 int illValue(const int STATS[47], int handicaps, int scores, int par, int count)
  198. 208 {
  199. 209 if (handicaps < 0 || handicaps > 36 || par < 62 || par >72 || scores < 60 || scores > 120)
  200. 210
  201. 211 count++;
  202. 212
  203. 213 return count;
  204. 214 }

the data file is above.

here is the output I am getting
  1. Golfer's Stats:
  2.  
  3. 12 72 87
  4. 11 71 81
  5. 6 70 78
  6. 19 70 89
  7. 38 71 109
  8. 35 72 110
  9. 1 72 73
  10. 8 72 81
  11. 9 72 83
  12. 5 71 76
  13. 4 72 76
  14. 14 71 88
  15. 20 72 90
  16. 22 72 97
  17. 31 70 103
  18. 34 72 108
  19. 22 72 96
  20. 23 73 96
  21. 21 71 92
  22. 17 72 90
  23. 16 72 85
  24. 15 71 85
  25. 12 71 83
  26. 11 70 83
  27. 10 71 84
  28. 9 72 81
  29. 8 69 77
  30. 0 72 71
  31. 25 72 98
  32. 24 72 96
  33. 29 71 100
  34. 33 66 99
  35. 31 71 104
  36. 1 62 60
  37. 0 62 59
  38. 14 72 85
  39. 14 72 85
  40. 14 72 83
  41. 14 72 84
  42. 14 72 89
  43. 15 72 90
  44. 15 72 91
  45. 16 72 90
  46. 16 72 89
  47. 14 72 79
  48. 14 72 78
  49. 15 72 85
  50.  
  51. Press ENTER key to continue:
  52.  
  53. 1. - The number of legal golf scores above par is 004111B8
  54.  
  55. 2 - The number of legal golf scores at or below par is 00411145
  56.  
  57. 3. - The average of all handicaps for the golfers is 004110DC
  58.  
  59. 4. - The average of all scores for the golfers is 004111C2
  60.  
  61. 5. - The number of golfer who scored at their handicap is 004111BD
  62.  
  63. 6. - The number of golfers who scored worse than their handicap is 00411046
  64.  
  65. 7. - The number of golfers who scored better than their handicap is 004111D6
  66.  
  67. 8. - The number of illegal values found is 004110EB
  68.  
  69. Press any key to continue . . .
  70.  

Thank you,
M~
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help with a Sequential File

 
0
  #4
Apr 30th, 2008
Please don't manually add line numbers because compilers don't like them. The code tags will insert line numbers for you. Please repost code without manual line numbers

[code=cplusplus]
// put your code here
[/code]
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 69
Reputation: Moporho is an unknown quantity at this point 
Solved Threads: 0
Moporho Moporho is offline Offline
Junior Poster in Training

Re: Need Help with a Sequential File

 
0
  #5
Apr 30th, 2008
Sorry, now that I know I will not ever do that again. I am sorry.

  1. #include <iostream>
  2. #include <iomanip>
  3. using std::setw;
  4. using std::setprecision;
  5. #include <fstream> // access to file related functions and types
  6. #include <cstdlib>
  7.  
  8. //assert contains routines that allow the programmer to make
  9. //assertions that must be true for program execution to continue
  10. #include <cassert>
  11.  
  12. using namespace std;
  13.  
  14. const int MAX_GOLFERS = 47; //1 student's scores are stored in a row
  15. const int STATS = 3; //Each row has this many columns (scores)
  16.  
  17.  
  18. void seeStats(int table[][STATS],
  19. int &number_of_golfers,
  20. int &handicaps,
  21. int &par,
  22. int &scores); //read data into a table
  23.  
  24. void print_a_row(int row[], int columns_to_print);
  25.  
  26.  
  27. void PrintStats(int table[][STATS],
  28. int rows_to_print,
  29. int columns_to_print); // print golfers' stats in a table
  30.  
  31. int scoresAbovePar(const int STATS[47], int handicaps, int scores, int par, int count);
  32. int scoresAtOrPar(const int STATS[47], int handicaps, int scores, int par, int count);
  33. float avgHand(const int STATS[], int handicaps, int count);
  34. float avgScore(int table[], int scores, int count);
  35. int scoreAtHand(const int STATS[47], int handicaps, int scores, int par, int count);
  36. int scoreWorse(const int STATS[47], int handicaps, int scores, int par, int count);
  37. int scoreBetter(const int STATS[47], int handicaps, int scores, int par, int count);
  38. int illValue(const int STATS[47], int handicaps, int scores, int par, int count);
  39.  
  40. int main(void)
  41. {
  42. int table[MAX_GOLFERS][STATS]; // stats table
  43. int number_of_golfers,
  44. handicaps,
  45. par,
  46. scores;
  47. int rows_to_print=47;
  48. int columns_to_print=3;
  49.  
  50. // read stats
  51. seeStats(table, number_of_golfers, handicaps, par, scores);
  52. cout << "Golfer's Stats: \n\n";
  53.  
  54. PrintStats(table, rows_to_print,columns_to_print);
  55. cout << "\nPress ENTER key to continue: \n";
  56. cin.get();
  57.  
  58. cout << "1. - The number of legal golf scores above par is " << scoresAbovePar << endl << endl;
  59.  
  60. cout << "2 - The number of legal golf scores at or below par is " << scoresAtOrPar << endl << endl;
  61.  
  62. cout << "3. - The average of all handicaps for the golfers is " << setprecision(1) << avgHand << endl << endl;
  63.  
  64. cout << "4. - The average of all scores for the golfers is " << setprecision(2) << avgScore << endl << endl;
  65.  
  66. cout << "5. - The number of golfer who scored at their handicap is " << scoreAtHand << endl << endl;
  67.  
  68. cout << "6. - The number of golfers who scored worse than their handicap is " << scoreWorse << endl << endl;
  69.  
  70. cout << "7. - The number of golfers who scored better than their handicap is " << scoreBetter << endl << endl;
  71.  
  72. cout << "8. - The number of illegal values found is " << illValue << endl << endl;
  73.  
  74.  
  75.  
  76. return 0;
  77. } // end of main
  78.  
  79.  
  80. // function seeStats
  81. void seeStats(int table[][STATS],
  82. int &number_of_golfers,
  83. int &handicaps,
  84. int &par,
  85. int &scores)
  86. {
  87. //Open external data file for reading
  88. ifstream infile("golf.dat");
  89. if (!infile.is_open()) {
  90. cerr << "File could not be opened" << endl;
  91. exit(1); // function in cstdlib
  92. }
  93. // read data into table
  94. int row=0;
  95. int column=0;
  96.  
  97. while (infile >> table[row][column]) {
  98. if (infile.peek() != '\n') {
  99. column++; //more scores exist on line, advance column
  100. }
  101. else { //end of line detected...
  102. handicaps = column + 1; // set actual number of columns
  103. column = 0; //start at beginning of next row
  104. row++;
  105. }
  106. } // end of while
  107. number_of_golfers = row; // set actual number of rows
  108. } // end of seeStats
  109.  
  110. //print one row of the table.
  111. void print_a_row(int row[], int columns_to_print)
  112. {
  113. for (int j = 0; j < columns_to_print; j++)
  114. cout << setw(4) << row[j];
  115. }
  116.  
  117. //function PrintStats
  118. void PrintStats(int table[][STATS],
  119. int rows_to_print,
  120. int columns_to_print)
  121. {
  122. for (int i = 0; i < rows_to_print; i++) {
  123. print_a_row(table[i], columns_to_print);
  124. cout << endl;
  125. }
  126. } // end of PrintStats
  127.  
  128. int scoresAbovePar(const int STATS[47], int handicaps, int scores, int par, int count)
  129. {
  130. int i;
  131. i = scores - handicaps;
  132. i = i- par;
  133.  
  134. if (i >= handicaps)
  135. count++;
  136.  
  137. return count;
  138. }
  139. int scoresAtOrPar(const int STATS[47], int handicaps, int scores, int par, int count)
  140. {
  141. int i;
  142. i = scores - handicaps;
  143. i = i- par;
  144.  
  145. if (i <= handicaps)
  146. count++;
  147.  
  148. return count;
  149. }
  150.  
  151. float avgHand(const int STATS[47], int handicaps, int count)
  152. {
  153. int sum=0;
  154. float avg;
  155.  
  156. for(int i=0; i < count; i++)
  157. sum = sum + STATS[i];
  158. avg = float(sum)/count;
  159.  
  160. return avg;
  161. } // end avgHand
  162.  
  163. float avgScore(int table[], int scores, int count)
  164. {
  165. int sum=0;
  166. float avg;
  167.  
  168. for(int i=0; i < count; i++)
  169. sum=sum+table[i];
  170. avg = float(sum)/count;
  171.  
  172. return avg; // end avgScore
  173. }
  174.  
  175. int scoreAtHand(const int STATS[47], int handicaps, int scores, int par, int count)
  176. {
  177.  
  178. if (scores == scores-handicaps)
  179. count ++;
  180. return count;
  181. }
  182. int scoreWorse(const int STATS[47], int handicaps, int scores, int par, int count)
  183. {
  184.  
  185. if (scores < scores-handicaps)
  186. count ++;
  187. return count;
  188. }
  189. int scoreBetter(const int STATS[47], int handicaps, int scores, int par, int count)
  190. {
  191.  
  192. if (scores > scores-handicaps)
  193. count ++;
  194. return count;
  195. }
  196.  
  197. int illValue(const int STATS[47], int handicaps, int scores, int par, int count)
  198. {
  199. if (handicaps < 0 || handicaps > 36 || par < 62 || par >72 || scores < 60 || scores > 120)
  200.  
  201. count++;
  202.  
  203. return count;
  204. }

M~
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help with a Sequential File

 
0
  #6
Apr 30th, 2008
  1. cout << "1. - The number of legal golf scores above par is " << scoresAbovePar << endl << endl;
  2.  
  3. cout << "2 - The number of legal golf scores at or below par is " << scoresAtOrPar << endl << endl;
  4.  
  5. cout << "3. - The average of all handicaps for the golfers is " << setprecision(1) << avgHand << endl << endl;
  6.  
  7. cout << "4. - The average of all scores for the golfers is " << setprecision(2) << avgScore << endl << endl;
  8.  
  9. cout << "5. - The number of golfer who scored at their handicap is " << scoreAtHand << endl << endl;
  10.  
  11. cout << "6. - The number of golfers who scored worse than their handicap is " << scoreWorse << endl << endl;
  12.  
  13. cout << "7. - The number of golfers who scored better than their handicap is " << scoreBetter << endl << endl;
  14.  
  15. cout << "8. - The number of illegal values found is " << illValue << endl << endl;

scoresAbovePar is a function, not a data element. All you are doing in the lines posted above is printing the address of those functions. You have to call them just as any other function.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 69
Reputation: Moporho is an unknown quantity at this point 
Solved Threads: 0
Moporho Moporho is offline Offline
Junior Poster in Training

Re: Need Help with a Sequential File

 
0
  #7
Apr 30th, 2008
I am sorry, I am not clear. How would a call to the function look? I am muttled up.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help with a Sequential File

 
0
  #8
Apr 30th, 2008
Like this:

cout << "1. - The number of legal golf scores above par is " << scoresAbovePar( <parameter list here> ) << endl << endl;
Last edited by Ancient Dragon; Apr 30th, 2008 at 9:28 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 69
Reputation: Moporho is an unknown quantity at this point 
Solved Threads: 0
Moporho Moporho is offline Offline
Junior Poster in Training

Re: Need Help with a Sequential File

 
0
  #9
Apr 30th, 2008
I can not get the functions to produce the correct data. Can someone make suggestions?

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using std::setw;
  5. using std::setprecision;
  6. #include <fstream>
  7. #include <cstdlib>
  8.  
  9.  
  10. #include <cassert>
  11.  
  12. using namespace std;
  13.  
  14. const int MAX_GOLFERS = 47;
  15. const int STATS = 3;
  16.  
  17.  
  18. void seeStats(int table[][STATS],
  19. int &number_of_golfers,
  20. int &handicaps,
  21. int &par,
  22. int &scores); //read data into a table
  23.  
  24. void print_a_row(int row[], int columns_to_print);
  25.  
  26.  
  27. void PrintStats(int table[][STATS],
  28. int rows_to_print,
  29. int columns_to_print); // print golfers' stats in a table
  30.  
  31. int scoresAbovePar(const int table[STATS], int handicaps,int par, int scores,int count);
  32. int scoresAtOrPar(const int table[STATS], int handicaps, int scores, int par, int count);
  33. float avgHand(const int table[STATS], int handicaps, int count);
  34. float avgScore(const int table[STATS], int scores, int count);
  35. int scoreAtHand(const int table[STATS], int handicaps, int scores, int par, int count);
  36. int scoreWorse(const int table[STATS], int handicaps, int scores, int par, int count);
  37. int scoreBetter(const int table[STATS], int handicaps, int scores, int par, int count);
  38. int illValue(const int table[STATS], int handicaps, int scores, int par, int count);
  39.  
  40. int main(void)
  41. {
  42. int table[MAX_GOLFERS][STATS]; // stats table
  43. int number_of_golfers,
  44. handicaps,
  45. par,
  46. scores;
  47. int rows_to_print=47;
  48. int columns_to_print=3;
  49. int count=0;
  50.  
  51. // read stats
  52. seeStats(table, number_of_golfers, handicaps, par, scores);
  53. cout << "Golfer's Stats: \n\n";
  54.  
  55. PrintStats(table, rows_to_print,columns_to_print);
  56. cout << "\nPress ENTER key to continue: \n";
  57. cin.get();
  58.  
  59. cout << "1. - The number of legal golf scores above par is " << scoresAbovePar(table[STATS], handicaps,par, scores,count) << endl << endl;
  60.  
  61.  
  62. cout << "2 - The number of legal golf scores at or below par is " << scoresAtOrPar(table[STATS], handicaps, scores, par, count) << endl << endl;
  63.  
  64. cout << "3. - The average of all handicaps for the golfers is " << setprecision(1) << avgHand(table[STATS], handicaps, count) << endl << endl;
  65.  
  66. cout << "4. - The average of all scores for the golfers is " << setprecision(2) << avgScore(table[STATS], scores, count) << endl << endl;
  67.  
  68. cout << "5. - The number of golfer who scored at their handicap is " << scoreAtHand(table[STATS], handicaps, scores, par, count) << endl << endl;
  69.  
  70. cout << "6. - The number of golfers who scored worse than their handicap is " << scoreWorse(table[STATS], handicaps, scores, par, count) << endl << endl;
  71.  
  72. cout << "7. - The number of golfers who scored better than their handicap is " << scoreBetter(table[STATS], handicaps, scores, par, count) << endl << endl;
  73.  
  74. cout << "8. - The number of illegal values found is " << illValue(table[STATS], handicaps, scores, par, count) << endl << endl;
  75.  
  76.  
  77.  
  78. return 0;
  79. } // end of main
  80.  
  81.  
  82. // function seeStats
  83. void seeStats(int table[][STATS],
  84. int &number_of_golfers,
  85. int &handicaps,
  86. int &par,
  87. int &scores)
  88. {
  89. //Open external data file for reading
  90. ifstream infile("golf.dat");
  91. if (!infile.is_open()) {
  92. cerr << "File could not be opened" << endl;
  93. exit(1); // function in cstdlib
  94. }
  95. // read data into table
  96. int row=0;
  97. int column=0;
  98.  
  99. while (infile >> table[row][column]) {
  100. if (infile.peek() != '\n') {
  101. column++; //more scores exist on line, advance column
  102. }
  103. else { //end of line detected...
  104. handicaps = column + 1; // set actual number of columns
  105. column = 0; //start at beginning of next row
  106. row++;
  107. }
  108. } // end of while
  109. number_of_golfers = row; // set actual number of rows
  110. } // end of seeStats
  111.  
  112. //print one row of the table.
  113. void print_a_row(int row[], int columns_to_print)
  114. {
  115. for (int j = 0; j < columns_to_print; j++)
  116. cout << setw(4) << row[j];
  117. }
  118.  
  119. //function PrintStats
  120. void PrintStats(int table[][STATS],
  121. int rows_to_print,
  122. int columns_to_print)
  123. {
  124. for (int i = 0; i < rows_to_print; i++) {
  125. print_a_row(table[i], columns_to_print);
  126. cout << endl;
  127. }
  128. } // end of PrintStats
  129.  
  130. int scoresAbovePar(const int table[STATS], int handicaps,int par, int scores,int count)
  131. {
  132. int i;
  133. i = scores - handicaps;
  134. i = i- par;
  135.  
  136. if (i >= handicaps)
  137. count++;
  138.  
  139. return count;
  140. }
  141. int scoresAtOrPar(const int table[STATS], int handicaps, int scores, int par, int count)
  142. {
  143. int i;
  144. i = scores - handicaps;
  145. i = i- par;
  146.  
  147. if (i <= handicaps)
  148. count++;
  149.  
  150. return count;
  151. }
  152.  
  153. float avgHand(const int table[STATS], int handicaps, int count)
  154. {
  155. int sum=0;
  156. float avg;
  157.  
  158. for(int i=0; i < count; i++)
  159. sum = sum + table[i];
  160. avg = float(sum)/count;
  161.  
  162. return avg;
  163. } // end avgHand
  164.  
  165. float avgScore(const int table[STATS], int scores, int count)
  166. {
  167. int sum=0;
  168. float avg;
  169.  
  170. for(int i=0; i < count; i++)
  171. sum=sum+table[i];
  172. avg = float(sum)/count;
  173.  
  174. return avg; // end avgScore
  175. }
  176.  
  177. int scoreAtHand(const int table[STATS], int handicaps, int scores, int par, int count)
  178. {
  179.  
  180. if (scores == scores-handicaps)
  181. count ++;
  182. return count;
  183. }
  184. int scoreWorse(const int table[STATS], int handicaps, int scores, int par, int count)
  185. {
  186.  
  187. if (scores < scores-handicaps)
  188. count ++;
  189. return count;
  190. }
  191. int scoreBetter(const int table[STATS], int handicaps, int scores, int par, int count)
  192. {
  193.  
  194. if (scores > scores-handicaps)
  195. count ++;
  196. return count;
  197. }
  198.  
  199. int illValue(const int table[STATS], int handicaps, int scores, int par, int count)
  200. {
  201. if (handicaps < 0 || handicaps > 36 || par < 62 || par >72 || scores < 60 || scores > 120)
  202.  
  203. count++;
  204.  
  205. return count;
  206. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need Help with a Sequential File

 
0
  #10
Apr 30th, 2008
line 59: scoresAbovePar(table[STATS], handicaps,par, scores,count)

The first parameter is incorrect. Here's the correction:
scoresAbovePar(table, handicaps,par, scores,count)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 880 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC