Nov 6, 2010 at 11:04pm UTC
I am doing a program that opens a file and reads the grade. then when its finished it has an average and number of students. then i open the file again and compares each individual grade to the average.
my problem is that when i open the file a second time the contents in the file are not being read. and when i try to write it to another file the file appears
empty
here is my code
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int count_of_student = 0;
int grade = 0;
int total_grade = 0;
int average = 0;
char ID_number[6];
ifstream inFile;
ofstream outFile;
inFile.open("Grades.txt");
if ( ! inFile )
{
cout << "Error opening file." << endl;
}
while ( ! inFile.eof() )
{
inFile >> ID_number >> grade;
if ( ( grade >= 0 ) && ( grade <= 10) )
{
total_grade = total_grade + grade;
count_of_student = count_of_student + 1;
}
}
inFile.close();
average = total_grade / count_of_student;
outFile.open("Average.txt");
inFile.open("Grades.txt");
while ( ! inFile.eof() )
{
inFile >> ID_number >> grade;
if ( ( grade = ( average + 4) ) || ( grade = 10 ) )
{
cout << ID_number << " " << grade << " " << "A" << endl;
outFile << ID_number << " " << grade << " " << "A" << endl;
}
else if ( ( grade = ( average + 2 ) ) || ( grade = ( average + 3 ) ) )
{
cout << ID_number << " " << grade << " " << "-A" << endl;
outFile << ID_number << " " << grade << " " << "-A" << endl;
}
else if ( ( grade = average ) || ( grade = (average + 1 ) ) )
{
cout << ID_number << " " << grade << " " << "B" << endl;
outFile << ID_number << " " << grade << " " << "B" << endl;
}
else if ( ( grade = (average - 1) ) )
{
cout << ID_number << " " << grade << " " << "C" << endl;
outFile << ID_number << " " << grade << " " << "C" << endl;
}
else if ( ( grade = ( average - 2 ) ) || ( (grade <= ( average - 2 ) ) && ( grade >= 0 ) ) )
{
cout << ID_number << " " << grade << " " << "F" << end
outFile << ID_number << " " << grade << " " << "F" << endl;
}
else
{
cout << ID_number << " " << grade << " " << endl;
outFile << ID_number << " " << grade << " " << endl;
}
}
inFile.close();
cout << "A total of " << count_of_student << " students took the qiz." << " The average was " << average << " ." << endl;
outFile.close();
system("PAUSE");
return EXIT_SUCCESS;
any help will be greatly appreciated