I just finished my change calculator (Calculates change based on cost of purchase and cash given). I was testing it to see if it works and found one bug. Whenever the amount of pennies to be given is exactly one, it says it is 0. I went through the code and did the math manually off the code I wrote (without running it) and it looks like it should work. If you want to test this yourself just put in 0 as the cost of the purchase and 36.91 as the cash given. (This means the change is 36.91 and it should be 1 twenty, 1 ten, 1 five, 1 half dollar, 1 quarter, 1 dime, 1 nickel, and 1 penny [but it says 0 pennies].
Due note: I am very new to C++ and know VERY little
Here is the code:
/*This program simulates a situation at the register where someone pays with cash
and the cashier needs to know how much change and of which coins he or she needs to give.
Written by Jay Schauer
*/
//Data Declarations
#include <iostream>
#include <cstdint>
int main()
{
using namespace std;
double cost; //Stores cost of purchase
cout << "Input cost of purchase (in USD)" << endl;
cin >> cost;
double cash; //Stores cash that pays for purchase
cout << "Input cash given (in USD)" << endl;
cin >> cash;
double changet = cash - cost; //Stores the change,
the t is for temporaary because I use a different change variable later
cout << "Change to be given is " << changet << " in..." << endl;
//Also, I thought this was pretty clever since doubles apparantly can't be used
//with modulus (it gave me an error),
//so I figured I just multiply it by 100 and multiply all the divisions by 100 also
changet *= 100;
int change = changet; //Converts changet to an integer to be used with the modulus
int coins; //Stores the amount of "coins" to be given as change
coins = change / 2000;
cout << coins << " twenty dollar bills" << endl;
change = change % 2000;
coins = change / 1000;
cout << coins << " ten dollar bills" << endl;
change = change % 1000;
coins = change / 500;
cout << coins << " five dollar bills" << endl;
change = change % 500;
coins = change / 100;
cout << coins << " one dollar bills" << endl;
change = change % 100;
coins = change / 50;
cout << coins << " half dollars" << endl;
change = change % 50;
coins = change / 25;
cout << coins << " quarters" << endl;
change = change % 25;
coins = change / 10;
cout << coins << " dimes" << endl;
change = change % 10;
coins = change / 5;
cout << coins << " nickels" << endl;
change = change % 5;
//There is one problem that I can't figure out
//If the number of pennies to be given for change is exactly 1,
//it says 0 for the number of pennies to be given as change
coins = change / 1;
cout << coins << " pennies" << endl;
system("pause");
return 0;
}