fork download
  1. //Sam Partovi CS1A Chapter 4, P. 221, #8
  2. //
  3. /*******************************************************************************
  4. *
  5. * SIMULATE COIN COUNTING GAME
  6. * ____________________________________________________________
  7. * This program simulates a coin counting game where the defined quantities of
  8. * coins including pennies, nickels, dimes, and quarters must equal a specific
  9. * dollar amount.
  10. *
  11. * The simulation is based on the following analysis:
  12. * If the value of each coin multiplied by the quantity of each coin is equal to
  13. * the specified dollar amount, the player wins the game.
  14. * ____________________________________________________________
  15. *INPUT
  16. * pennyQuantity : Player's quantity of pennies
  17. * nickelQuantity : Player's quantity of nickels
  18. * dimeQuantity : Player's quantity of dimes
  19. * quarterQuantity : Player's quantity of quarters
  20. * pennyValue : Monetary value held by one penny
  21. * nickelValue : Monetary value held by one nickel
  22. * dimeValue : Monetary value held by one dime
  23. * quarterValue : Monetary value held by one quarter
  24. * dollarGoal : Goal dollar amount to be counted
  25. *
  26. *OUTPUT
  27. * coinTotal : Total value of all coins entered
  28. ******************************************************************************/
  29. #include <iostream>
  30. #include <iomanip>
  31. using namespace std;
  32. int main ()
  33. {
  34. int pennyQuantity; //INPUT - Player's quantity of pennies
  35. int nickelQuantity; //INPUT - Player's quantity of nickels
  36. int dimeQuantity; //INPUT - Player's quantity of dimes
  37. int quarterQuantity; //INPUT - Player's quantity of quarters
  38. float dollarGoal; //INPUT - Goal dollar amount to be counted
  39. float coinTotal; //OUTPUT - Total value of all coins entered
  40.  
  41. //Initialize program variables
  42. const float pennyValue = 0.01; //INPUT - Monetary value held by one penny
  43. const float nickelValue = 0.05; //INPUT - Monetary value held by one nickel
  44. const float dimeValue = 0.10; //INPUT - Monetary value held by one dime
  45. const float quarterValue = 0.25; //INPUT - Monetary value held by one quarter
  46. dollarGoal = 1.00;
  47.  
  48. //Format output to 2 decimal places
  49. cout << fixed << setprecision(2);
  50.  
  51. //Prompt player for coin quantities
  52. cout << "Coin counting game\n";
  53. cout << "___________________\n";
  54. cout << "Your goal is to count enough coins to make $" << dollarGoal << ".\n";
  55. cout << "Enter number of pennies: ";
  56. cin >> pennyQuantity;
  57. cout << "\nEnter number of nickels: ";
  58. cin >> nickelQuantity;
  59. cout << "\nEnter number of dimes: ";
  60. cin >> dimeQuantity;
  61. cout << "\nEnter number of quarters: ";
  62. cin >> quarterQuantity;
  63.  
  64. //Perform coin total value calculation
  65. coinTotal = (pennyQuantity * pennyValue) + (nickelQuantity * nickelValue)
  66. + (dimeQuantity * dimeValue) + (quarterQuantity * quarterValue);
  67.  
  68. //Determine if player has won or lost the game
  69. if (coinTotal == dollarGoal) {
  70. cout << "\nCongratulations! You counted out $" << coinTotal << ".";
  71. }
  72. else {
  73. cout << "\nTry again! You entered $" << coinTotal << ".\n";
  74. }
  75.  
  76. return 0;
  77. }
  78.  
  79.  
Success #stdin #stdout 0s 5272KB
stdin
10
0
4
2
stdout
Coin counting game
___________________
Your goal is to count enough coins to make $1.00.
Enter number of pennies: 
Enter number of nickels: 
Enter number of dimes: 
Enter number of quarters: 
Congratulations! You counted out $1.00.