Need help with small beginner program, please.

Reply

Join Date: Oct 2004
Posts: 2
Reputation: tionik06 is an unknown quantity at this point 
Solved Threads: 0
tionik06 tionik06 is offline Offline
Newbie Poster

Need help with small beginner program, please.

 
0
  #1
Oct 30th, 2004
: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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help with small beginner program, please.

 
0
  #2
Oct 30th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: tionik06 is an unknown quantity at this point 
Solved Threads: 0
tionik06 tionik06 is offline Offline
Newbie Poster

Re: Need help with small beginner program, please.

 
0
  #3
Oct 31st, 2004
thanks, what does fmod look like? do i just type "fmod" or does it have its own symbol?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Need help with small beginner program, please.

 
1
  #4
Oct 31st, 2004
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>
May 'the Google' be with you!
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