Thursday, July 30, 2009

Programming help.. C++ Change making?

#include %26lt;iostream%26gt;


using namespace std;


int main()


{


int dollars,quarters,dimes,nickels,pennies,a...


cout %26lt;%26lt; "Please enter amount_due:";


cin %26gt;%26gt; amount_due;


amount_due_value = amount_due*100;


cout %26lt;%26lt; "Please enter the amount of money you intend to use:\n"


%26lt;%26lt; "Please enter the number of dollars:";


cin %26gt;%26gt; dollars;


dollar_value = dollars*100;


cout %26lt;%26lt; "Please enter the number of quarters";


cin %26gt;%26gt; quarters;


quarter_value = quarters*25;


cout %26lt;%26lt; "Please enter the number of dimes";


cin %26gt;%26gt; dimes;


dime_value = dimes*10;


cout %26lt;%26lt; "Please enter the number of nickels";


cin %26gt;%26gt; nickels;


nickel_value = nickels*5;


cout %26lt;%26lt; "Please enter the number of pennies";


cin %26gt;%26gt; pennies;


penny_value = pennies*1;


dollars_due = amount_due_value - dollar_value;


quarters_due = (amount_due_value-dollars_due)/quarter_v...

Programming help.. C++ Change making?
What's the problem? (You want us to compile this and debug it for you?)





***Update***


OK - I'm guessing at what it's supposed to do. I suspect you'll enter an amount due and an amount paid, like 33.58 (due) and 50.00 paid, and the program should spit out something like:


Change due: $16.42 paid as


1 Ten dollar bill,


1 Five dollar bill,


1 One dollar bill,


1 Quarter,


1 Dime,


1 Nickle,


2 pennies.





If this is correct, then you can do it using the following logic:





1) Get the amount due and amount paid (You alreadu have this as amount_due and dollars)


2) These should both be float values as you'll want to know fraction values.





3) Once you get the input, you need to validate it. Make sure the amount due is %26gt; 0.01 and that the amount paid is %26gt; amount due.





4) Create two arrays to hold your "change drawer" items. This would be something like:


char changeDesc[9][30] = {


"One Hundred Dollar Bill","Fifty Dollar Bill","Twenty Dollar Bill", %26lt;fill in the rest%26gt; %26lt;"Dime","Nickle","Pennies"};


float changeAmt[9]={10000,5000,2000, ... 10, 5, 1};


//Note: above values are 100* actual value to remove decimal.





5) Calculate the amount due: changeDue=(int)(dollars-amount_due*100);





7) You'll then have a loop which will compute a reducing balance based on the number of change items given.


Each time through, you'll compare the changeDue value to that in the changeAmt[position]. If it's smaller than the changeAmt[position] value, add one to position. If not, then print out 1 changeDesc[position] value andsubtract changeAmt[position] from the change_due.





Now the above will print multiple lines for each time. For example, if you should get three one dollar bills, it'll print One Dollar Bill three times. You can add code to total each item up and print the total, but get the first part working before you worry about that. Post a new question with your changed source once you're ready for review.





(Or - email me at mdr1119 @ sbcglobal.net and I'll respond.)
Reply:How come you are having the user enter the amount in each denomination?





The best approach would be to input the amount due first. Something like $5.25 instead of 5 dollars, 2 dimes and a nickel (which is what I am getting out of your program).





Once you have this total amount paid subtract that from the total amount due.





From there you will have change due. And then you can calculate the amount of change due as you currently are.





The only problem I see is that you never have a total of money used.
Reply:This is what i think ...





U have used integer variables in ur program right,


%26amp; u say u r getting modulo Errors;


so maybe if u r providing float values as input during the runtime it may be getting parsed into integers.


No comments:

Post a Comment