#include <iostream>
#include<string.h>
#include<fstream>
#include<conio.h>
usingnamespace std;
class student{
char admno[99];
char name[99];
char stbno[6];
int token;
public:
void create_student()
{
cout<<"\nNEW STUDENT ENTRY...\n";
cout<<"\nEnter The admission no. ";
cin.getline(admno,99);
cout<<"\n\nEnter The Name of The Student ";
cin.getline(name,99);
cout<<"\n\nStudent Record Created..";
getch();
}
};
student st;
fstream fp;
void write_student()
{
char ch;
fp.open("student.txt",ios::out|ios::app);
do
{
st.create_student();
fp.write((char*)&st,sizeof(st));
cout<<"\n\ndo you want to add more record..(y/n?)";
cin>>ch;
cout<<endl;
}while(ch=='y'||ch=='Y');
fp.close();
}
int main()
{
write_student();
return 0;
}
This program takes the first entry(admno and name) perfectly but but skips admno if I want to input another entry.
Compiler used - Codeblocks
Maybe the problem lies with getline.
Any suggestions ?
I myself would guess the problem has to do with that cin >> ch. Remember this leaves the end of line character in the stream which messes with getline().
Then what do you recommend about it ?
Because cin.ignore is not working at all times.
Even after using it, if for eg. I enter admno=6035 first time, the second entry for admno in the file "student.txt" is 035 irrespective of what I enter.
After adding ignore after cin>>ch, the program does not take the first entry's admno. i.e. it skips admno and directly asks for name.
After that it works without any problem.
Why is this happening ?