943,985 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2802
  • C RSS
Oct 30th, 2004
0

Need help with small beginner program, please.

Expand Post »
:rolleyes:
This is a program where the user enters how much they owe, then they enter how much they are gonna pay, like (30 dollars for a $25.63 amount), and their change is calculated back to them in ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.

My instructor hinted us to use the mod operator but i found that '%' does not work with doubles or floats. So i am left with no clue on how to program this thing and i dont want to get an F, because so far ive been gettings A's. Its just this darn mod operator is so stubborn. thanks for any help i can get.

here is the code i have.



  1. #include <stdio.h> /* includes stdio.h i/o */
  2.  
  3. void pause(void); /* declares function */
  4.  
  5. int main(void)
  6. {
  7. double due, given, bal = 0; /* sets double float variables */
  8. double tens, fives, ones, quarters, dimes, nickels, pennies = 0;
  9. printf("\n\n\tHow much do you owe?\n\n\t\tAmount : ");
  10. /* asks for debt */
  11. scanf("%lf", &due); /* accepts user's input */
  12. printf("\n\n\tHow much will you pay?\n\n\t\tAmount : ");
  13. /* asks user how much they will give to pay for the amount */
  14. scanf("%lf", &given); /* accepts user's input */
  15. bal = given - due; /* calculates the user's change */
  16. printf("\n\nAmount Due = $%.2lf", due); /* displays transaction info */
  17. printf("\n\nAmount Given = $%.2lf", given);
  18. printf("\n\nAmount Balance = $%.2lf", bal);
  19. printf("\n\n\tHere is your Change.");
  20. if((bal / 10) >= 1) /* if can be divided by bill, determines
  21.   how many bills */
  22. {
  23. tens = bal / 10;
  24. bal = bal % 10;
  25. }
  26. if((bal / 5) >= 1) /* if can be divided by bill, determines
  27.   how many bills */
  28. {
  29. fives = bal / 5;
  30. bal = bal % 5;
  31. }
  32. if((bal / 1) >= 1) /* if can be divided by bill, determines
  33.   how many bills */
  34. {
  35. ones = bal / 1;
  36. bal = bal % 1;
  37. }
  38. if((bal / .25) >= 1) /* if can be divided by coin, determines
  39.   how many coins */
  40. {
  41. quarters = bal / .25;
  42. bal = bal % .25;
  43. }
  44. if((bal / .10) >= 1) /* if can be divided by coin, determines
  45.   how many coins */
  46. {
  47. dimes = bal / .10;
  48. bal = bal % .10;
  49. }
  50. if((bal / .05) >= 1) /* if can be divided by coin, determines
  51.   how many coins */
  52. {
  53. nickels = bal / .05;
  54. bal = bal % .05;
  55. }
  56. if((bal / .01) >= 1) /* if can be divided by coin, determines
  57.   how many coins */
  58. {
  59. pennies = bal / .01;
  60. bal = bal % .01;
  61. }
  62. printf("\nTens = %d", tens); /* prints change resulsts */
  63. printf("\nFives = %d", fives);
  64. printf("\nOnes = %d", ones);
  65. printf("\nQuarters = %d", quarters);
  66. printf("\nDimes = %d", dimes);
  67. printf("\nNickels = %d", nickels);
  68. printf("\nPennies = %d", pennies);
  69. pause(); /* pauses */
  70. return (0);
  71. }
  72. void pause(void) /* tells what pause does */
  73. {
  74. char ch; /* sets character variable */
  75. printf("\n\nPress \'Enter\' to exit.");
  76. scanf("%c", &ch); /* waits for 'enter' key */
  77. scanf("%c", &ch);
  78. }
Last edited by alc6379; Oct 30th, 2004 at 7:12 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tionik06 is offline Offline
2 posts
since Oct 2004
Oct 30th, 2004
0

Re: Need help with small beginner program, please.

>Its just this darn mod operator is so stubborn.
When working with money, it's best to use integers because floating-point has accuracy issues. However, if you really want to use floating-point, the standard library supports the fmod function. fmod will perform modulus on two floating-point values.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 31st, 2004
0

Re: Need help with small beginner program, please.

thanks, what does fmod look like? do i just type "fmod" or does it have its own symbol?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tionik06 is offline Offline
2 posts
since Oct 2004
Oct 31st, 2004
1

Re: Need help with small beginner program, please.

double fmod(double x, double y) is a function, so you have to rewrite your code a little. It returns the remainder of the division x / y. Make sure to #include <math.h>
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: Problems of looping in saving to a text file
Next Thread in C Forum Timeline: binary trees





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


Follow us on Twitter


© 2011 DaniWeb® LLC