This program is suppose to be a file handling program which can store phone no. with their respective names and can add or modify a record
. UNFORTUNATELY HOWEVER I HAVING TRIED MY LEVEL BEST CANNOT DEBUG IT........
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int count=0;
struct Customer
{
char name[40];
long int phone;
};
void getdata(void);
int search_name(char*s);
int search_ph(long ph);
void mad_data();
void getdata(Customer &c)
{
char ch;
cin.get(ch);
clrscr();
gotoxy(15,10);
cout<<"Add Customer Data\n";
gotoxy(17,12);
cout<<"Record#"<<(++count)<<endl;
gotoxy(1,14);
for(int i=0;i<40;i++)
c.name[i]=' ';
c.phone=0;
cout<<"Enter the name:";
cin.getline(c.name,40);
cout<<"\nEnter the phone:";cin>>c.phone;
cout<<"\n";
}
void display(Customer c)
{
clrscr();
gotoxy(15,10);
cout<<"Customer details\n";
gotoxy(1,12);
cout<<"Name:"<<c.name<<"\t"<<"phone:"<<c.phone<<"\n";
}
:
int search_name(char*s)
{
finout.clear();
finout.seekg(0,ios::beg);
int ctr=0;
int flag;
Customer cs;
while(ctr<=count);
{
finout.seekg((ctr*size of(Customer)),ios::beg);
finout.read((char*)&cs,size of(Customer));
ctr++;
getch();
if(strcmpi(cs.name,s)==0)
return ctr;
}
getch();
return 0;
}
int search_ph(long ph)
{
finout.clear();
finout.seekg(0);
int ctr=0;
customer cs;
while(ctr<=count)
{
fin.seekg((ctr*size of(Customer)),ios::beg);
finout.read((char*)&cs,size of(Customer));
ctr++;
getch();
if(cs.phone==ph)
return ctr;
}
getch();
return 0;
}
void mod_data(Customer &c)
{
finout.clear();
char nm[40],cph[8];
clrscr();
gotoxy(15,8);
cout<<"Modify Customer data\n"<<endl;
char ch=cin.get();
cout<<ch;
gotoxy(17,10);
cout<<"Current details\n";
gotoxy(17,11);
cout<<"___________\n\n";
cout<<"Name:"<<c.name<<"\phone:"<<c.phone<<endl;
gotoxy(17,16);
cout<<"Enetr new details\n";
gotoxy(17,17);
cout<<"_____________\n\n";
gotoxy(10,24);
cout<<"If you want to retain old Name, just press Enter:";
gotoxy(50,17);
cout<<endl;
cout<<"Name:";cin.getline(nm,40);
cout<<"Phone:";cin.getline(cph,8);
if(strlen(nm)!=0)
strcpy(c.name,nm);
if (strlen(cph)!=0)
c.phone+atol(cph);
gotoxy(10,24);
clreol();//to clear uptil end of the line
}
int main()
{
clrscr();
Customer cust;
fstream;ftemp;
finout.open("customer.dat",ios::in|ios::opp|ios::binary|ios::ate);
if(!finout)
{
cout<<"Cannot open file!!\n";
getch();
return 1;
}
int last;
fin.seekg(0,ios::end);
last=finout.tdlg();
count=last/size of(cust);
int choice,mrec,offset;long ph;
char nm[40],ans,ch;
for(int i=0;i<40;i++)
nm[i]=' ';
do
{
clrscr();
textclor(12);
textbackground(WHITE);
gotoxy(35,4);cout<<"Main menu:";
gotoxy(15,6);cout<<"1.Add record.";
gotoxy(15,7);cout<<"2.Modify record.";
gotoxy(15,8);cout<<"3.Search for a name.";
gotoxy(15,9);cout<<"4.Search for a phone number.";
gotoxy(15,10);cout<<"5.List.";
gotoxy(15,11);cout<<"6.delete record ny phone number.";
gotoxy(15,12);cout<<"7.Exit.";
gotoxy(15,13);cout<<"Enter your choice...(1-7)::";fflush(std in);
cin>>choice;
switch(choice)
{
case 1:getdata(cust);
finout.clear();
mrec=count;
offset=((mre-1)*size of(Customer));
//place the file pointer
finout.write((char*)&cust,size of(Customer))
if(finout.fail;())
{
cout<<"Record is not written to file.........";
getch();
finout.clear();
goto x;
}
else
cout<<"Record is written to file";
getch();
break;
}
case 2: if(!count)
{
cout<<"No record has been added yet.\n";
cout<<"Please run option 1 first of all\n\n";
gotoxy(10,23);
cout<<"press a key to continue....."<<endl;
getch();
break;
}
cout<<"\n\nModify Which Phone Number?";
cin>>ph;
mrec=search_ph(ph);
if(mrec==0)
{
cout<<"\n\n\n No such phone number exists\n";
gotoxy(15,23);
cout<<"press akey to continue......"<<endl;
getch();
break;
}
else
{
offset=((mrec-1)*size of(Customer));
finout.seekg(offset,ios::beg);//place the file pointer
finout.read((char*)&cust,size of(customer));
display(cust);
cout<<"Modify this record?(y/n)....";
cin>>ans;
if (ans=='y'||ans='n')
{
cout<<"Enter new details\n";
mod_data(cust);
finout.seekp(offset,ios::beg);
finout.write((char*)&cust,size of(Customer));
cout<<"\n Record modified\n";
gotoxy(30,24);
cout<<"press a key to continue..."<<endl;
getch();
}
break;
}
case 3: if(!count)
{
cout<<"\n\n\n\n No record has been added yet.\n";
cout<<"Please run option 1 first of all..\n\n";
gotoxy(10,23);
cout<<"Press akey to continue....";
getch();
break;
}
cout<<"\n\nSearch Which Name?";
cin.getline(nm,40);
mrec=search_name(nm);
if (mrec==0)
{
cout<<"\n\n\n No such name exists.\n";
gotoxy(15,23);
cout<<"Press akey to continue...\n";
getch();
}
break;
case 4: if(!count)
{
cout<<"\n\n\nNO record has been added yet.\n";
cout<<"Please run option 1 first of all..\n\n";
gotoxy(10,23);
cout<<"Press akey to continue....";
getch();
break;
}
cout<<"\n\nSearch which phone number?";
cin>>ph;
mrec=search_ph(ph);
if(mrec==0)
{cout<<"\n\n\n No such phone number exists....\n";
gotoxy(15,23);
cout<<"press a key to continue...."<<endl;
getch();
break;
}
else
{
(offset=(mrec-1)*size of(Customer));
finout.seekg(offset,ios::beg);
finout.read((char*)&cust,size of(Customer));
display(cust);
gotoxy(10,23);
cout<<"Press a key to continue....";
getch();
}
break;
case 5: clrscr();
finout.clear();
finout.seekg(0);
while(finout.read((char*)&cust,size of(cust)))
{
cout<<"\nName:"<<cust.name<<"\t Phone:"<<cust.phone;
getch();
break;
case 6: long tph;
cout<<"\nEnter the phone number:";
cin>>tph;
del_data(tph);
cout<<"\n Record is completely deleted";
getch();
break;
case 7: exit(0);
while(finout.read((char*)&cust,size of(cust)))
{
if (cust.phone!=tph)
{
ftemp.write((char*)&cust,size of(Customer));
}
}
default:cout<<"Wrong choice!! Valid choices are(1-7)\n.";
getch();
}
while(1)
return(0);
}
void del_dat(long tph)
{
Customer cust;
fstream ftemp;ftemp.open("temp.dat",ios::app|ios::bin);
finout.clear();
ftemp.clear();
finout.seekg(0);
finout.close();
ftemp.close();
remove("Customer.dat");
rename("temp.dat","Customer.dat");
finout.open("customer.dat",ios::in|ios::app|ios::binary|ios::ate);
}
PLEASE I HAVE POSTED THIS COMMENT AFTER BEING THOROUGHLY FRUSTRATED.......

New Topic/Question
Reply



MultiQuote




|