944,085 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5740
  • C++ RSS
Apr 22nd, 2007
0

Assistance with change program

Expand 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

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Slavrix is offline Offline
67 posts
since Oct 2005
Apr 22nd, 2007
0

Re: Assistance with change program

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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 22nd, 2007
0

Re: Assistance with change program

so something like

C++ Syntax (Toggle Plain Text)
  1. int temp
  2.  
  3. temp = money * 100

?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Slavrix is offline Offline
67 posts
since Oct 2005
Apr 22nd, 2007
0

Re: Assistance with change program

Click to Expand / Collapse  Quote originally posted by Slavrix ...
so something like

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 22nd, 2007
0

Re: Assistance with change program

You should have a function called change so that you can intiate it once and call it whenever you need it!


Click to Expand / Collapse  Quote originally posted by Slavrix ...
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

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
~Ken Esquire~ is offline Offline
3 posts
since Oct 2006
Apr 22nd, 2007
-1

Re: Assistance with change program

Click to Expand / Collapse  Quote originally posted by Slavrix ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how do i saw every lines in my database w/c i have entered.?
Next Thread in C++ Forum Timeline: Reading a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC