I am a beginner to this c++ language,a problem arised while del() function was carried out in my program which follows as:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<ctype.h>
#include<iomanip.h>
class student
{
int admno;
char name[35];
float m_eng;
float m_maths;
float m_sc;
float m_cmp;
float perc;
char grade;
public:
student()
{
m_eng=0;
m_maths=0;
m_sc=0;
m_cmp=0;
perc=0;
grade= ' ';
}
void getdata();
void putdata();
void read();
int getadmno()
{
return admno;
}
void calcres();
void disp1(int);
float ret_m_eng()
{
return m_eng;
}
float ret_m_maths()
{
return m_maths;
}
float ret_m_sc()
{
return m_sc;
}
float ret_m_cmp()
{
return m_cmp;
}
};
void del(int rno)
{
student stud;
ifstream fio("student.dat",ios::in);
ofstream file("temp.dat",ios::out);
char found='f',confirm='n';
while(!fio.eof())
{
fio.read((char*)&stud,sizeof(student));
if(stud.getadmno()==rno)
{
stud.putdata();
found='t';
cout<<" R u sure that u want 2 delete???";
cin>>confirm;
if(confirm=='n')
file.write((char*)&stud,sizeof(student));
}
else
file.write((char*)&stud,sizeof(student));
}
if (found=='f')
cout<<"Record no found";
fio.close();
file.close();
remove("student.dat");
rename("temp.dat","student.dat");
}
void modify(int rno)
{
student stud;
int flag=0,rec=0;
fstream fin("student.dat",ios::in|ios::out|ios::binary);
if(fin==NULL)
{
cout<<"\n File does not exist.";
return;
}
while(fin.read((char*)&stud,sizeof(student)))
{
rec++;
if(rno==stud.getadmno())
{
gotoxy(21,16);cout<<"ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
gotoxy(21,17);cout<<"º º";
gotoxy(21,18);cout<<"º STUDENT DETAILS AT PRESENT ARE : º";
gotoxy(21,19);cout<<"º º";
gotoxy(21,20);cout<<"ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
stud.putdata();
gotoxy(22,42);cout<<"ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
gotoxy(22,43);cout<<"º º";
gotoxy(22,44);cout<<"º Press any key to modify º";
gotoxy(22,45);cout<<"º º";
gotoxy(22,46);cout<<"ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
getch();
clrscr();
gotoxy(12,10);cout<<"ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
gotoxy(12,11);cout<<"º º";
gotoxy(12,12);cout<<"º ENTER THE NEW DETAILS OF THE STUDENT : º";
gotoxy(12,13);cout<<"º º";
gotoxy(12,14);cout<<"ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
stud.read();
fin.seekg((rec-1)*sizeof(student),ios::beg);
fin.write((char*)&stud,sizeof(student));
flag=1;
}
}
if(flag==0)
{
gotoxy(14,20);cout<<"ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
gotoxy(14,21);cout<<"º º";
gotoxy(14,22);cout<<"º THIS ROLL NUMBER DOES NOT EXIST º";
gotoxy(14,23);cout<<"º º";
gotoxy(14,24);cout<<"ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
}
else
{
gotoxy(24,39);cout<<"ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
gotoxy(24,40);cout<<"º º";
gotoxy(24,41);cout<<"º RECORD MODIFIED º";
gotoxy(24,42);cout<<"º º";
gotoxy(24,43);cout<<"ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
return;
}
fin.close();
}
the del() fuction will be executed without any errors but when the program is runned evenif a record exist say it to be of admission no:1
in the output"Enter the admission no to delete the record of one who belongs to it ",evenif i give the admission no:as 1(which is existing )th o\p shwn is "the record doesnot exist",
i think that the while executing the program control doesnot enter the loop (while(fin.read((char*)&stud,sizeof(student))))