I've have a project due this week and I'm having trouble with a specific section of my code. The exercise is titled "MakeChange" and I am to design it so that it makes the least amount of change for a dollar value entered. I'm pretty sure I have 90% of the code done correctly but I can't figure out how to have the change done correctly. Basically after it takes out the whole dollar amounts, I can't figure out how to have the following lines of code remove the previous amount.
// Make Change Exercise
#include <iostream>
usingnamespace std;
// Step 1: Prompt user to enter dollar value
// Step 2: Assign dollar value variables
// Step 3: Return change in full dollars
// Step 4: Return change in half dollars
// Step 5: Return change in quarters
// Step 6: Return change in dimes
// Step 7: Return change in nickels
// Step 8: Return change in cents
// End of Algorithm
int main ()
{
int change;
cout << "How much change would you back? ""Enter dollar value: ";
cin >> change;
int dollar = change / 1;
int halfdollar = dollar / .50;
int quarters = halfdollar / .25;
int dimes = quarters / .10;
int nickels = dimes / .05;
int cents = nickels / .01;
//Step 2: Return change in full dollars
cout << "The # of dollars= " << dollar << endl;
//Step 3: Return change in half dollars
cout << "The # of half dollars= " << halfdollar << endl;
//Step 4: Return change in quarters
cout << "The # of quarters= " << quarters << endl;
//Step 5: Return change in dimes
cout << "The # of dimes= " << dimes << endl;
//Step 6: Return change in nickels
cout << "The # of nickels= " << nickels << endl;
//Step 7: Return change in cents
cout << "The # of cents= " << cents << endl;
system("pause");
return 0;
}
The number of half dollars to output is being calculated wrongly.
Here, for example, you are calculating the number of half-dollars in the amount of dollars you have given back as change. You should be calculating the number of half-dollars in the change yet to be given back. int halfdollar = dollar / .50;
The correct calculation is the amount of change minus the number of dollars already calculated, divided by 0.5 and rounded down to an integer.
Similarly for all the others.
I suggest that after each calculation, you recalculate change, subtracting the amount of change you've already dealt with.
Something like:
1 2
int dollar = change / 1;
change = change - dollar;
Secondly on line 3 I have it listed as a "double" but then as an "int" throught the rest of the code. When I compile it I get a "[Warning:] converting to 'int' from 'double'". Is this just a warning or can I not do this?