Answer to Question #295530 in C++ for Imman

Question #295530

Sample Run:

      Welcome to CS127 Mart!

Item Codes Description      Price

100   Sweet 'n Ripe Grapes 125.35

101     Crunchy Apples  52.20

109     Green Peas      25.75

Enter Code: 100 

  Sweet 'n Ripe Grapes 125.35 Qty: 2

  Subtotal: 250.70

Enter Code: 101

  Crunchy Apples 52.20 Qty: 1

  Subtotal: 52.20

Total Due: 302.90

Cash:   1000.00

Change:  697.10

Another Transaction? <Y/N> N

Your change is Six hundred ninety-seven and ten centavos.



1
Expert's answer
2022-02-09T05:55:18-0500
#include<iostream>
#include<string>

using namespace std;

void Menu()
{
	cout<<"\tWelcome to CS127 Mart!\n"
		<<"100 Sweet`n Ripe Grapes 125.35\n"
		<<"101 Crunchy Apples 52.20\n"
		<<"109 Grean Peas 25.75\n"
		<<"0   Exit program\n";
}

int main()
{
	
	int code, Qty;
	double total=0;
	string units[]={"one","two","three","four","five","six","seven","eight",
				"nine","ten","eleven","twelve","thirteen","fourteen",
				"fifteen","sixteen","seventeen","eighteen","nineteen"};
	string tens[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
	char select;
	Menu();
	do
	{
		do
		{
			cout<<"\nEnter Code: ";
			cin>>code;
			if(code==0)break;
			if(code==100)
			{
				cout<<"Sweet`n Ripe Grapes 125.35 Qty: ";
				cin>>Qty;
				cout<<"Subtotal: "<<Qty*125.35;
				total+=Qty*125.35;
			}
			else if(code==101)
			{
				cout<<"Crunchy Apples 52.20 Qty: ";
				cin>>Qty;
				cout<<"Subtotal: "<<Qty*52.20;
				total+=Qty*52.20;
			}
			else if(code==109)
			{
				cout<<"Grean Peas 25.75 Qty: ";
				cin>>Qty;
				cout<<"Subtotal: "<<Qty*25.75;
				total+=Qty*25.75;
			}
		}while(true);
		cout<<"\nTotal Due: "<<total;
		double cash;
		cout<<"\nCash: ";
		cin>>cash;
		double change=cash-total;
		cout<<"\nChange: "<<change<<endl;
		string str=to_string(change);
		string strnum=str.substr(0,str.find('.'));
		

		int num;
		cout<<"Your change is ";
		if(strnum.size()==1)
		{
			num=strnum[0]-'0';
			cout<<units[num-1];
		}
		else if(strnum.size()==2)
		{
			num=strnum[0]-'0';
			if(num<2)	
				cout<<units[stoi(strnum)-1];
			else
			{
				cout<<tens[num-2];
				num=strnum[1]-'0';
				if(num>0)
					cout<<"-"<<units[num-1];
			}
		}
		else if(strnum.size()==3)
		{
			num=strnum[0]-'0';
			cout<<units[num-1]<<" hundred ";
			num=strnum[1]-'0';
			if(num<2)	
				cout<<units[stoi(strnum)-1];
			else
			{
				cout<<tens[num-2];
				num=strnum[2]-'0';
				if(num>0)
					cout<<"-"<<units[num-1];
			}
		}
		else if(strnum.size()==4)
		{
			num=strnum[0]-'0';
			cout<<units[num-1]<<" thousand ";
			num=strnum[1]-'0';
			cout<<units[num-1]<<" hundred ";
			num=strnum[2]-'0';
			if(num<2)	
				cout<<units[stoi(strnum)-1];
			else
			{
				cout<<tens[num-2];
				num=strnum[3]-'0';
				if(num>0)
					cout<<"-"<<units[num-1];
			}
		}
		string afterPoint=str.substr(str.find('.'),str.size());
		afterPoint=afterPoint.substr(1,2);
		if(stoi(afterPoint)>0)
		{
			num=afterPoint[0]-'0';
			cout<<" and ";
			if(num<2)	
				cout<<units[stoi(afterPoint)-1];
			else
			{
				cout<<tens[num-2];
				num=afterPoint[1]-'0';
				if(num>0)
					cout<<"-"<<units[num-1];
			}
		}
		
		cout<<"\nAnother transaction? <Y/N> ";
		cin>>select;
	}while(select=='Y');
	
}

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

LATEST TUTORIALS
New on Blog