#include <iostream>
#include <fstream>
using namespace std;
typedef int GradeType[6];
float findAverage (const GradeType, int);
int findHighest (const GradeType, int);
int findLowest (const GradeType, int);
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos = 0; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
When you do while (pos < grades[pos] && inputFile >> grades[pos]),
you are comparing your loop variable "pos" to a student's grade. Apart from not making sense, grades[] has not been initialised.
Furthermore, you're limited to an array of 6 grades.
Later on you keep comparing your loop variable to the grades, there's just no way this will work as you expect.
I can't really help much because the code you showed is not formatted, so I can't refer to specific line numbers, but generally, you should read te whole file, and initialise numberOfGrades, which is what you should be comparing pos to. Use a vector or other dynamic container instead of fixed sized array of 6.
ok so i adjusted my code to this but now im having trouble getting the average
**********************
#include <iostream>
#include <fstream>
using namespace std;
typedef int GradeType[100];
float findAverage (const GradeType, int);
int findHighest (const GradeType, int);
int findLowest (const GradeType, int);
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
ifstream inputFile;
inputFile.open("gradfile.txt");
int count = 0;
while (inputFile >> grades[count])
{
cout << "Grade number " << grades[count] << endl;
count++;
}
numberOfGrades = count;
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
// Fill in the call to the function that calculates highest grade
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;
// Fill in the call to the function that calculates lowest grade
lowestGrade = findLowest(grades, numberOfGrades);
cout << endl << "The lowest grade is " << lowestGrade << endl;
// Fill in code to write the lowest to the screen
inputFile.close();
return 0;
}
findAverage
float findAverage (const GradeType grades, int numberOfGrades)
{
float sum = 0.0; // holds the sum of all the numbers
for (int count = 0; count < grades[count]; count++)
{
sum += grades[count];
}
return sum / numberOfGrades; //returns the average
:D i figured it out the program runs but i dont understand the why if someone can explain to me
***********************
while (inputFile >> grades[count])
{
cout << "Grade number " << grades[count] << endl;
count++;
}
***********************
while (inputFile >> grades[count])
{
cout << "Grade number " << grades[count++] << endl;
}
numberOfGrades = count;
***********************
by changing the 1st block of code above to the second one it works fine but whats the difference between "grades[count++]" and "grades[count]" i thought i fixed that by putting "count++" on the outside or even if i didn't put it on the outside why wouldn't it work
I assume the batch of code you posted isn't exactly what you typed, as this would never compile:
1 2 3 4 5 6 7 8 9 10 11 12
findAverage
float findAverage (const GradeType grades, int numberOfGrades)
{
float sum = 0.0; // holds the sum of all the numbers
for (int count = 0; count < grades[count]; count++)
{
sum += grades[count];
}
return sum / numberOfGrades; //returns the average
If you fix that, I can't see what the problem is, exactly what happened wrt "now im having trouble getting the average"?
As for the two blocks you're comparing, I can't see at first glance what the difference is, if they're not working the same way, it's probably related to some other section of code.
Please try and post using the code formatting tags and some indentation.
"
yeah the code is different now than the one i posted and runs fine, im just curious to try and understand what is going on between the two different blocks of code i posted above this comment, so i can mark it as solved, id just hate to have the answer and not understand completely why, ill re-post the piece im talking about in this
********************************************
while (inputFile >> grades[count])
{
cout << "Grade number " << grades[count] << endl;
count++;
}
********************************************
while (inputFile >> grades[count])
{
cout << "Grade number " << grades[count++] << endl;
}
numberOfGrades = count;
********************************************
by changing the 1st block of code above to the second one below it the code works fine but whats the difference between "grades[count++]" and "grades[count]" i thought i fixed that by putting "count++" on the outside or even if i didn't put it on the outside why wouldn't it work