# Answer to Question #32078 in C++ for amal

Question #32078
Write a program that will prompt the user for a purchase amount. If this bill amount is valid, then prompt the user again for the payment amount. If this is valid then compute the change to be returned. This amount should be displayed in 1 fils, 5 fils, 10 fils, 20 fils, 50 fils, 100 fils, quarter dinar, half dinar, one dinar, five dinar, ten dinar and twenty dinar. If either of the input values are invalid, an error message should be displayed explaining precisely where the error is. Do NOT display bills or coins that will not be returned. Send your output to a file called &quot;Output.dat&quot;.
1
2013-06-21T10:19:26-0400
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

void subtract_banknotes(int denomination, double& dinars, ostream& out)
{
if (dinars >=
denomination)
{
int banknoteCount = (int)dinars / denomination;
dinars -= banknoteCount * denomination;
out << banknoteCount << " X " << denomination << " dinars" << endl;
}
}

void subtract_coins(double denomination, double& dinars, ostream& out)
{
if (dinars >= denomination)
{
int coinCount = dinars / denomination;
dinars -= coinCount * denomination;

if (denomination < 0.100)
out << coinCount << " X " << denomination * 1000 << " fils" << endl;
else if (denomination == 0.250)
out << coinCount << " X quarter dinar" << endl;
else if (denomination == 0.500)
out << coinCount << " X half dinar" << endl;
}
}

static const int banknoteDenominations = { 20, 10, 5, 1 };
static const double coinDenominations = { 0.500, 0.250, 0.100, 0.050, 0.020, 0.010, 0.005, 0.001 };

int main()
{
ofstream out("Output.dat", ios::out);

double purchaseAmount;
cout << "Enter purchase amount: ";
cin >> purchaseAmount;

if (purchaseAmount <= 0)
{
out << "Invalid purchase amount: " << purchaseAmount << endl;
out.close();
return -1;
}

double paymentAmount;
cout << "Enter payment amount: ";
cin >> paymentAmount;

if (paymentAmount < purchaseAmount)
{
out << "Invalid payment amount: " << paymentAmount << endl;
out.close();
return -2;
}

double change = paymentAmount - purchaseAmount;
out << "Change amount: " << change << endl << endl;

for (int b = 0; b < 4; b++)
subtract_banknotes(banknoteDenominations[b], change, out);
for (int c = 0; c < 8; c++)
subtract_coins(coinDenominations[c], change, out);

out.close();
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!