In this program everything work except the change back, the quarters, dimes, nickels, and pennies wont work, everytihng else works, for my input i used 65.67, which with hst came out to 75.52, the amount tendered was 100. I just need help with getting the quarters, dimes, nickels, and pennies to work.
#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
// Associating program identifiers with external file names
#define in_file "data.txt"//input file name
#define out_file "result.txt"//output file name
using namespace std;
void purchase(float&);
float Change(float);
void reportchange(float);
ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
float total=0.0;
float leftover;
float tendered;
float change;
int main()
{
ins.open(in_file);// associates ins as an input stream
outs.open(out_file);// associates outs as an output stream
purchase(total);
Change(total);
reportchange(change);
ins.close();// closing input file
outs.close();// closing output file
return 0;
}
void purchase(float& total)
{
float amount=0.0;
ins>>amount;
while(amount>0)
{
total+=amount;
ins>>amount;
}
if(amount==0)
ins>>tendered;
total=total+(total*0.15);
}
float Change(float total)
{
outs<<setprecision(4)<<endl;
outs<<"Your total is: " << total<<endl;
outs<<"You payed: "<< tendered <<endl;
change=tendered-total;
outs<<"Your total change is: " <<change<<endl;
return total;
}
void reportchange(float change)
{
int leftover=change;
int hundreds, fifties, twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies;
quarters=0.0;
dimes=0.0;
nickels=0.0;
int q=0.25;
int d=0.10;
int n=0.05;
// Calculate the number of quarters, dimes, and nickels in change
hundreds=leftover/100;
leftover=leftover%100;
fifties=leftover/50;
leftover=leftover%50;
twenties=leftover/20;
leftover=leftover%20;
tens=leftover/10;
leftover=leftover%10;
fives=leftover/5;
leftover=leftover%5;
toonies=leftover/2;
leftover=leftover%2;
loonies=leftover/1;
leftover=leftover%1;
if(q>0)
{
quarters=leftover/q;
leftover=leftover%q;
}
if(d>0)
{
dimes=leftover/d;
leftover=leftover%d;
}
if(n>0)
{
nickels=leftover/n;
leftover=leftover%n;
}
pennies=leftover/(0.01);
leftover=(100-leftover); //set change again otherwise it put garbage in output.
outs<<setprecision(4)<<endl;
outs<<"That is: "<<endl;
if (hundreds < 1);
else if (hundreds == 1)
outs << "\t" << hundreds << " Hundred" << "\n";
else
outs << "\t" << hundreds << " Hundreds" << "\n";
if (fifties < 1);
else if (fifties == 1)
outs << "\t" << fifties << " Fifty" << "\n";
else
outs << "\t" << fifties << " Fifties" << "\n";
if (twenties < 1);
else if (twenties == 1)
outs << "\t" << twenties << " Twenty" << "\n";
else
outs << "\t" << twenties << " Twenties" << "\n";
if (tens < 1);
else if (tens == 1)
outs << "\t" << tens << " Ten" << "\n";
else
outs << "\t" << tens << " Tens" << "\n";
if (fives < 1);
else if (fives == 1)
outs << "\t" << fives << " Five" << "\n";
else
outs << "\t" << fives << " Fives" << "\n";
if (toonies < 1);
else if (toonies == 1)
outs << "\t" << toonies << " Toonie" << "\n";
else
outs << "\t" << toonies << " Toonies" << "\n";
if (loonies < 1);
else if (loonies == 1)
outs << "\t" << loonies << " Loonie" << "\n";
else
outs << "\t" << loonies << " Loonies" << "\n";
if (quarters < 1);
else if (quarters == 1)
outs << "\t" << quarters << " Quarter" << "\n";
else
outs << "\t" << quarters << " Quarters" << "\n";
if (dimes < 1);
else if (dimes == 1)
outs << "\t" << dimes << " Dime" << "\n";
else
outs << "\t" << dimes << " Dimes" << "\n";
if (nickels < 1);
else if (nickels == 1)
outs <<"\t" << nickels << " Nickel" << "\n";
else
outs << "\t" << nickels << " Nickels"<< "\n";
if (pennies <= 0);
else if (pennies < 2)
outs << "\t" << pennies << " Penny" << "\n";
else
outs << "\t" << pennies << " Pennies" << "\n";
}
Thanks.