Hello, I am having a bit of a beginner problem :) In my code my average is not coming out to be right. Bellow is my code and the out put. Thanks for the help!
// main.cpp
// Program 8
// Created by William Blake Harp on 7/16/14.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
double average = 0;
double class_average = 0;
int sum_of_test_scores = 0;
//2D Array.
constint students = 5;
constint test_scores = 4;
int scores[students][test_scores];
//Arrays on the heap.
string* arrayNames = nullptr;
arrayNames = new string[students];
int* arrayID = nullptr;
arrayID = newint[students];
int* arrayAverage = nullptr;
arrayAverage = newint[students];
int* arrayLetterGrade = nullptr;
arrayLetterGrade = newint[students];
ifstream inputFile;
// Open the file.
inputFile.open("Student Data.txt");
// do an initial test.
cout<<boolalpha;
cout<< "Is the file good? --> "<<inputFile.good()<<endl<<endl;
if (inputFile.fail())
cout << "Can't open the file!" << endl;
else
{
for( int i(0); i != 5; i++ )
{
// read name.
getline(inputFile, arrayNames[i]);
cout<<arrayNames[i]<<endl;
// read student id.
inputFile >> arrayID[i];
cout<< arrayID[i] <<endl;
// read four(4) scores.
for( int j(0); j != 4; j++ )
{
inputFile >> scores[i][j];
cout << scores[i][j] << endl;
}
string str;
// consume '\n' char (Use getline for mac/Xcode!!!).
getline (inputFile, str);
cout << "I have read "<<i+1 <<" record/s\n\n";
}// end read loop.
// display student name, score, ID and average.
for( int i(0); i != 5; i++ )
{
cout << arrayNames[i];
for( int j(0); j != 4; j++ )
{
cout << scores[i][j] << " ";
sum_of_test_scores += scores[i][j];
}
cout << endl;
average = sum_of_test_scores / 5.0;
cout << "The average is: " << average << endl;
if (average > 100)
{
cout << "invalid average for grade please try again." << endl;
}
elseif(average >= 90 && average <= 100)
{
cout << "Letter grade is A!" << endl;
}
elseif (average >= 89 && average <= 80)
{
cout << "Letter grade is B" << endl;
}
elseif (average >= 79 && average <= 70)
{
cout << "Letter grade is C" << endl;
}
elseif (average >= 79 && average <= 60)
{
cout << "Letter grade is D" << endl;
}
else
{
cout << "Letter grade is F!" << endl;
}
cout << endl;
}// End display name & scores.
cout << endl;
// Loop for class average.
for (int i = 0; i != 5; i++)
{
class_average = sum_of_test_scores / 5.0;
}
cout << "The class average is: " << class_average << endl;
// Close the file.
inputFile.close();
// releasing memory block on the heap.
delete [] arrayNames;
arrayNames = 0;
delete [] arrayID;
arrayID = 0;
delete [] arrayAverage;
arrayAverage = 0;
delete [] arrayLetterGrade;
arrayLetterGrade = 0;
}
return 0;
}// End Code.
Is the file good? --> true
Amy Adams
10111
97
86
78
95
I have read 1 record/s
Ben Barr
20222
89
81
73
87
I have read 2 record/s
Carla Carr
30333
79
71
63
77
I have read 3 record/s
Don Davis
40444
69
62
58
67
I have read 4 record/s
Edna Eaton
50555
63
51
62
48
I have read 5 record/s
Amy Adams
97 86 78 95
The average is: 71.2
Letter grade is F!
Ben Barr
89 81 73 87
The average is: 137.2
invalid average for grade please try again.
Carla Carr
79 71 63 77
The average is: 195.2
invalid average for grade please try again.
Don Davis
69 62 58 67
The average is: 246.4
invalid average for grade please try again.
Edna Eaton
63 51 62 48
The average is: 291.2
invalid average for grade please try again.
The class average is: 291.2
This is one reason magic number are bad average = sum_of_test_scores / 5.0;
should be average = sum_of_test_scores / test_scores;
then you would not make the mistake of putting the wrong number for the divisor.
Also sum_of_test_scores is not reset to 0 so it just keeps adding up all the scores.
((97 + 86 + 78 + 95) + (89 + 81 + 73 + 87)) / 5.0 = 137.2
sum_of_test_scores = 0;
I would do it after the average is calculated. You are also going to need another variable to keep a running total of all the scores in order to get the class average. Obviously you need to add the sum to that variable before resetting it also.