problem with the last item of the file

This is a simple program that gets objects of a class and write them into a file. it can del(by a sign in the obj) and edit the objects of file. It works correctly But when I want to edit or delete the last object in the file I doesn't work. Excuse me for long code...
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/****************************************HeaderFiles*/

#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdio.h>

/****************************************Class definition*/

class pay
{
	public:
		char tit[80],det[400];
   	float c;
   	int y,m,d;
   	pay(){};
      void getdata();
      void showdata();
};
void pay::getdata()
{
	cout<<"Title: ";
   gets(tit);
   cout<<"Cost: ";
   cin>>c;
   cout<<"Date:    ";
   do{
		cout<<"\n      Year: ";
      cin>>y;
   }while(y<1380||y>1400);
   do{
   	cout<<"     Month: ";
      cin>>m;
   }while(m>12||m<1);
   if(m>0&&m<7)
   	do{
      	cout<<"       Day: ";
         cin>>d;
      }while(d>31||d<1);
   if(m>6&&m<12)
   	do{
      	cout<<"       Day: ";
         cin>>d;
      }while(d>30||d<1);
   if(m==12&&y%4==3)
   	do{
      	cout<<"       Day: ";
         cin>>d;
      }while(d>30||d<1);
   if(m==12&&y%4!=3)
   	do{
      	cout<<"       Day: ";
         cin>>d;
      }while(d>29||d<1);
   cout<<"Det: ";
   gets(det);
}
void pay::showdata()
{
	cout<<"Title: ";
   puts(tit);
   cout<<"Cost: "<<c;
   cout<<"\nDate: "<<y<<" / "<<m<<" / "<<d;
   if(det[0])
   {
   	cout<<"\nDetails: ";
   	puts(det);
      cout<<endl;
      for(int m=0;m<75;m++)
	   	cout<<"*";
      return;
   }
   cout<<endl<<endl;
   for(int m=0;m<75;m++)
   	cout<<"*";
}

/****************************************function prototypes*/

void enter();
void view();
void edit();
void del();

/****************************************Main function*/

void main()
{
	char ans;
   while(1)
   {
   	clrscr();
      cout<<"1)Enter\n2)View\n3)Edit\n4)Delete\n5)Exit";
      ans=getch();
      switch(ans)
      {
      	case '1': enter();  break;
      	case '2': view();  break;
      	case '4': del();  break;
         case '3': edit(); break;
         case '5': return;
      }
   }
}

/****************************************functions definition*/

void enter()
{
	clrscr();
	pay p;
   fstream f;
   f.open("Datafile.dat",ios::out|ios::binary|ios::app);
   p.getdata();
   f.write((char*) &p,sizeof(pay));
   f.close();
   return;
}


void view()
{
	clrscr();
   int i=0;
   fstream f;
   pay p;
   f.open("Datafile.dat",ios::in|ios::binary);
   while(f.read((char*) &p,sizeof(pay)))
   {
      if(p.d!=0)
      {
         cout<<++i<<":\n";
   		p.showdata();
      	cout<<"\n\n";
      }
   }
   f.close();
   getch();
}


void edit()
{
	clrscr();
   clrscr();
   int n,i=0,e=0;
   pay p;
   fstream f;
   f.open("Datafile.dat",ios::in|ios::out|ios::binary);
   cout<<"Enter num of record to edit: ";
   cin>>n;
   while(f.read((char*) &p,sizeof(pay))&&i<n)
   {
   	if(p.d==0)
      {
      	e++;
         i--;
      }
      i++;
   }
   n=n+e-1;
   f.seekg(n*sizeof(pay));
   f.read((char*)&p,sizeof(pay));
   cout<<"\n\n";
   p.showdata();
   char ans;
   cout<<"\nAre you sure to edit? <y>";
   ans=getch();
   if(ans!='y')
   	return;
   cout<<"\n\n";
   p.getdata();
   f.seekp(n*sizeof(pay));
	f.write((char*) &p,sizeof(pay));
   f.close();
}


void del()
{
	clrscr();
   int n,i=0,e=0;
   pay p;
   fstream f;
   f.open("Datafile.dat",ios::in|ios::out|ios::binary);
   cout<<"Enter num of record to delete: ";
   cin>>n;
   while(f.read((char*) &p,sizeof(pay))&&i<n)
   {
   	if(p.d==0)
      {
      	e++;
         i--;
      }
      i++;
   }
   n=n+e-1;
   f.seekg(n*sizeof(pay));
   f.read((char*)&p,sizeof(pay));
   cout<<"\n\n";
   p.showdata();
   char ans;
   cout<<"\nAre you sure to delete? <y>";
   ans=getch();
   if(ans!='y')
   	return;
   p.d=0;
   f.seekp(n*sizeof(pay));
	f.write((char*) &p,sizeof(pay));
   f.close();
}
Add a f.clear(); before lines 173 and 208.
You are terminating the read process by hitting the EOF. You can't do anything else to the file (even seek) until you clear the error flag.)

:-)

I don't know what compiler you are using, but it is ancient. It took me a minute to fix all the little things that are non-standard so I could compile it. I suggest you upgrade to one of the free modern compilers:
Microsoft Visual C++ Express http://www.microsoft.com/express/vc/
Borland C++ Turbo Explorer http://www.turboexplorer.com/cpp
MinGW http://www.mingw.org/

Hope this helps.
Topic archived. No new replies allowed.