Having problem in program with FILES. Pls reply ASAP.

Jan 30, 2016 at 3:14pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include<string.h>
#include<fstream>
#include<conio.h>


using namespace 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 ?
Last edited on Jan 30, 2016 at 3:32pm
Jan 30, 2016 at 3:50pm
Maybe the problem lies with getline.

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().

Why are you using those global variables?

Jan 30, 2016 at 3:53pm
There are no global variables. Variables are there in the class and the function.

And I dont understand how cin>>ch messes with getline ?
Jan 30, 2016 at 3:53pm
use
cin.ignore(100,'\n');
after every cin u do since the new line character is left in the stream
Last edited on Jan 30, 2016 at 3:59pm
Jan 30, 2016 at 3:59pm
Now it works.
Thanks :)
Jan 30, 2016 at 4:02pm
There are no global variables. Variables are there in the class and the function.

Then what are these variables?

1
2
3
4
5
6
7
8
9
...
}; // End of class definition.

student st;
fstream fp;

void write_student()

...


They look like global variables to me.

Why are you using getch()?

Edit:
And I dont understand how cin>>ch messes with getline ?
Remember this leaves the end of line character in the stream which messes with getline().


When getline() encounters a new line character, when using the default third argument, it stops processing the input since it found the delimiter.
Last edited on Jan 30, 2016 at 4:06pm
Jan 30, 2016 at 4:18pm
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.
Last edited on Jan 30, 2016 at 4:22pm
Jan 30, 2016 at 4:28pm
You only need ignore() after a formatted input, cin >>

In your code that means after the cin>>ch; at line 40.
Jan 30, 2016 at 4:41pm
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 ?
Jan 30, 2016 at 4:48pm
Post your current code.
Topic archived. No new replies allowed.