fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. unsigned change = time(0);
  11. int quarters = 0;
  12. int dime = 0;
  13. int nickel = 0;
  14. int penny = 0;
  15.  
  16. //Get a random number from 0-100
  17. srand(change);
  18. change = change % 100;
  19.  
  20.  
  21. // If change is greater than or equal to one run the program
  22. if (change != 0)
  23. {
  24.  
  25. // using fixed and set precision to eliminate need for decimal fixing.
  26. cout << fixed << setprecision(2);
  27.  
  28. cout << "Change Due:" << setw(4) << "$" << change / 100.0 << "\n";
  29. cout << "Coin Dispenser will dispense:\n";
  30.  
  31. // Calculate and adjust variables
  32. quarters = change / 25;
  33. change = change % 25;
  34.  
  35. dime = change / 10;
  36. change = change % 10;
  37.  
  38. nickel = change / 5;
  39. change = change % 5;
  40.  
  41. penny = change / 1;
  42.  
  43. if (quarters != 0)
  44. {
  45. cout << " Quarters: " << quarters << " (" << "$" << quarters * 25 / 100.0 << ")" << "\n";
  46. }
  47.  
  48. if (dime != 0)
  49. {
  50. cout << " Dimes:" << setw(5) << dime << " (" << "$" << dime * 10 / 100.0 << ")" << "\n";
  51. }
  52.  
  53.  
  54. if (nickel != 0)
  55. {
  56. cout << " Nickels:" << setw(3) << nickel << " (" << "$" << nickel * 5 / 100.0 << ")" << "\n";
  57. }
  58.  
  59.  
  60. if (penny != 0)
  61. {
  62. cout << " Pennies:" << setw(3) << penny << " (" << "$" << penny * 1 / 100.0 << ")" << "\n";
  63. }
  64. }
  65. // If change is = to 0, don't run the entire program, just run this portion.
  66. else
  67. { cout << "Change Due" << "$0.00" << "\n";
  68. cout << "Coin Dispenser will dispense:\n";
  69. cout << "No Coins\n";
  70. }
  71.  
  72. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Change Due:   $0.43
Coin Dispenser will dispense:
 Quarters: 1 ($0.25)
 Dimes:    1 ($0.10)
 Nickels:  1 ($0.05)
 Pennies:  3 ($0.03)