Searching in Data file handling in c++

i am not able to search more than 11 or 12 records in dfh after 11 or 12 records there is output which is not desirable,like a output is unncessory charactors,binary code.such as in 13th record there is undesirable output but input was different.

here is my code:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<dos.h>
#include<string.h>

fstream a;

class files
{
long a,b;
float d,c,e,f;
unsigned z;
char x[50],y[50];
public:
void add()
{
cout<<"\n\t\t\tPRODUCT DETAILS";
cout<<"\n\tEnter NEW Product Name(MAX. 7 CHARCTERS):";
gets(x);
cout<<"\n\tEnter PRODUCT NO:";
cin>>a;
cout<<"\n\tEnter MRP(MAXIMUM RETAIL PRICE):";
cin>>d;
cout<<"\n\tENTER DISCOUNT ON PRODUCT:";
cin>>c;
}
void show()
{
cout<<"\n\t"<<a<<"\t\t"<<x<<"\t\t"<<d<<"\t\t"<<c;
}
char* NAME()
{
return x;
}

int PNO()
{
return a;
}
float MRP()
{ return d;
}
float DIS()
{ return c;
}
};
files obj;\\global object

void show()
{
cout<<"\n\t\tMY PREVIOUS ENTER PRODUCTS ARE";
a.open("nnshop.dat",ios::in|ios::app);
cout<<"\n\tS.NO\t\tNAME\t\tMRP(Rs)\t\tDISCOUNT(Rs)";
while(a.read((char*)&obj,sizeof(obj)))
{
obj.show();
}
a.close();
}
void input()
{
clrscr();
char b='y';
show();
a.open("nnshop.dat",ios::out|ios::app);
while(b=='y'||b=='Y')
{
obj.add();
a.write((char*)&obj,sizeof(obj));
cout<<"WANT TO ENTER MORE PRODUCT(Y/N):";
cin>>b;
}
cout<<"\n\t\tPLS WAIT UR PRODUCTS ARE ADDING....";
delay(1200);
cout<<"\n\t\tADDED PRODUCTS.";
delay(1000);
a.close();
}
void output()
{ clrscr();
char b='y';int e[50],f[50],x=0,q=0,p=0;
long c;
show();
do
{
cout<<"\n\tENTER PRODUCT U WANT TO BUY(S.NO):";
cin>>f[x];
cout<<"\n\tENTER PRODUCT QUANTITY:";
cin>>e[x];
cout<<"U WANT TO BUY MORE PRODUCT(Y/N):";
cin>>b;
x++;
}while(b=='y'||b=='Y');
for(int i=0;i<x;i++)
{ a.open("nnshop.dat",ios::in);
while(a.read((char*)&obj,sizeof(obj)))
{
if(obj.PNO()==f[i])
{
q=q+(obj.DIS()*e[i]);
p=p+(obj.MRP()*e[i]);
}
}
a.close();
}
cout<<"\n\t\tTOTAL DISCOUNT ON YOUR PRODUCT IS "<<q<<" Rs";
cout<<"\n\t\tYOU HAVE TO PAY JUST "<<p-q<<" Rs ONLY";
cout<<"\n\t\t!!!!!THANK U FOR UR SHOPPING!!!!!!!!!!!!!";
cout<<"\n\t\t\tKINDLY PLEASE VISIT AGAIN ";
getch();
}



void del()
{
clrscr();
int b;
char e='y';
show();
do
{
ofstream c("temp.dat",ios::out|ios::app);
a.open("nnshop.dat",ios::in|ios::out);
cout<<"\n\n\t\tWHICH PRODUCT U WANT TO DELETE(ENTER S.NO):";
cin>>b;
a.seekg(0,ios::beg);
while(a.read((char*)&obj,sizeof(obj)))
{
if(obj.PNO()!=b)
{
c.write((char*)&obj,sizeof(obj));
}
}
a.close();
c.close();
remove("nnshop.dat");
rename("temp.dat","nnshop.dat");
cout<<"\n\t\tU WANT TO DELETE MORE RECORDS(Y/N):";
cin>>e;
}while(e=='Y'||e=='y');
cout<<"\n\t\t\tDELETING RECORDS....";
delay(800);
cout<<"\n\t\t\tYOUR RECORDS ARE DELETED";
delay(1000);

}


void modify()
{
clrscr();
int b;
char e='y';
show();
do
{
cout<<"\n\n\tWHICH PRODUCT U WANT TO MODIFY(ENTER S.NO):";
cin>>b;
a.open("nnshop.dat",ios::in|ios::out);
while(a.read((char*)&obj,sizeof(obj)))
{
if(obj.PNO()==b)
{
int pos=-1*sizeof(obj);
a.seekp(pos,ios::cur);
obj.add();
a.write((char*)&obj,sizeof(obj));
}
}
cout<<"\n\t\tU WANT TO MODIFY MORE PRODUCTS(Y/N):";
cin>>e;
a.close();
}while(e=='y'||e=='Y');
cout<<"\n\t\tPRODUCTS ARE MODIFYING......";
delay(1000);
cout<<"\n\t\tPRODUCTS MODIFIED.";
delay(800);

}
void shop()
{
int c;
p:
clrscr();
cout<<"\n\t\t\t WELLCOME TO CRATERIFY SHOP"
<<"\n\n\t\t1.Enter NEW PRODUCTS "
<<"\n\t\t2.PURCHASE PRODUCTS"
<<"\n\t\t3.DELETE PRODUCTS"
<<"\n\t\t4.MODIFY PRODUCTS\n\t\t5.EXIT(EMERGENCY DARWAJA)"
<<"\n\n\t\tWHAT WOULD U LIKE TO DO:";
cin>>c;
if(c==1)
{
input();
goto p;
}
else if(c==2)
{
output();
goto p;
}
else if(c==3)
{ del();
goto p;
}
else if(c==4)
{
modify();
goto p;
}
else if(c==5)
exit(0);
else
cout<<"SORRY U HAVE ENTER WRONG OPTION" ;
}

void main()
{
clrscr();
shop();
getch();
}

//code complete


There is file(nnshop.dat) where u have to enter 12 records and then enter 1 more record to understand my question.


Last edited on
Topic archived. No new replies allowed.