I made a program in c++ for courier billing/ invoice generation
The program takes the details from the user.
Saves the details in a file.
the details can be displayed again in an organized manner
The problem is that I want the file in which the details are being saved to the details exactly as they are being displayed again.
So the file can be used for printing
Thanks
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>
class entry
{
char cn[10];
char dest[50];
char ccode[20];
char date[20];
int wt,rate,ch,wtf;
public:
void create_entry()
{
cout<<"\nEntry Details...\n";
cout<<"\nEnter the date";
gets(date);
cout<<"\n\nEnter Client Code";
gets(ccode);
cout<<"\n\nEnter Cong Number";
gets(cn);
cout<<"\n\nEnter Destination";
gets(dest);
cout<<"\n\nEnter Weight";
cin>>wt;
cout<<"\n\n\nEntry Completed..";
cust_entry();
}
void show_entry()
{
cout<<"\nDate : ";
puts(date);
cout<<"\nClient Code : ";
puts(ccode);
cout<<"\nDestination : ";
puts(dest);
cout<<"\nWeight : "<<wt;
cout<<"\n\nRate : "<<rate;
}
void modify_entry()
{
cout<<"\nEnter Cong Number : ";
gets(cn);
cout<<"\nModify Client Code : ";
gets(ccode);
cout<<"\nModify Destination : ";
gets(dest);
cout<<"Modify Date : ";
gets(date);
cout<<"\nModify Weight : ";
cin>>wt;
cust_entry();
}
void cust_entry()
{
if(strcmp(ccode,"AB000")==0)
{
if(strcmp(dest,"GN")==0 || strcmp(dest,"NOD")==0 || strcmp(dest,"ND")==0 || strcmp(dest,"NCR")==0)
{
if(wt<=100)
rate=20;
else if(wt>=101 && wt<=250)
rate=30;
else if(wt>=251 && wt<=500)
rate=100;
else
{
wtf=wt;
if(wtf%500==0)
{
wtf=wtf/500;
wtf=wtf-1;
rate=(100+(wtf*25));
}
else
{
wtf=wtf/500;
rate=(100+(wtf*25));
}
}
}
else if(strcmp(dest,"NORTH")==0)
rate=50;
else if(strcmp(dest,"METRO")==0)
rate=70;
else
rate=100;
}
if(strcmp(ccode,"CAS00")==0)
{
if(strcmp(dest,"GN")==0 || strcmp(dest,"NOD")==0 || strcmp(dest,"ND")==0 || strcmp(dest,"NCR")==0)
rate=30;
else if(strcmp(dest,"NORTH")==0)
rate=60;
else if(strcmp(dest,"METRO")==0)
rate=80;
else
rate=110;
}
}
char* retcn()
{
return cn;
}
void report()
{
cout<<cn<<setw(15)<<date<<setw(15)<<ccode<<setw(15)<<dest<<setw(10)<<wt<<setw(10)<<rate<<endl;}
};
fstream fp,fp1;
entry dt ;
void write_entry()
{
char ch;
fp.open("entry.dat",ios::out|ios::app);
do
{
clrscr();
dt.create_entry();
fp.write((char*)&dt,sizeof(entry));
cout<<"\n\nDo you want to add more record..(y/n?)";
cin>>ch;
}while(ch=='y'||ch=='Y');
fp.close();
}
void display_spb(char n[])
{
cout<<"\nEntry DETAILS\n";
int flag=0;
fp.open("entry.dat",ios::in);
while(fp.read((char*)&dt,sizeof(entry)))
{
if(strcmpi(dt.retcn(),n)==0)
{
dt.show_entry();
flag=1;
}
}
fp.close();
if(flag==0)
cout<<"\n\nEntry does not exist";
getch();
}
void modify_entry()
{
char n[6];
int found=0;
clrscr();
cout<<"\n\n\tMODIFY Entry REOCORD.... ";
cout<<"\n\n\tEnter The cong no. of The entry";
cin>>n;
fp.open("entry.dat",ios::in|ios::out);
while(fp.read((char*)&dt,sizeof(entry)) && found==0)
{
if(strcmpi(dt.retcn(),n)==0)
{
dt.show_entry();
cout<<"\nEnter The New Details of cong"<<endl;
dt.modify_entry();
int pos=-1*sizeof(dt);
fp.seekp(pos,ios::cur);
fp.write((char*)&dt,sizeof(entry));
cout<<"\n\n\t Record Updated";
found=1;
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}
void delete_entry()
{
char n[6];
clrscr();
cout<<"\n\n\n\tDELETE Entry ...";
cout<<"\n\nEnter The cong no. of the Entry You Want To Delete : ";
cin>>n;
fp.open("entry.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&dt,sizeof(entry)))
{
if(strcmpi(dt.retcn(),n)!=0)
{
fp2.write((char*)&dt,sizeof(entry));
}
}
fp2.close();
fp.close();
remove("entry.dat");
rename("Temp.dat","entry.dat");
cout<<"\n\n\tRecord Deleted ..";
getch();
}
void display_allb()
{
clrscr();
fp.open("entry.dat",ios::in);
if(!fp)
{
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch();
return;
}
cout<<"\n\n\t\tEntry LIST\n\n";
cout<<"===============================================================================\n";
cout<<"Cong Number"<<setw(10)<<"Date"<<setw(20)<<"Client Code"<<setw(15)<<"Destination"<<setw(10)<<"Weight"<<setw(10)<<"Rate"<<"\n";
cout<<"===============================================================================\n";
while(fp.read((char*)&dt,sizeof(entry)))
{
dt.report();
}
fp.close();
getch();
}
void intro()
{
clrscr();
gotoxy(36,11);
cout<<"COURIER";
gotoxy(35,14);
cout<<"MANAGEMENT";
gotoxy(36,17);
cout<<"SYSTEM";
getch();
}
void admin_menu()
{
clrscr();
int ch2;
cout<<"\n\n\n\tADMINISTRATOR MENU";
cout<<"\n\n\t1.Enter Data ";
cout<<"\n\n\t2.DISPLAY ALL Entries ";
cout<<"\n\n\t3.DISPLAY SPECIFIC Entry ";
cout<<"\n\n\t4.MODIFY Entry ";
cout<<"\n\n\t5.DELETE Entry ";
cout<<"\n\n\t6.BACK TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (4-6) ";
cin>>ch2;
switch(ch2)
{
case 1: clrscr();
write_entry();break;
case 2: display_allb();break;
case 3: {
char num[6];
clrscr();
cout<<"\n\n\tPlease Enter The Cong No. ";
cin>>num;
display_spb(num);
break;
}
case 4: modify_entry();break;
case 5: delete_entry ();break;
case 6: return;
default:cout<<"\a";
}
admin_menu();
}
void main()
{
char ch;
intro();
do
{
clrscr();
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. Billing Entry ";
cout<<"\n\n\t02. Sorting Entry";
cout<<"\n\n\t03. ADMINISTRATOR MENU";
cout<<"\n\n\t04. EXIT";
cout<<"\n\n\tPlease Select Your Option (1-4) ";
ch=getche();
switch(ch)
{
case '1':clrscr();
break;
case '2':break;
case '3':admin_menu();
break;
case '4':exit(0);
default :cout<<"\a";
}
}while(ch!='4');
}