I program has a problem when reading from the file given:
robert arnold 85
hiedi turberville 90
sally hall 55
Program output is:
robert arnold 85
hiedi turberville 90
sally hall 55
3998044
The class average is: 999568
robert arnold 85 /Below class average/
hiedi turberville 90 /Below class average/
sally hall 55 /Below class average/
3998044 /Above class average/
The highest grade of the class belongs to: 3998044
As you can see the average is not computing right.
Here is the code:
Code:#include <cstdlib> #include <iostream> #include <fstream> #include <string> using namespace std; // declare function prototypes void infile_class_average(string firstName[], string lastName[], int grade[], int& averageGrade); void below_class_average(string firstName[], string lastName[],int grade[], int& averageGrade); void highest_score(string firstName[], string lastName[],int grade[], int& averageGrade); ///////////////////// main function //////////////////////////////////// int main(int argc, char *argv[]) { // local variables // int grade[50], averageGrade; string firstName[50], lastName[50]; infile_class_average(firstName, lastName, grade, averageGrade); below_class_average(firstName,lastName, grade, averageGrade); highest_score(firstName, lastName, grade, averageGrade); system("PAUSE"); return EXIT_SUCCESS; } ////////////////// function definition #1 infile_class_average ////////////////////////////////////// // get file input and calculate class average void infile_class_average(string firstName[] , string lastName[], int grade[], int& averageGrade) { ifstream infile; ofstream outfile; infile.open("C:\\testScores.txt"); outfile.open("testScoresOut.txt"); // intitialize integers to zero int count = 0, totalGrades = 0; int numOfStudents = 0; while(!infile.eof() && numOfStudents < 50) { //get student input first name , last name, and student grade numOfStudents++; infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents]; outfile << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << endl; cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << endl; // find the class average. count = count + 1; totalGrades = totalGrades + grade[numOfStudents]; /////////////////////////////////// } averageGrade = totalGrades / count; cout << "\n\nThe class average is: " << averageGrade << endl; infile.close(); } ////////////////// function definition #2 below class average //////////////////////////// // output the number of students that has a grade below the class average. void below_class_average(string firstName[], string lastName[], int grade[], int& averageGrade) { ifstream infile; ofstream outfile; infile.open("c:\\testScores.txt"); outfile.open("testScoresOut.txt"); int numOfStudents = 0; do { infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents]; numOfStudents++; if(averageGrade > grade[numOfStudents]) cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << " /Below class average/" << endl; if(averageGrade < grade[numOfStudents]) cout << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << grade[numOfStudents] << " /Above class average/" << endl; } while(!infile.eof() && numOfStudents < 50); infile.close(); } ////// function definition #3 highest score ///////////////////// void highest_score(string firstName[], string lastName[],int grade[], int& averageGrade) { ifstream infile; ofstream outfile; infile.open("c:\\testScores.txt"); outfile.open("testScoresOut.txt"); int largest = 0; int numOfStudents = 0; do { infile >> firstName[numOfStudents] >> lastName[numOfStudents] >> grade[numOfStudents]; numOfStudents++; if(largest < grade[numOfStudents]) largest = grade[numOfStudents]; } while(!infile.eof() && numOfStudents < 50); cout << "\n\nThe highest grade of the class belongs to: " << firstName[numOfStudents] << " " << lastName[numOfStudents] << " " << largest << endl; infile.close(); }
whst I believe is happening is in the first function class average it is looping one too many times.
if I initialize numOfStudents to 1 it will work but you get the wrong answer of course.
any suggestions?



LinkBack URL
About LinkBacks


