
November 11th, 2009, 04:32 AM
|
 |
Contributing User
|
|
Join Date: Dec 2007
Posts: 990

Time spent in forums: 1 Week 15 h 32 m 39 sec
Reputation Power: 3
|
|
Quote: | Originally Posted by Sherina
errors about having too few arguments.
int highGrade(int h[], int const size)
highest = highGrade(grades[ i ]);
|
Of course you get that error.
The correct syntax for that function call is highGrade(grades,i);. I also strongly suggest you move that call to somewhere more appropriate (no reason to do it for every value read).
In your code, you fail to initialize i. As such, it can contain any value. Your grades[ i ]= can then be writing to anywhere in memory. Hope it crashes rather than wipe your harddrive
Also, I would suggest you use vectors rather than array/length pairs, especially coupled with the incredibly naive array allocation method.
Using the standard library where possible, (instead of writing the supporting functions myself, likely adding bugs in doing so) and being functionally identical to your program (just without the bugs),
cpp Code:
Original
- cpp Code |
|
|
|
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <vector> #include <algorithm> // max_element #include <numeric> // accumulate (aka sum) using namespace std; int main () { ifstream myfile("a5.txt"); vector<int> grades; if (myfile.is_open()) { // Not changing the input method (read to string, atoi) because there is some input // it can accept (it will discard trailing letters, give 0 for words that start with letters) // that the usual method would not. string classGrades; myfile>>classGrades; while (myfile) // see http://www.cppreference.com/wiki/io/eof { grades.push_back(atoi (classGrades.c_str())); myfile>>classGrades; } cout<< *max_element(grades.begin(),grades.end()) <<endl; // returns iterator, prefix * to get the value itself float avg = accumulate(grades.begin(), grades.end(),0); avg /= grades.size(); cout<<fixed<<setprecision(2)<<avg<<endl; //cout<<grades[1]<<endl; // what was this line meant to do again? myfile.close(); } else cout<<"Error: File 'a5.txt' Not Found."<<endl; return 0; }
__________________
Quote: Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place. |
Last edited by MaHuJa : November 11th, 2009 at 05:19 AM.
Reason: Corrected the usual messageboard [i] formatting cockup
|