Creating a supermarket billing system in C++ is a project that involves developing a basic console application, devoid of any graphical interface. Through this project, you can gain knowledge on utilizing the stream class and handling files within the C++ programming realm.
What is exactly a Supermarket Bill?
The purpose of the supermarket billing system is to assist supermarkets in computing and displaying invoices efficiently, while also enhancing customer service with quicker and more efficient processes. The software's user interface is designed to be both effective and easy to use, supporting staff in tasks related to customer service and invoice generation.
Why does Supermarket need a billing system?
They have the ability to manage their finances effectively by utilizing billing software, which stands out as an excellent choice for various types of grocery store businesses. An effective billing application can maintain precise financial documentation and reduce errors that may occur during manual data input.
What Functions Do Supermarket Billing Systems Serve?
By maintaining transparency in the billing system and accurately specifying the total amount for purchases, the supermarket's billing system plays a vital role in maintaining a positive relationship between customers and store management. This system facilitates and ensures timely payment by consumers.
Import the Necessary Libraries
Identifying the essential C++ libraries needed for our software is the initial task at hand. These library requirements will be outlined at the beginning of the presentation. To include the necessary header files, we utilize the #include directive from a library. The primary library our application requires is iostream. This specific library will be employed to handle the functionalities of our Supermarket Billing Project by utilizing basic input and output operations in C++.
Program Breakdown
# include < iostream >
# include < process.h >
# include < fstream >
# include < stdlib.h >
We will include all the essential libraries to prevent any potential errors.
class head
{
char Iname [ 50 ] [ 50 ] ;
public :
int totalitems ;
float Qty [ 3 ] ;
float price [ 3 ] ;
int vatprice [ 3 ] ;
int tprice [ 3 ] ;
void input ( ) ;
void output ( ) ;
} ;
In the provided code snippet, a head class has been defined with member functions such as name, total items, quantity, price, input, and output.
class vat : public head
{
float vats ;
public :
void vatcal ( ) ;
void outputs ( ) ;
void sum ( ) ;
} ;
In the provided snippet, a class named vat has been created by inheriting from the head class.
void head : : input ( )
{
system ( " CLS " ) ;
cout << " \ nEnter number of items = " ;
cin >> totalitems ;
for ( int i = 0 ; i < totalitems ; i + + )
{
cout << " \ n Enter name of item " << i + 1 << " : " ;
cin >> Iname [ i ] ;
cout << " Enter quantity : " ;
cin >> Qty [ i ] ;
cout << " Enter price of item " << i + 1 << " : " ;
cin >> price [ i ] ;
tprice [ i ] = Qty [ i ] * price [ i ] ;
}
}
In the aforementioned scenario, it will handle tasks such as inputting the number of items, specifying item names, indicating item quantities, and entering their respective prices.
void head : : output ( )
{
int a ;
ifstream infile ( " COUNT . TXT " ) ;
infile >> a ;
ofstream outfile ( " COUNT . TXT " ) ;
a + = 1 ;
outfile << a ;
outfile . close ( ) ;
{ ofstream outfile ( " HIS . TXT " , ios : : app ) ;
outfile << endl << " Bill No . : " << a << endl ;
outfile << " ------------------------------------------------------------------------ " << endl ;
cout << " \ n " ;
cout << " Name of Item \ t Quantity Price Total Price \ n " ;
for ( int i = 0 ; i < totalitems ; i + + )
{
outfile << " Name : " << Iname [ i ] << " Qty : " << Qty [ i ] << " Price : " << tprice [ i ] << endl ;
cout << Iname [ i ] << " \ t \ t " << Qty [ i ] << " \ t " << price [ i ] << " \ t " << tprice [ i ] << ' \ n ' ;
}
outfile << " ------------------------------------------------------------------------ " << endl ;
outfile . close ( ) ;
}
}
The provided snippet is responsible for showcasing the items inputted by the user.
void vat : : vatcal ( )
{
input ( ) ;
for ( int i = 0 ; i < totalitems ; i + + )
{
if ( price [ i ] < = 100.00 )
{
vatprice [ i ] = tprice [ i ] + ( 0.03 * tprice [ i ] ) ;
}
else
{
vatprice [ i ] = tprice [ i ] + ( 0.1 * tprice [ i ] ) ;
}
}
} void vat : : vatcal ( )
{
input ( ) ;
for ( int i = 0 ; i < totalitems ; i + + )
{
if ( price [ i ] < = 100.00 )
{
vatprice [ i ] = tprice [ i ] + ( 0.03 * tprice [ i ] ) ;
}
else
{
vatprice [ i ] = tprice [ i ] + ( 0.1 * tprice [ i ] ) ;
}
}
}
The code snippet above is utilized to determine the quantity of items input by the user.
void vat : : outputs ( )
{
output ( ) ;
float cash = 0 , sum = 0 , qty = 0 , sumt = 0 ;
for ( int i = 0 ; i < totalitems ; i + + )
{
sumt + = tprice [ i ] ;
sum + = vatprice [ i ] ;
qty + = Qty [ i ] ;
}
cout << " \ n Total : " ;
cout << " \ n ------------------------------------------------------------------------------ " ;
cout << " \ n \ t Quantity = " << qty << " \ t \ t Sum = " << sumt << " \ tWith Vat : " << sum ;
cout << " \ n------------------------------------------------------------------------------ " ;
pay :
cout << " \ n \ n \ t \ t \ t **** PAYMENT SUMMARY **** \ n " ;
cout << " \ n \ t \ t \ t Total cash given : " ;
cin >> cash ;
if ( cash > = sum )
cout << " \ n \ t \ t \ t Total cash repaid : " << cash-sum << ' \ n ' ;
else
{ cout << " \ n \ t \ t \ t Cash given is less than total amount!!! " ;
goto pay ;
}
}
The code snippet above displays the calculated price of the items input by the user.
int passwords ( )
{
char p1 , p2 , p3 ;
cout << " \ n \ n \ n \ n \ n \ n \ t \ t \ t ENTER THE PASSWORD : " ;
cin >> p1 ;
cout << " * " ;
cin >> p2 ;
cout << " * " ;
cin >> p3 ;
cout << " * " ;
if ( ( p1 == ' s ' || p1 == ' S ' ) && ( p2 == ' i ' || p2 == ' I ' ) && ( p3 == ' d ' || p3 == ' D ' ) )
return 1 ;
else
return 0 ;
}
The provided code snippet enables users to input their password into the billing system, enhancing security measures for the supermarket's billing system.
int main ( )
{
vat obj ;
char opt , ch ;
int a = 1 ;
ifstream fin ;
a==passwords ( ) ;
if ( ! a )
{
for ( int i = 0 ; i < 2 ; i + + )
{
cout << " \ n Wrong password try once more \ n " ;
if ( passwords ( ) )
{
goto last ;
}
else
{
cout << " \ n \ n \ t \ t \ t all attempts failed ..... " ;
cout << " \ n \ n \ n \ t \ t \ t see you .................. " ;
exit ( 0 ) ;
}
}
cout << " \ t \ t \ t sorry all attempts failed ............. \ n \ t \ t \ t inactive " ;
}
The provided code serves as the main driver for our supermarket billing system. Here, the user will be prompted to input a password. Should the user input an incorrect password, the system will notify them with a message stating "Incorrect password, please try again."
else {
last : ;
do {
start :
system ( " PAUSE " ) ;
system ( " CLS " ) ;
cout << " \ n \ n \ t \ t \ t ------------------------------ " ;
cout << " \ n \ t \ t \ t Billing Management System " ;
cout << " \ n \ t \ t \ t ------------------------------ " ;
cout <<"\n\n\t\t\tWhat you want to do?";
cout<<"\n\t\t\t1.\tTo enter new entry\n\t\t\t2.\tTo view previous entries\n\t\t\t3.\tExit\n";
cout<<"\n\nEnter your option: ";
cin>>opt;
If the user enters the correct password, the loop mentioned above will be activated.
switch ( opt )
{
case ' 1 ' :
obj . vatcal ( ) ;
obj . outputs ( ) ;
goto start ;
case ' 2 ' :
fin . open ( " HIS . TXT " , ios : : in ) ;
while ( fin . get ( ch ) )
{
cout << ch ;
}
fin . close ( ) ;
goto start ;
case ' 3 ' :
exit ( 0 ) ;
default :
cout << " \ a " ;
}
} while ( opt ! = 3 ) ;
}
return 0 ;
}
The loop mentioned above will iterate based on the option chosen by the user.
Program for Supermarket Billing System
#include<iostream>
#include<process.h>
#include<fstream>
#include <stdlib.h>
using namespace std;
class head
{
char Iname[50][50];
public:
int totalitems;
float Qty[3];
float price[3];
int vatprice[3];
int tprice[3];
void input();
void output();
};
class vat:public head
{
float vats;
public:
void vatcal();
void outputs();
void sum();
};
//******************************************************************
// INPUT FUNCTION
//******************************************************************
void head::input()
{
system("CLS");
cout<<"\nEnter number of items= ";
cin>>totalitems;
for(int i=0; i<totalitems; i++)
{
cout<<"\nEnter name of item "<<i+1<<": ";
cin>>Iname[i];
cout<<"Enter quantity: ";
cin>>Qty[i];
cout<<"Enter price of item "<<i+1<<": ";
cin>>price[i];
tprice[i]=Qty[i]*price[i];
}
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// OUTPUT FUNCTION
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
void head::output()
{
int a;
ifstream infile("COUNT.TXT");
infile>>a;
ofstream outfile("COUNT.TXT");
a+=1;
outfile<<a;
outfile.close();
{ofstream outfile("HIS.TXT", ios::app);
outfile<<endl<<"Bill No.: "<<a<<endl;
outfile<<"------------------------------------------------------------------------"<<endl;
cout<<"\n";
cout<<"Name of Item\tQuantity Price Total Price\n";
for(int i=0;i<totalitems;i++)
{
outfile<<"Name: "<<Iname[i]<<" Qty: "<<Qty[i]<<" Price: "<<tprice[i]<<endl;
cout<<Iname[i]<<"\t\t"<<Qty[i]<<"\t "<<price[i]<<"\t "<<tprice[i]<<'\n';
}
outfile<<"------------------------------------------------------------------------"<<endl;
outfile.close();
}
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// VAT CALCULATION
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
void vat::vatcal()
{
input();
for(int i=0;i<totalitems;i++)
{
if(price[i]<=100.00)
{
vatprice[i]=tprice[i]+(0.03*tprice[i]);
}
else
{
vatprice[i]=tprice[i]+(0.1*tprice[i]);
}
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// VAT OUTPUTS
//* * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * *
void vat::outputs()
{
output();
float cash=0,sum=0,qty=0,sumt=0;
for(int i=0;i<totalitems;i++)
{
sumt+=tprice[i];
sum+=vatprice[i];
qty+=Qty[i];
}
cout<<"\nTotal:";
cout<<"\n------------------------------------------------------------------------------";
cout<<"\n\tQuantity= "<<qty<<"\t\t Sum= "<<sumt<<"\tWith Vat:"<<sum;
cout<<"\n------------------------------------------------------------------------------";
pay:
cout<<"\n\n\t\t\t * * * * PAYMENT SUMMARY * * * * \n";
cout<<"\n\t\t\tTotal cash given: ";
cin>>cash;
if(cash>=sum)
cout<<"\n\t\t\tTotal cash repaid: "<<cash-sum<<'\n';
else
{ cout<<"\n\t\t\tCash given is less than total amount!!!";
goto pay;
}
}
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// PROTECTION PASSWORD
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
int passwords()
{
char p1,p2,p3;
cout<<"\n\n\n\n\n\n\t\t\tENTER THE PASSWORD: ";
cin>>p1;
cout<<"*";
cin>>p2;
cout<<"*";
cin>>p3;
cout<<"*";
if ((p1=='s'||p1=='S')&&(p2=='i'||p2=='I')&&(p3=='d'||p3=='D'))
return 1;
else
return 0;
}
// END of Password.
//****************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************
int main()
{
vat obj;
char opt, ch;
int a=1;
ifstream fin;
a==passwords();
if(!a)
{
for(int i=0;i<2;i++)
{
cout<<"\nWrong password try once more\n";
if(passwords())
{
goto last;
}
else
{
cout<<"\n\n\t\t\t all attempts failed.....";
cout<<"\n\n\n\t\t\t see you.................. ";
exit(0);
}
}
cout<<"\t\t\t sorry all attempts failed............. \n \t\t\tinactive";
}
else{
last:;
do{
start:
system("PAUSE");
system("CLS");
cout<<"\n\n\t\t\t------------------------------";
cout<<"\n\t\t\tBilling Management System";
cout<<"\n\t\t\t------------------------------";
cout<<"\n\n\t\t\tWhat you want to do?";
cout<<"\n\t\t\t1.\tTo enter new entry\n\t\t\t2.\tTo view previous entries\n\t\t\t3.\tExit\n";
cout<<"\n\nEnter your option: ";
cin>>opt;
switch(opt)
{
case'1':
obj.vatcal();
obj.outputs();
goto start;
case'2':
fin.open("HIS.TXT", ios::in);
while(fin.get(ch))
{
cout<<ch;
}
fin.close();
goto start;
case'3':
exit(0);
default:
cout<<"\a";
}
}while(opt!=3);
}
return 0;
}
Output:
ENTER THE PASSWORD: 123
***Press any key to continue . . .
---------------------------------------
Billing Management System
---------------------------------------
What you want to do?
1. To enter new entry
2. To view previous entries
3. Exit
Enter your option: 1
Enter number of items= 3
Enter name of item 1: Vegetables
Enter quantity: 1kg
Enter price of item 1:
Enter name of item 2: Enter quantity: Enter price of item 2:
Enter name of item 3: Enter quantity: Enter price of item 3:
Name of Item Quantity Price Total Price
Vegetables 1 0 0
0 1.4013e-045 0
1.13027e-038 0 0
Total:
------------------------------------------------------------------------------
Quantity= 1 Sum= 0 With Vat:0
------------------------------------------------------------------------------
****PAYMENT SUMMARY****
Total cash given:
Total cash repaid: 0
Press any key to continue . . .