Create a C++ program in Simple Cash register
Question
Create a C++ program in a Simple Cash Register – (Grocery Store)
-
- Transaction
- Product or service availed by the customer
- Paying and change
- TAX
Summary
In this question, we have to write a C++ code for a Simple Cash Register (for grocery stores). In order to create this simple cash register, we have to consider four factors majorly.
We have to do the transaction part, in that part we have to calculate the total cost for each customer for the products he has bought.
We have to handle the products that have been bought by the customers.
After getting the products bought by the customers, we have to get the total cost and take money from them, and we have to return the change also if it is more than the cost.
After that, we will add the tax of 1% on the calculated total cost also.
At last, we will print the output of the BILL.
Explanation
In order to write the program for this, we will first declare an array of strings for the available products in the grocery, and we declare one more array of double type for storing the corresponding cost of the available products. We will also take the products input from the user and for storing the products we will create one more array of string types. After that, we will check if the product item is chosen by the user or not, if the user chooses it we will add its cost otherwise we will leave that product. The user can choose only two products. We will use a nested loop to know the chosen products. After getting the total cost we will add 1% of the tax to it. And at last, we will collect the money from the user if the money is more than the total cost, we will also give a change to the user. And we will print the BILL.
Code
#include <iostream> using namespace std; #include <iomanip> int main(){ /* names of products */ string items[5]={"chocolates","rice","wheat","milk","sweets"}; /* cost of products */ double costs[5]={10,20,30,40,50}; string products_bought[2]; double bought_cost[2]; /* to get the products from customer */ cout<<"Enter the products to be bought: "<<endl; for(int i=0;i<2;i++) cin>>products_bought[i]; double total_cost=0.0; /* to calculate the total cost */ for(int i=0;i<2;i++){ for(int j=0;j<5;j++){ if(products_bought[i]==items[j]){ bought_cost[i]=costs[j]; total_cost+=costs[j]; break; } } } /* to claculate tax */ double tax=total_cost/100.0; double total=total_cost+tax; /* to give the chnage */ double amount_given; cout<<"Give the cash: "; cin>>amount_given; double change=amount_given-total; cout<<"\n\n-------------------BILL------------------"<<endl; for(int i=0;i<2;i++){ cout<<left; cout<<setw(15)<<products_bought[i]<<setw(10)<<bought_cost[i]<<endl; } cout<<setw(15)<<"Total Cost"<<setw(10)<<total_cost<<endl; cout<<setw(15)<<"Tax"<<setw(10)<<tax<<endl; cout<<setw(15)<<"Total"<<setw(10)<<total<<endl; cout<<"------------------------------------------"<<endl<<endl; cout<<"Take your change of "<<change<<" rupees"<<endl; }
Output
Also read, An application for a patient visits using queue in C++