Answer to Question #290192 in C++ for xinix

Question #290192

Cashier is a C++ program that displays on the screen item codes with corresponding item description and price (at least 3 items). It asks the user to enter the code of the item purchased by a customer. It looks for a match of the item code stored in items.txt. Then, its description and price are displayed on the screen too. Also, it asks for the quantity of the particular item code entered. It displays its subtotal thereafter. Moreover, it keeps on accepting item codes until ‘0’ is pressed. Consequently, it displays the total amount due. Then, it asks the user to tender the amount of cash of the customer. It displays the change, its breakdown and change in words and sends the details of the transaction such as total amount due, amount tendered and change due to customer.txt

Constraints:

• The program should inform the user is “items.txt” does not exist.

• The program should tell the user that the code is not found in “items.txt”.




1
Expert's answer
2022-01-24T18:39:49-0500
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<dos.h>
#include <bits/stdc++.h> 
#include<vector>
using namespace std;


string S;
vector<string> k;
#define MAX_ITEMS	3
vector<string> splitString(string str, string delimiter)
{
	vector <string> result;
    size_t pos=0;
	string token;


	while((pos = str.find(delimiter))!= std::string::npos)	
	{
		token = str.substr(0,pos);
		result.push_back(token);
		str.erase(0,pos+delimiter.length());
	}
		
	if(!str.empty()) result.push_back(str);
	return(result);
}




int main()
{
	char x;
	int n=0,Code[MAX_ITEMS],i,Flag=1,ItemCode,ICFlag=0,ItemNo=0,BillFlag=1;
	string Name1 = "H:\\Items.txt";
	string Items[MAX_ITEMS],z,str;
	float Price[MAX_ITEMS],Qty[MAX_ITEMS],Amount[MAX_ITEMS],Total=0;
	
	std::ifstream infile(Name1.c_str());
    if (!infile) 
	{
	    cout << "Unable to open file: "<<Name1;
        exit(1); // terminate with error
    }
	else
	{
	    cout<<"\n\nInput File Contents:\n";
    	while (std::getline(infile, str))
	    {
			z = "";
	    	if(str.size() > 0)            z = z + str;
			k = splitString(&z[0]," ");
			for(i=0;i<k.size();i++)
			{
//				cout<<"\t"<<k[i];
				if(i==0) 	Code[n] 	= atoi(k[i].c_str());
				if(i==1) 	Items[n] 	= k[i];
				if(i==2)	Price[n] 	= atof(k[i].c_str());
			}
//			cout<<"\n";
			n++;
		}
    	infile.close();	
	}
	cout<<"\n\tItems code, Name and Price:\n";
	for(n=0;n<MAX_ITEMS;n++)
	{
		cout<<setw(20)<<Code[n]<<"\t"<<Items[n]<<"\t"<<Price[n]<<endl;
	}
	while(Flag)
	{
		for(n=0;n<MAX_ITEMS;n++) {Qty[n]=0; Amount[n]=0;}
		Total=0;
		ICFlag=0;
		while(ICFlag==0 && BillFlag==1)
		{
			cout<<"\n\tEnter the Item Code: "; cin>>ItemCode;
			for(n=0;n<MAX_ITEMS;n++) 
			{
				if(ItemCode==Code[n]) 
				{
					ICFlag=1;				
					ItemNo=n;
				}
			}
			if(ICFlag==1) 
			{
				cout<<"\n\tEnter Qty. Code: "; cin>>Qty[ItemNo];
				Amount[ItemNo] = Price[ItemNo]*Qty[ItemNo];
				Total = Total + Amount[ItemNo];
			}
			ICFlag=0;
			cout<<"\n\tPress 1 to continue or 0 to Bill:"; cin>>BillFlag;
		}
	
		for(n=0;n<MAX_ITEMS;n++)
		{
			cout<<"\n\tItem Code: "<<Code[n]<<"\tItem: "<<Items[n]<<"\tQty.: "<<Qty[n]<<"\tAmount: "<<Amount[n];
		}
		cout<<"\n\tTotal Amount = "<<Total;
		cout<<"\n\tPress 1 to continue or 0 to quit:"; cin>>Flag;
	}
	return(0);	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment