Reading from a file...

in the else if, compiler gives an error saying "undeclared identifier" HELP plz...!

void studentrecord()
{
char sub;
cout<<"\n\nChoose from one of the following subcategories: "<<endl<<endl;
cout<<"\tPress 'a' to search student's information data"<<endl;
cout<<"\tPress 'b' to enter a new record for a student"<<endl<<endl;
cout<<"Sub-category: ";
cin>>sub;

if(sub=='b')
{
fstream TOfile_student;
TOfile_student.open("Student_Record.txt", fstream::in | fstream::out | fstream::app);

char stuname[40];
char rollno[10];
char fathname[40];
char dob[12];
char doe[12];
char dep[8];
char batch[20];
char section [10];
char gpa[50];


cout<<"\n\n\t*STUDENT'S PERSONAL RECORD*"<<endl<<endl;
cout<<"Please, enter the following: "<<endl<<endl;
cout<<"\n\n 1.Student's name: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (stuname, 40, '\n'); TOfile_student<<"\n\n\t Student's name : "<<stuname<<endl;
cout<<"\n\n 2.Roll #: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (rollno, 10, '\n'); TOfile_student<<"\t Roll # : "<<rollno<<endl;
cout<<"\n\n 3.Father's name: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (fathname, 40, '\n'); TOfile_student<<"\t Father's name : "<<fathname<<endl;
cout<<"\n\n 4.Date of birth (dd/mm/yy): "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (dob, 12, '\n'); TOfile_student<<"\t Date of birth : "<<dob<<endl;
cout<<"\n\n 5.Date of enrollment: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (doe, 12, '\n'); TOfile_student<<"\t Date of enrollment : "<<doe<<endl;
cout<<"\n\n 6.Department: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (dep, 8, '\n'); TOfile_student<<"\t Department : "<<dep<<endl;
cout<<"\n\n 7.Batch: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (batch, 20, '\n'); TOfile_student<<"\t Batch : "<<batch<<endl;
cout<<"\n\n 8.Section: "; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (section, 10, '\n'); TOfile_student<<"\t Section : "<<section<<endl;
cout<<"\n\n 9.SEMESTER WISE G.P.A "<<endl;
cout<<"(1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th)"; cin.ignore(cin.rdbuf()->in_avail(),'\n'); cin.getline (gpa, 50, '\n'); TOfile_student<<"\t GPA : "<<gpa<<endl;
}

else if(sub=='a')
{

fstream FROMfile_student;
FROMfile_student.open("Student_Record.txt", fstream::in | fstream::out | fstream::app);

while( !FROMfile_student.eof() ) {

FROMfile_student.getline(rollno,10 );
cout<<rollno<<endl;

}

FROMfile_student.close();


}
Last edited on
rollno was declared in the if part but you are using it in the else part of your if statement. Move it before the if.
Also, you're missing one }.
Also, when you post code, put it in [code] tags.
please include the [code] tag when posting source code.

Then "undeclared identifier" is a very generic error, probably resulting of a missing include (iostream for instance)
Last edited on
Without the name of the "undeclared identifier", there's no point in reading your code.

Anyway, it means you typed a variable name that isn't declared. Most likely due to a typo.
Thanx a lot...@ hamsterman :) solved the problem!
There isn't an error anymore or a warning, but when I execute the program, the file does not open... any suggestions why that could be the case?
What does "does not open" mean? Are you sure the else part is executed? What is the value of FROMfile_student.is_open() ? Are you sure the file exists?
Topic archived. No new replies allowed.