Error issue

Here is a program I wrote for an assignment, there is an error that I tried for days to fix but with no luck. I know where it is but I dont know why the error shows up. Its a grading sheet program



#include <fstream>
#include<iostream>
#include<iomanip>
#include<string>


using namespace std;


int main()
{
string NAME, filename, S1, S2, S3;
int ID, students;
double GRADE;




cout<<"Please specify a name for the file"<<endl;
getline(cin, filename);
//cin>>filename;

ofstream file; // Explain please

file.open(filename.c_str());

cout<<"How many students?"<<endl;
cin>>students;
//{
// cout<<"Class X grade sheet"<<endl; //Header ?
//}


file << left << setw(5) <<"ID"<<setw(10)<<"NAME"<<setw(10)<<"GRADE\n\n"<<endl;

for(int s = 0; s<students; s++)
{
cout<<"Enter the ID:"<<endl;
cin>>ID;
cout<<"Enter student name:"<<endl;
cin>>NAME;
cout<<"Enter student grade:"<<endl;
cin>>GRADE;



while( GRADE<0 || GRADE>100)
{
cout<<"Invalid Grade! Please enter the correct Grade value."<<endl;
cin>>GRADE;

}

file<<left<<setw(5)<<ID<<setw(10)<<NAME<<setw(10)<<GRADE<<endl;
}


while(!file.eof()) //While it is not the end of the file...
{
cout<<setw(5)<<ID; //Display ID
cout<<setw(20)<<NAME; //Display First name
cout<<setw(20)<<GRADE<<endl; //Display Grade
file>>ID>>NAME>>GRADE; //Reads from the data file (ERROR IS AT THIS LINE)
}

file.close(); //Closes the file
system("pause");
return 0;
}


Have you tried unrolling it into:
1
2
3
file >> ID;
file >> NAME;
file >> GRADE;

That might help you figure out if it's a file problem, a stream problem or a variable problem.

Also, I would change while (!file.eof()) to while(file.good())
I'm not familiar with while(file.good())
And, I tried unrolling, error still exists. I think its a variable error as because I think I got all my streams in right. I still am unable to fix it
you do not use file.seak(std::ios::begin) between the read and write. Nor do you open the file do write operations, then close it and then reopen it. Unless you know how to manipulate the file pointer, it is never a good idea to mess with the file pointer in the way you have.
Can you explain a little more? I'm new at this and is very confused


Also, when I remove the ofile in the end using //, the sheet keeps on repeating itself to infinity it seems. Anyway to make it show the sheet once?
Last edited on
Topic archived. No new replies allowed.