Need to Debug this Program

This is a simple program about managing a class's performance report.It uses the concept of file handling and stored the data in binary format in the data file 'Record.dat'.The problem is that on executing this program the latest record is getting displayed 2 times on executing the display all option in the entry menu.Same problem with the class result option in 'results menu'.Now maybe this is because 2 records are being created and stored on the file when I enter a fresh new record or maybe something else,I don't know.Moreover when I delete a non existent record, the latest record is again showing up another time in the data file.What should I do??
Here's the program 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<stdlib.h>


class student
{
 private:
  int roll_no;
  int standard;
  char name[40];
  float per;
  float chem,maths,phy,comp,eng;
  void calculate();              //calculates percentage and assigns grade
  char grade;
 public:
  void get_data();               //input
  void show_data();              //output
  void show_table();             //show data in a tabular form or row form
  int get_roll();                //returns roll-no
};

void student::calculate()
{per=(chem+maths+phy+comp+eng)/5.00;
 if(per>80 && per<=100)
 grade='A';
 else if(per>60 && per<=80)
      grade='B';
      else if(per>40 && per<=60)
	   grade='C';
	   else grade='D';
}

void student::get_data()
{
cout<<"ENTER YOUR NAME:"<<endl;
gets(name);
cout<<"ENTER YOUR ROLL_NO:"<<endl;
cin>>roll_no;
cout<<"ENTER YOUR CLASS:"<<endl;
cin>>standard;
cout<<"ENTER YOUR MARKS IN PHYSICS OUT OF 100:"<<endl;
cin>>phy;
cout<<"ENTER YOUR MARKS IN CHEMISTRY OUT OF 100:"<<endl;
cin>>chem;
cout<<"ENTER YOUR MARKS IN MATHS OUT OF 100:"<<endl;
cin>>maths;
cout<<"ENTER YOUR MARKS IN ENGLISH OUT OF 100:"<<endl;
cin>>eng;
cout<<"ENTER YOUR MARKS IN COMPUTER SCIENCE OUT OF 100:"<<endl;
cin>>comp;
calculate();
}

void student::show_data()
{
cout<<"NAME:"<<name<<endl;
cout<<"ROLL_NO:"<<roll_no<<endl;
cout<<"CLASS:"<<standard<<endl;
cout<<"MARKS IN PHYSICS:"<<phy<<endl;
cout<<"MARKS IN CHEMISTRY:"<<chem<<endl;
cout<<"MARKS IN MATHS:"<<maths<<endl;
cout<<"MARKS IN ENGLISH:"<<eng<<endl;
cout<<"MARKS IN COMPUTER:"<<comp<<endl;
cout<<"TOTAL PERCENTAGE:"<<per<<endl;
cout<<"GRADE:"<<grade<<endl;
}

void student::show_table()
{
  cout<<roll_no<<setw(6)<<" "<<name<<setw(10)<<phy<<setw(4)<<chem<<setw(4)<<maths<<setw(4)<<eng<<setw(4)<<comp<<setw(4)<<per<<setw(6)<<" "<<grade<<endl;
 }

int student::get_roll()
{
return roll_no;
}

void enter_record()                  //To enter Records in a file
{
ofstream file("Record.dat",ios::out|ios::binary|ios::app);
student obj;
obj.get_data();
cout<<"DETAILS OF THE STUDENT"<<endl;
obj.show_data();
file.write((char*)&obj,sizeof(obj));
cout<<"Record Created!"<<endl;
getch();
file.close();
}

void del_record(int n)             //To delete Records from a file
{ int flag=0;
ifstream fin("Record.dat",ios::in|ios::binary);
student buf;
ofstream fout("temp.dat",ios::out|ios::binary);
while(!fin.eof())
{
 fin.read((char*)&buf,sizeof(buf));
 if(buf.get_roll()==n)
 {
 flag=1;
 }
 else fout.write((char*)&buf,sizeof(student));
}
if(flag==0){cout<<"Record not found"<<endl;
getch();    }
fin.close();
fout.close();
remove("Record.dat");
rename("temp.dat","Record.dat");
}

void disp_all()                   //To display all the
				  //records and its details
{
ifstream f("Record.dat",ios::in|ios::binary);
student buf;
while(!f.eof())
{
 f.read((char*)&buf,sizeof(student));
 buf.show_data();
}
f.close();
}

void disp_sp(int n)               //to display the details
				 // of a particular student
{
ifstream f("Record.dat",ios::in|ios::binary);
student buf;
int flag=0;
while(!f.eof())
{
 f.read((char*)&buf,sizeof(student));
 if(buf.get_roll()==n)
 {flag=1;
  buf.show_data();
  break;
 }
}
if(flag==0)
cout<<"Record not found!"<<endl;
getch();
f.close();
}

void mod_record(int n)                //To Modify
				      //A Record
{      int flag=0;student buf;
 fstream f("Record.dat",ios::in|ios::out|ios::binary);
 while(!f.eof())
 {f.read((char*)&buf,sizeof(student));
  if(buf.get_roll()==n)
  {flag=1;
   cout<<"THE OLD RECORD"<<endl;
   buf.show_data();
   cout<<"Enter the new records"<<endl;
   buf.get_data();
   int pos=(-1)*sizeof(student);
   f.seekp(pos,ios::cur);
   f.write((char*)&buf,sizeof(student));
   cout<<"Record Updated"<<endl;
   getch();
   }
  }
 if(flag==0)
 {cout<<"Record not found!"<<endl;
  getch();  }
  f.close();
}

void class_result()               //To Display Class results
				  //in a tabular form
{
 student buf;
 ifstream f("Record.dat",ios::in|ios::binary);
  cout<<"\t\t CLASS RESULT"<<endl;
  cout<<"======================================================"<<endl;
  cout<<"R.no       Name       P   C   M   CS   %age   Grade"<<endl;
  cout<<"======================================================"<<endl;
  while(f.eof()==0)
  {f.read((char*)&buf,sizeof(student));
   buf.show_table();
  }
  getch();
  f.close();
}

void results()                     //THE 'Results' sub-menu
{
while(1)
{
clrscr();
cout<<"\t\t\t WELCOME TO RESULTS MENU"<<endl;
cout<<"\t1.CLASS RESULT"<<endl;
cout<<"\t2.STUDENT RESULT"<<endl;
cout<<"\t3.BACK TO MAIN MENU"<<endl;
cout<<"\tENTER YOUR CHOICE"<<endl;
int op;
cin>>op;
switch(op)
{
 case 1:class_result();
	break;
 case 2:cout<<"enter the student roll.no"<<endl;
	int x;
	cin>>x;
	disp_sp(x);
	break;
}
if(op==3)
{break;}
}
}

void entry()                    //the 'entry' sub-menu
{
while(1)
{clrscr();
 cout<<"\t\t\t WELCOME TO ENTRY MENU"<<endl;
 cout<<"\t1.Enter a record"<<endl;
 cout<<"\t2.Delete a record"<<endl;
 cout<<"\t3.Modify a record"<<endl;
 cout<<"\t4.Search a record"<<endl;
 cout<<"\t5.Show all records"<<endl;
 cout<<"\t6.Back to Main Menu"<<endl;
 cout<<"\tENTER YOUR CHOICE"<<endl;
 int op;
 cin>>op;
 switch(op)
 {
  case 1:enter_record();
	 break;
  case 2:cout<<"ENTER THE ROLL NO. TO BE DELETED"<<endl; int x;cin>>x;
	 del_record(x);
	 break;
  case 3:cout<<"ENTER THE ROLL NO. OF THE RECORD TO BE MODIFIED"<<endl;
	 int y;
	 cin>>y;
	 mod_record(y);
	 break;
  case 4:cout<<"ENTER THE ROLL NO. OF THE RECORD TO BE SEARCHED"<<endl;
	 int z;
	 cin>>z;
	 disp_sp(z);
	 break;
  case 5:disp_all();
	 getch();
 }
 if(op==6)
 {break;}
}
}

void main()
{
while(1)
{
clrscr();
cout<<"\t\t\t WELCOME TO MAIN MENU"<<endl;
cout<<"\t1.ENTRY"<<endl;
cout<<"\t2.RESULTS"<<endl;
cout<<"\t3.Exit"<<endl;
cout<<"\tEnter your choice"<<endl;
int op;
cin>>op;
switch(op)
{
 case 1:entry();
	break;
 case 2:results();
	break;
 case 3:exit(0);
}
}
}

Please suggest something to debug this program.

Last edited on
On line 138/156: You do not check whether the read operation was successful (or an eof occurs). So you read beyond the end and store the last line twice.
Topic archived. No new replies allowed.