What's wrong with my code when i calculate the average? I set it up so
average = sum of all scores divided by the number of scores.
But it keeps on returning a number like 5.634543-e306 and i don't know why it's doing that.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int ARRAY_SIZE = 0;
void score_reader ( ifstream& inFile, int&count);
void printScores(const double list[], int listSize);
double averageArray(const double list [], int listSize);
int main()
{
int count = 0;
int sub;
int category;
int student_number;
double average;
double scores[ARRAY_SIZE] ;
string filename;
ifstream inFile;
ifstream inData;
// Call function score_reader with scores1.txt
cout << "What's the name of the input file?: " ;
cin >> filename;
inFile.open(filename.c_str () );
if (!inFile)
{
cout << "NO!! THAT'S WRONG!! \n\n";
system ("pause");
return 1;
}
cout <<"Results:\n";
printScores (scores, ARRAY_SIZE);
score_reader (inFile, count);
cout << endl;
cout<<endl<<"The average test score is "<<average<<endl;
cout << "\nNumber of students in the class: " << count << "\n\n";
cout << "\n Class Average is =" << " "<< "\n\n";
cout << "\nNumber of students in each Category: ";
cout << endl;
cout << endl;
inFile >> category >> student_number;
while (inFile )
{
cout << category << " " <<student_number << endl;
scores[category -1] = scores[category -1] + student_number;
inFile >> category >> student_number;
}
inFile.close ();
system("pause");
return 0;
}
void score_reader ( ifstream& inFile, int&count)
{
string first_name;
string last_name;
int score1;
int score2;
int score3;
int score4;
int score5;
double scores[ARRAY_SIZE];
while (inFile) //if not at end of file, continue reading numbers
{
//cout<<score_reader<<endl; //print numbers to screen
inFile >> first_name;
inFile >> last_name;
inFile >> score1;
inFile >> score2;
inFile >> score3;
inFile >> score4;
inFile >> score5;
count++;
cout << last_name << "," << first_name <<"\t"<<score1 << "\t " << score2;
cout << "\t " << score3 << "\t " << score4 << "\t " << score5;
cout << endl;
}
inFile.close( ); //close file
assert(inFile.fail( ));
}
void printScores(const double list[], int listSize)
{
int index;
cout << "Name\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage**\tGrade " <<endl << endl;
for (index = 0; index < listSize; index++)
cout << index + 1 << "\t\t\t\t\t\t\t\t" << list[index] << endl;
}
double averageArray(const double list [], int listSize)
{
double total = 0;
for (int i = 0; i < listSize; i++)
total = total + list[i];
return (total/listSize);
}

