now you have complete the project and want to add some extra feature like document printing or receipt etc. Here in this article we will learn how to print receipt in C++, invoice or student marksheet also. This is the simple demonstration of printing an invoice/receipt.
Remember here, you need to learn File handling concept in C++ before proceeding to below program.
Let’s start,
Document also print date and time which will depend on your system data/time, the following code will be used to fetch the system date and time-
time_t current = time(0);
char* dt = ctime(¤t);
Here is the Program,
Print receipt in C++
#include<fstream> // file handling
#include<iomanip> // setw, fetch system date and time
using namespace std;
time_t current = time(0);
char* dt = ctime(¤t);
int main ()
{
char title[20]="cppHindiTutorials";
char student[20]="Rahul";
int mobile = 1111111;
ofstream fout("invoice.txt"); // txt file create
fout<<"\n\t";
for(int i=0;i<48;i++)
fout<<"_";
fout<<endl; // next line
fout<<setw(35)<<"Card\n";
fout<<setw(45)<<"______________________"<<setw(14)<<"\n";
fout<<"\t"<<setw(48);
fout<<"\n\t\tTITLE..."<<title;
fout<<"\n\n\t\tMOBILE NO...........+91";
fout<<mobile<<setw(9)<<"\n";
fout<<setw(48);
fout<<"\n\tDate and Time........";
fout<<dt;
fout<<"\n\n\t\t >>Thank You For Visiting<<";
fout<<setw(13)<<"\n";
fout<<setw(49)<<"\n\n";
fout<<setw(30)<<"* * *"<<setw(24)<<"\n";
fout<<setw(48);
fout<<"\n\t";
for(int j=0;j<48;j++)
fout<<"_";
return 0;
}
OUTPUT
________________________________________________
Card
______________________
TITLE...cppHindiTutorials
MOBILE NO...........+9124242452
Date and Time........Fri Jun 11 10:13:20 2021
>>Thank You For Visiting<<
* * *
________________________________________________
This program will generate a file (invoice.txt)which will be located in you directory where you save all your C++ project.
This is the also an example of text file in C++. Do you know what is text file in C++ read. this article text format vs binary format in C++
- we can also code in the program where document will go to direct for printing machine and mail to the user.
- sometime
"\t"
and"setw()"
can misbehave in different system so replace them with each other. - in the above program we generated a text file but our requirement we can also generate other formats like pdf, ppt, ms word, html document etc.
- Above receipt has up and bottom horizontal line but what will be the code if we give complete border left and right(vertical line). maybe you should read this article where print student marksheet in C++.
Did You understand well ?