|
-
November 2nd, 2009, 09:42 PM
#1
Weird average
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);
}
-
November 2nd, 2009, 09:45 PM
#2
Re: Weird average
const int ARRAY_SIZE = 0;
Errr....zero?
-
November 2nd, 2009, 09:56 PM
#3
Re: Weird average
Well, i'm going to be using more than one file that have different amounts of scores and people in it.
One has 17 people, another has 4 and another has 12.
So i thought that since it was like that there it can't equal anything but zero.
-
November 2nd, 2009, 11:27 PM
#4
Re: Weird average
 Originally Posted by hougigo
Well, i'm going to be using more than one file that have different amounts of scores and people in it.
One has 17 people, another has 4 and another has 12.
You should use a vector in this case.
-
November 5th, 2009, 11:30 AM
#5
Re: Weird average
You never assign anything to 'average'. It's just trash on the stack.
-
November 6th, 2009, 04:03 PM
#6
Re: Weird average
Arrays are sequences with fixed length. Once declared, they size cannot change. Lists are sequences with variable length. They can grow and shrink. I too suggest you use a vector. Here is a tutorial: http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4027/.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|