Dec 8, 2015 at 11:38pm UTC
Hi, I wrote this program, however it doesn't want to run. I'm really stuck and I tried changing stuff around, but still makes the program crash when I run it.
This was the original question:Write a program to read class grades from a file (A4Q3.dat - which is given to you) into an array grades
here are the 30 grades for the array:
99 95 88 82 66 72 51 64 44 25 57 15 3 89 84 51 62 99 35 68 100 10 15 76 64 41 33 17 94 70
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
using std::setw;
const int SIZE = 30; // maximum size, since number of grades is unknown
int main() {
cout << "-----------------------------------------------------" << endl
<< " Welcome to Grade statistics evaluator" << endl
<< "-----------------------------------------------------" << endl << endl;
// Variable definition
int number = 0;
int grades[30];
int nbGrades_90 = 0;
int nbGrades_90_80 = 0;
int nbGrades_80_65 = 0;
int nbGrades_65_50 = 0;
int nbGrades_50 = 0;
int total = 0;
// File input setup
ifstream fin;
fin.open("A4Q3.txt" ); {
if (!fin) {
cout << "Error: file not found." ;
return 0;
}
fin >> grades[number];
// Loop to read grade values from file
for (number = 0; !fin.eof(); number++) {
total = total + grades[number]; // Calculates total, for average calculation
// Count grades within each interval
if (grades[number] >= 90)
nbGrades_90++;
else if ((grades[number] < 90) && (grades[number] >=80))
nbGrades_90_80++;
else if ((grades[number] < 80) && (grades[number] >=65))
nbGrades_80_65++;
else if ((grades[number] < 65) && (grades[number] >=50))
nbGrades_65_50++;
else if (grades[number] < 50)
nbGrades_50++;
}
// Output
cout << "Class average: " << total / number << setprecision(2)<< endl << endl
<< "Grades >= 90: " << (nbGrades_90 / number) * 100 << setprecision(2) << endl
<< "Grades >= 80: " << (nbGrades_90_80 / number) * 100 << setprecision(2) << endl
<< "Grades >= 65: " << (nbGrades_80_65 / number) * 100 << setprecision(2) << endl
<< "Grades >= 50: " << (nbGrades_65_50 / number) * 100 << setprecision(2) << endl
<< "Grades < 50: " << (nbGrades_50 / number) * 100 << setprecision(2) << endl;
fin.close();
}
return 0;
}
Last edited on Dec 8, 2015 at 11:50pm UTC
Dec 8, 2015 at 11:58pm UTC
Ok so I added it as you said but it still doesn't run, no errors.
In your opinion, did I write arrays? Because I tried looking up notes but it isn't clear.
Like my question is: do we need to write in type arrays or arr of some sort
@DeathLeap
Dec 9, 2015 at 12:40am UTC
Sorry for deleting my post. That was by accident.
I think the problem with your code is that you don't actually have a proper way to read the numbers from your file to your grades[30] array.
Last edited on Dec 9, 2015 at 12:41am UTC
Dec 9, 2015 at 12:44am UTC
The loop which begins on line 36 is infinite. You never read anything in from your file, so the condition governing the for will never be false.
In most instances it is wrong to loop on EOF anyway.