Assistance with change program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 62
Reputation: Slavrix is an unknown quantity at this point 
Solved Threads: 0
Slavrix Slavrix is offline Offline
Junior Poster in Training

Assistance with change program

 
0
  #1
Apr 22nd, 2007
i have this simple program im writing to help my dads business.

thoguh it doesnt seem to be working, this is what i have currently

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int change(double money,int onedollar,int fiftycent,int twentycent,int tencent,int fivecent);
  5.  
  6. int main()
  7. {
  8. int money, onedollar, fiftycent, twentycent, tencent, fivecent;
  9.  
  10. cout << "Enter amount: ";
  11. cin >> money;
  12.  
  13. change(money, onedollar, fiftycent, twentycent, tencent, fivecent);
  14.  
  15. cout << "One Dollar: " << onedollar << endl;
  16. cout << "Fifty Cent: " << fiftycent << endl;
  17. cout << "Twenty Cent: " << twentycent << endl;
  18. cout << "Ten Cent: " << tencent << endl;
  19. cout << "Five Cent: " << fivecent << endl;
  20. return 0;
  21.  
  22. }
  23.  
  24. int change(float money, int onedollar, int fiftycent, int twentycent, int tencent, int fivecent)
  25.  
  26. {
  27. float temp;
  28.  
  29. temp = money;
  30.  
  31. temp *= 100;
  32. onedollar = temp/100;
  33. temp = temp%100;
  34. fiftycent = temp/50;
  35. temp = temp%50;
  36. twentycent = temp/20;
  37. temp = temp%20;
  38. tencent = temp/10;
  39. temp = temp%10;
  40. fivecent = temp/5;
  41.  
  42. return 0;
  43.  
  44. }


i dont know if ive got the right idea or anything

this is sorta what im trying to write, a C++ function, named change(), that has a floating-point parameter and 5 integer reference parameters named onedollars, fiftycents, twentycents, tencents and fivecents. The function is to consider the floating-point passed value as a dollar amount and convert the value into an equivalent number of dollar coins, fifty, twenty, ten and five cents coins. Using the references, the function should directly alter the respective arguments in the calling function.

For example, calling the function with the following arguments: change(78.85, onedollars, fiftycents, twentycents, tencents, fivecents); will assign the following values to the appropriate variables:
onedollars = 78
fiftycents = 1
twentycents = 1
tencents = 1
fivecents = 1


any help wuld b awsome
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assistance with change program

 
0
  #2
Apr 22nd, 2007
what you need is integer division. in your program, temp is a float. float(123)/100 will give you 1.23, not 1. and a standards-compliant compiler would give you a compile-time error if you try float(123)%100
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 62
Reputation: Slavrix is an unknown quantity at this point 
Solved Threads: 0
Slavrix Slavrix is offline Offline
Junior Poster in Training

Re: Assistance with change program

 
0
  #3
Apr 22nd, 2007
so something like

  1. int temp
  2.  
  3. temp = money * 100

?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Assistance with change program

 
0
  #4
Apr 22nd, 2007
Originally Posted by Slavrix View Post
so something like

  1. int temp
  2.  
  3. temp = money * 100
?
yes. and if you want the function to have output parameters, pass them by reference, not value.
also passing the amount in cents (as an integer) rather than a float would help avoid floating point round off errors.
Last edited by vijayan121; Apr 22nd, 2007 at 1:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: ~Ken Esquire~ is an unknown quantity at this point 
Solved Threads: 0
~Ken Esquire~ ~Ken Esquire~ is offline Offline
Newbie Poster

Re: Assistance with change program

 
0
  #5
Apr 22nd, 2007
You should have a function called change so that you can intiate it once and call it whenever you need it!


Originally Posted by Slavrix View Post
i have this simple program im writing to help my dads business.

thoguh it doesnt seem to be working, this is what i have currently

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int change(double money,int onedollar,int fiftycent,int twentycent,int tencent,int fivecent);
  5.  
  6. int main()
  7. {
  8. int money, onedollar, fiftycent, twentycent, tencent, fivecent;
  9.  
  10. cout << "Enter amount: ";
  11. cin >> money;
  12.  
  13. change(money, onedollar, fiftycent, twentycent, tencent, fivecent);
  14.  
  15. cout << "One Dollar: " << onedollar << endl;
  16. cout << "Fifty Cent: " << fiftycent << endl;
  17. cout << "Twenty Cent: " << twentycent << endl;
  18. cout << "Ten Cent: " << tencent << endl;
  19. cout << "Five Cent: " << fivecent << endl;
  20. return 0;
  21.  
  22. }
  23.  
  24. int change(float money, int onedollar, int fiftycent, int twentycent, int tencent, int fivecent)
  25.  
  26. {
  27. float temp;
  28.  
  29. temp = money;
  30.  
  31. temp *= 100;
  32. onedollar = temp/100;
  33. temp = temp%100;
  34. fiftycent = temp/50;
  35. temp = temp%50;
  36. twentycent = temp/20;
  37. temp = temp%20;
  38. tencent = temp/10;
  39. temp = temp%10;
  40. fivecent = temp/5;
  41.  
  42. return 0;
  43.  
  44. }


i dont know if ive got the right idea or anything

this is sorta what im trying to write, a C++ function, named change(), that has a floating-point parameter and 5 integer reference parameters named onedollars, fiftycents, twentycents, tencents and fivecents. The function is to consider the floating-point passed value as a dollar amount and convert the value into an equivalent number of dollar coins, fifty, twenty, ten and five cents coins. Using the references, the function should directly alter the respective arguments in the calling function.

For example, calling the function with the following arguments: change(78.85, onedollars, fiftycents, twentycents, tencents, fivecents); will assign the following values to the appropriate variables:
onedollars = 78
fiftycents = 1
twentycents = 1
tencents = 1
fivecents = 1


any help wuld b awsome
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Assistance with change program

 
0
  #6
Apr 22nd, 2007
Originally Posted by Slavrix View Post
i have this simple program im writing to help my dads business.
Yeah, right. Look, we don't mind helping people with their homework assignments. So you don't have to lie about it in a lame attempt to get homework help.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC