fork download
  1. //Sam Partovi CS1A Ch. 11, P.648, #13
  2. /*******************************************************************************
  3. * SIMULATE DRINK MACHINE
  4. * ____________________________________________________________
  5. * This program simulates a drink vending machine that accepts drink choice and
  6. * money given to the machine.
  7. * ____________________________________________________________
  8. *INPUT
  9. * name : Drink name
  10. * cost : Cost of drink
  11. * numInMachine : Drink quantity in stock
  12. * moneyInserted : Money given to machine
  13. *OUTPUT
  14. * Menus that allow for selection and purchase of drinks
  15. *******************************************************************************/
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <string>
  19.  
  20. using namespace std;
  21.  
  22. // Structure definition for Drink
  23. struct Drink {
  24. string name;
  25. double cost;
  26. int numInMachine;
  27. };
  28.  
  29. // Function to display the drink menu
  30. void displayMenu(Drink drinks[], int size) {
  31. cout << "Available Drinks:" << endl;
  32. for (int i = 0; i < size; i++) {
  33. cout << i + 1 << ". " << drinks[i].name << " - $" << fixed <<
  34. setprecision(2) << drinks[i].cost;
  35. if (drinks[i].numInMachine == 0)
  36. cout << " (Sold Out)";
  37. cout << endl;
  38. }
  39. cout << "99. Quit and Display Machine Earnings" << endl;
  40. }
  41.  
  42. // Main function
  43. int main() {
  44. // Array of 5 drinks
  45. Drink drinks[5] = {
  46. {"Cola", 0.75, 20},
  47. {"Root Beer", 0.75, 20},
  48. {"Lemon-Lime", 0.75, 20},
  49. {"Grape Soda", 0.80, 20},
  50. {"Cream Soda", 0.80, 20}
  51. };
  52.  
  53. int choice;
  54. double moneyInserted;
  55.  
  56. do {
  57. displayMenu(drinks, 5);
  58.  
  59. cout << "Select a drink (1-5) or type 99 to quit: ";
  60. cin >> choice;
  61.  
  62. if (choice >= 1 && choice <= 5) {
  63. if (drinks[choice - 1].numInMachine == 0) {
  64. cout << "Sorry, " << drinks[choice - 1].name <<
  65. " is sold out." << endl;
  66. continue; // Skip the rest of the loop for this iteration
  67. }
  68.  
  69. // Get money from user
  70. do {
  71. cout << "Insert money ($0 to $1): ";
  72. cin >> moneyInserted;
  73.  
  74. if (moneyInserted < 0 || moneyInserted > 1) {
  75. cout << "Invalid amount. Please insert money between $0 and"
  76. "$1." << endl;
  77. }
  78. } while (moneyInserted < 0 || moneyInserted > 1);
  79.  
  80. if (moneyInserted >= drinks[choice - 1].cost) {
  81. double change = moneyInserted - drinks[choice - 1].cost;
  82. cout << "Dispensing " << drinks[choice - 1].name << ". Your "
  83. "change is $" << fixed << setprecision(2) << change << endl;
  84. drinks[choice - 1].numInMachine--;
  85. } else {
  86. cout << "Not enough money inserted. Returning $" << fixed <<
  87. setprecision(2) << moneyInserted << endl;
  88. }
  89. } else if (choice != 99) {
  90. cout << "Invalid selection! Please select a valid option." << endl;
  91. }
  92.  
  93. } while (choice != 99); // End of the main loop
  94.  
  95. return 0;
  96. }
  97.  
  98.  
Success #stdin #stdout 0.01s 5280KB
stdin
1
0.85
5
1
2
0.30
99
stdout
Available Drinks:
1. Cola - $0.75
2. Root Beer - $0.75
3. Lemon-Lime - $0.75
4. Grape Soda - $0.80
5. Cream Soda - $0.80
99. Quit and Display Machine Earnings
Select a drink (1-5) or type 99 to quit: Insert money ($0 to $1): Dispensing Cola. Your change is $0.10
Available Drinks:
1. Cola - $0.75
2. Root Beer - $0.75
3. Lemon-Lime - $0.75
4. Grape Soda - $0.80
5. Cream Soda - $0.80
99. Quit and Display Machine Earnings
Select a drink (1-5) or type 99 to quit: Insert money ($0 to $1): Dispensing Cream Soda. Your change is $0.20
Available Drinks:
1. Cola - $0.75
2. Root Beer - $0.75
3. Lemon-Lime - $0.75
4. Grape Soda - $0.80
5. Cream Soda - $0.80
99. Quit and Display Machine Earnings
Select a drink (1-5) or type 99 to quit: Insert money ($0 to $1): Not enough money inserted. Returning $0.30
Available Drinks:
1. Cola - $0.75
2. Root Beer - $0.75
3. Lemon-Lime - $0.75
4. Grape Soda - $0.80
5. Cream Soda - $0.80
99. Quit and Display Machine Earnings
Select a drink (1-5) or type 99 to quit: