getting memory errors on beginners progam
using C++
The program compiles fine, but it will pop up a little window saying it "Generated Errors and must be closed by Windows. :confused: Also, how can i properrly pass the grade array into my function? I cant find my notes on how to do that.
here is my program. it takes input like so:
1 80 84 91
where its Student ID, Test1 score, Test2, Final Exam, then outputs things like student average score and stuff to output.txt
Code:
//
// Nicholas Funnell
// Assignment #1
// january 29, 2003
// -------------------
// Takes data from input.txt, processes test scores and sends output to output.txt
//
#include <fstream>
using namespace std;
struct DATA
{
// holds data about student that will be printed to output.txt
int id;
int average;
int grade;
};
void GetStudentData ( DATA &student, ifstream &fin, int grade[5] )
{
// *** GetStudentData() will get one line from the file opened by fin
// (we will open input.txt in main() ) Takes the values from the line and
// puts them in a struct, and also computes the students average grade and
// letter grade.
int letter_grade, test1, test2, testFinal;
fin >> student.id >> test1 >> test2 >> testFinal; // read in from file.
student.average = int(0.20 * test1 + 0.30 * test2 + 0.50 * testFinal);
// compute average semester grade
// here we decide which letter grade student got
if(student.average >= 90)
letter_grade = 4; // grade: A
else if(student.average >= 80 && student.average <= 89)
letter_grade = 3; // grade: B
else if(student.average >= 70 && student.average <= 79)
letter_grade = 2; // grade: C
else if(student.average >= 60 && student.average <= 69)
letter_grade = 1; // grade: D
else
letter_grade = 0; // grade: F
// now assign that grade to the student & update the count of that grade
student.grade = letter_grade;
grade[letter_grade] += 1; // keeping track of number of grades
// end of function
return;
}
void PrintStudentData ( DATA &student, ofstream &fout )
{
char grade[5]; // letter grades.
grade[0] = 'F';
grade[1] = 'D';
grade[2] = 'C';
grade[3] = 'B';
grade[4] = 'A';
// print out the stuff on a student one line at a time...
fout << student.id << "\t\t\t" << student.average << "\t\t\t"
<< grade[student.grade] << '\n' << flush;
return;
}
int main ( void )
{
DATA student[50]; // hold all student records.
ifstream fin; // for input
ofstream fout; // for output
int i=0, numstudents, grade[5];
float class_average;
// grade will hold the number of grades recived in the class.
// ex: grade[4] will hold the number of A's given to students.
fin.open("input.txt"); // open for input
fout.open("output.txt"); // open for output
// lets go get the data from input.txt
for(i=0; i<50; i++)
{
// the for loop will make sure we only get the first 50 lines of the file
if(!fin.eof()) // while were not at the end of the file...
GetStudentData(student[i], fin, grade); // get the data for a line!
else
break; // kills for loop when we reach end of file.
}
numstudents = i; // holds number of students...
// compute class average
class_average = ( (4 * grade[4]) + (3 * grade[3]) + (2 * grade[2]) + (1 * grade[1]) ) / numstudents;
// now we can print it all out
// start will the ID, semester average, and letter grade for every student
fout << "Student ID: \t Semester Average: \t Letter Grade: \n" << flush;
for(i=0; i<=numstudents; i++)
{
PrintStudentData( student[i], fout);
}
// now print class average and grade distribution
fout << "\n\nClass Average: " << class_average << flush;
fout << "\n Grade Distribution: \n"
<< "A \t" << grade[4]
<< "\nB\t" << grade[3]
<< "\nC\t" << grade[2]
<< "\nD\t" << grade[1]
<< "\nF\t" << grade[0] << flush;
fout << "\n\nNumber of Students: " << numstudents << flush;
// and then we die very quietly...
return 0;
}
Thanks in advance for any help!