#include <QCoreApplication>
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
double change_due_now( double payment, double item_cost)
{
return payment - item_cost;
}
double convert_cents(double cents)
{
return cents*100;
}
int find_change(int cents_no_decimal, int quarters, int &dimes, int &nickels, int & pennies, int & checksum_1)
{
//rounding error cmopensation
int r_remainder = 0;
double pennies_1=0;
quarters = cents_no_decimal / 25;
r_remainder=cents_no_decimal % 25;
cout<<"quarters :"<<quarters<<endl;
//cout<<"remainder :"<<r_remainder<<endl;
dimes = r_remainder/10;
cout<<"dimes :"<<dimes<<endl;
r_remainder = r_remainder % 10;
//cout<<dimes<<endl;//" "<<r_remainder;
nickels = r_remainder/5;
cout<<"nickles :"<<nickels<<endl;
pennies = (r_remainder % 5);
cout<<"pennies :"<<pennies<<endl;
cout<<"---------"<<endl;
checksum_1 = (quarters*25)+ (dimes*10) + (nickels * 5) + (pennies * 1) ;
cout<<"sum change = "<<checksum_1<<endl;
return 0;
}
int main()
{
double total_change_due; double item_cost = 0; double payment = 0;
double dollar_amount = 0; double cents = 0; double quarters = 0;
int dimes = 0; int nickels = 0; int pennies = 0; double cents_no_decimal = 0;
int a = 0; int checksum_1 = 0;
bool flag = true; char j = 0;
while (flag==true) {
cout<<"item cost:";
cin>>item_cost;
cout<<"payment :";
cin>>payment;
cout<<fixed<<setprecision(2)<<showpoint;
total_change_due= change_due_now(payment, item_cost);
dollar_amount = int(total_change_due); double difference = 0;
cents = total_change_due-int(total_change_due);
cout<<"Change due: $"<<total_change_due<<endl;
cout<<"dollars : $"<<dollar_amount<<endl;
cout<<"cents : $"<<cents<<endl;
cents_no_decimal = convert_cents(cents);
cout<<cents_no_decimal<<" converted to no decimal"<<endl;
find_change(cents_no_decimal, quarters, dimes, nickels, pennies, checksum_1);
//cout<<cents_no_decimal<<endl;
//executive function
double diff_1 = 0;
diff_1 = cents_no_decimal - checksum_1; //find the offset amt which is the money before the function then after
cout<<"diff_1: "<<diff_1<<endl;
if (diff_1 == 0) {cout<<"no adjustment necessary"<<endl;
cout<<"difference :"<<difference <<endl;}
else if(diff_1!=0) {cout<<"adjustment necessary"<<endl;
pennies = pennies +1;
cout<<"Pennies adjusted total: "<<pennies;}
cout<<endl<<endl;
cout<<"continue y/n : ";
cin>>j;
if (j!='y')
{
cout<<"terminating program";
flag = false;
}
}
return 0;
}
Bookmarks