Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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

C++
//***************************************************************
//                   HEADER FILE USED IN PROJECT
//****************************************************************

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

//***************************************************************
//                   CLASS USED IN PROJECT
//****************************************************************



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;
	 }

	}
/*
	void amount_entry()
	{

	      if(strcmp(dest,"up")==0)
	       rate=50;
	      else if(strcmp(dest,"nd")==0)
	       rate=20;
	      else
	       rate=100;

	}
	    */
	char* retcn()
	{
		return cn;
	}

	void report()
	{
	 cout<<cn<<setw(15)<<date<<setw(15)<<ccode<<setw(15)<<dest<<setw(10)<<wt<<setw(10)<<rate<<endl;}


	};         //class ends here


//***************************************************************
//    	global declaration for stream object, object
//****************************************************************

fstream fp,fp1;
entry dt ;


//***************************************************************
//    	function to write in file
//****************************************************************

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();
}


//***************************************************************
//    	function to read specific record from file
//****************************************************************


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();
}


//***************************************************************
//    	function to modify record of file
//****************************************************************


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();
}


//***************************************************************
//    	function to delete record of file
//****************************************************************


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();
}


//***************************************************************
//    	function to display Entry list
//****************************************************************

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();
}


//***************************************************************
//    	INTRODUCTION FUNCTION
//****************************************************************

void intro()
{
	clrscr();
	gotoxy(36,11);
	cout<<"COURIER";
	gotoxy(35,14);
	cout<<"MANAGEMENT";
	gotoxy(36,17);
	cout<<"SYSTEM";
	getch();
}



//***************************************************************
//    	ADMINISTRATOR MENU FUNCTION
//****************************************************************

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();
}


//***************************************************************
//    	THE MAIN FUNCTION OF PROGRAM
//****************************************************************


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');
}

//***************************************************************
//    			END OF PROJECT
//***************************************************************
Posted
Updated 19-Aug-12 14:18pm
v4
Comments
[no name] 19-Aug-12 11:23am    
And your problem is what exactly? What do you mean that it's text but not readable?
Blackdragon_adi 19-Aug-12 12:45pm    
I meant that the contents of the text file are not in a proper manner
[no name] 19-Aug-12 12:47pm    
That means absolutely nothing to anyone but you. What is the "proper manner" and how is your file not in the "proper manner"? We cannot see your code, your project or read your mind.
Richard MacCutchan 19-Aug-12 11:56am    
Write another program that can read the file and create the invoices; that's what most people would do.
Blackdragon_adi 19-Aug-12 12:46pm    
the text file has to printed as a bill

Wow - that has to be the most illegible question, ever. If you wrote the program, then the output is under your control. If you want the output to look different, change your code. If you have a specific question, post the code and explain specifically what's wrong and why you're stuck.
 
Share this answer
 
Comments
Blackdragon_adi 19-Aug-12 20:19pm    
I have posted the code
Christian Graus 19-Aug-12 20:27pm    
Well, two of your case statements do nothing, and one does next to nothing. fstream.h is not valid C++, you need to include fstream ( no .h ). That's a huge block of code. However, there's no reason I can see why you can't use tabs as a delimiter, which means you can have a file that is printable, as well as being readable from your code.
Blackdragon_adi 20-Aug-12 0:27am    
In the file it is not coming in a readable form like in the program
I don't know if this is your problem or not, but here I'm pasting you a link were it explains the ways files can be used:

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/[^]

you can create the file in different methods, and if you want to create it in a way in which you can use notepad (as an example) to read it then you must use text mode. if you serialize a complete structure in binary mode then you won't be able to read it easily.

Take a look at this link and improve your question if you need further help.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900