I am writing a program that takes the cost of an item, and the amount payed, and calculates how much of each coin you should get back in change (quarter, dime, nickel, penny). In the function that is calculating how many quarters are needed back, it always returns 2
float calculateCoins(float change)
{
int x=1;
float result=1;
while (result>0)
{
result =fmod(change, (.25 * x));
x++;
}
return x;
I am not sure what is wrong.
Also, excuse my inefficient code and probably ugly code, I am still learning
#include <iostream>
#include <math.h>
using namespace std;
float calculateChange(float, float);
float calculateCoins(float);
int main()
{
float amountPay, amountDue, changeDue, quarter;
cout << "This program calculates how much change should be returned "
<< "\nwhen a payment is made" << endl << endl;
cout << "Please input the cost of the item:" << endl;
cin >> amountDue;
cout << endl << "Please input the amount paid:" << endl;
cin >> amountPay;
changeDue = calculateChange(amountDue,amountPay);
quarter = calculateCoins(changeDue);
cout << changeDue << endl;
cout << quarter << " quarters needed";
return 0;
}
float calculateChange(float amount, float payment)
{
return payment-amount;
}
float calculateCoins(float change)
{
int x=1;
float result=1;
while (result>0)
{
result =fmod(change, (.25 * x));
x++;
}
return x;
}