#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>

struct customerrecord
{
char customerno[3];
char customerphone[9];
double balance;
double totalpay;
};

int getmenu();
void addnewcustomer(fstream&);
void deleterec(fstream&);
void modifycustomerrec(fstream&);
int getfield();
void dispfile(fstream&);
int validaterecordno(long);

int main()
{
int choice;
int quit=0;
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
fstream custfile;
customerrecord emptycustrec={"","",0.0,0.0};
custfile.open("custfile.dat",ios::in|ios::binary);
 if(custfile.fail())
 {
custfile.open("custfile.dat",ios::out|ios::binary);
for(long recordno=1l;recordno<=999l;++recordno)
custfile.write((char*)&emptycustrec,sizeof(customerrecord));
cout<<"\nnull file created";
}
custfile.close();

custfile.open("custfile.dat",ios::in|ios::out|ios::binary);

do
{
choice=getmenu();
switch(choice)
{
case 1:
addnewcustomer(custfile);break;
case 2:
deleterec(custfile);break;
case 3:
modifycustomerrec(custfile);break;
case 4:
dispfile(custfile);break;
case 5:
quit=1;break;
default :
cout<<"\n\ninvalid choice. \n";break;}
}
while(!quit);

custfile.close();
return 0;
}

int getmenu()
{
int choice;
cout<<"\n\nmain menu";
cout<<"\n\n1-add a new customer"
<<"\n\n2-delete a customer"
<<"\n\n3-modify a customer record"
<<"\n\n4-display the customers file"
<<"\n\n5-exit";

cout<<"\n\nenter your choice:";
cin>>choice;
return choice;
}

void addnewcustomre(fstream& file)
{
customerrecord cust;
customerrecord tempcustrec;
long recordno;
//cout<<"enter get";
cin.get();
cout<<"\nenter the data for a customer:";
cout<<"\n\ncustomer number(01-999):";
cin.getline(cust.customerno,3);

recordno=atol(cust.customerno);
if(!validaterecordno(recordno))
return;
cout<<"\ncustomer phone:";
cin>>cust.customerphone;
cout<<"\nbalance:";
cin>>cust.balance;
cout<<"\ntotal payments:";
cin>>cust.totalpay;

file.seekg((recordno-1)*sizeof(customerrecord),ios::beg);
file.read((char*)&tempcustrec,sizeof(customerrecord));
if(strcmp(tempcustrec.customerno,"")==0)
{
file.seekg(-(long)sizeof(customerrecord),ios::cur);
file.write((char*)&cust,sizeof(customerrecord));
cout<<"\nrecord"<<cust.customerno
<<"added to file";
}
else
cout<<"\nthis customer already exists";
}

void deleterec(fstream&file)
{
customerrecord cust;
customerrecord emptycustrec={"","",0.0,0.0};
long recordno;
cout<<"\nthe ID of thed customer to delete:";
cin>>recordno;
if(!validaterecordno(recordno))
return;
file.seekg((recordno-1)*sizeof(customerrecord),ios::beg);
file.read((char*)&cust,sizeof(customerrecord));
if(strcmp(cust.customerno,"")==0)
cout<<"\nthe record does not exist.";

else
{
file.seekg(-(long)sizeof(customerrecord),ios::cur);
file.write((char*)&emptycustrec,sizeof(customerrecord));
cout<<"\nrecord "<<recordno<<" is delete";
}

}

void modifycustomerrec(fstream& file)
{
customerrecord cust;
long recordno;
int change;
int quit=0;
cout<<"\nenter number of record to modify:";
cin>>recordno;
if(!validaterecordno(recordno))
return;
file.seekg((recordno- 1)*sizeof(customerrecord),ios::beg);
file.read((char*)&cust,sizeof(customerrecord));
if(strcmp(cust.customerno,"")==0)
{
cout<<"\nthe customer does not exist.";return;
}
cout<<"\nthe current record contents are:\n\n";
cout<<setw(20)<<"customer number"
<<setw(20)<<"phone"
<<setw(20)<<"balance"<<"total payments"
<<endl;
cout<<setw(20)<<cust.customerno
<<setw(20)<<cust.customerphone
<<setw(20)<<cust.balance
<<setw(20)<<cust.totalpay;

do
{
change=getfield();
switch(change)
{
case 1:
cout<<"\nenter customer phone:";
cin>>cust.customerphone;
break;

case 2:
cout<<"\nenter balance:";
cin>>cust.balance;
break;

case 3:
cout<<"\nenter total payments:";
cin>>cust.totalpay;
break;

case 4:
quit=1;
file.seekg(-(long)sizeof(customerrecord),ios::cur);
file.write((char*)&cust,sizeof(customerrecord));
cout<<"\nchangemade to record "
<<recordno;
break;

default :
cout<<"\ninvalid choice. try again";
break;
}
}
while(!quit);
}

int getfield()
{
int change;
cout<<"\n\nchange menu\n\n";
cout<<"1-change customer phone\n"
<<"2-change balance\n"
<<"3-change total payments:\n"
<<"4-write changes and return to menu\n\n";
cout<<"enter your choice:";
cin>>change;
return change;
}

void dispfile(fstream&file)
{
customerrecord cust;
long recordno;
long counter=0l;
file.seekg(0l,ios::beg);
cout<<setw(20)<<"\ncustomer number"
<<setw(20)<<"customer phone"
<<setw(20)<<"balance"
<<setw(20)<<"total payments:"<<endl;
for(recordno=1l;recordno<=99l;++recordno)
{
file.read((char*)&cust,sizeof(customerrecord));
	if(strcmp(cust.customerno,"")!=0)
	{
	cout<<setw(20)<<cust.customerno
	<<setw(20)<<cust.customerphone
	<<setw(20)<<cust.balance
	<<setw(20)<<cust.totalpay<<endl;
	++counter;
	}
}
cout<<"\n\nfile contains "<<counter<<"records.";
}

int validaterecordno(long recordno)
{
if(recordno<1l||recordno>999l)
{
cout<<"\n\nerror:invalid record number ";
return 0;
}
else
return 1;
}

