fork download
  1. // Elaine Torrezn #13 CS1A Chapter 11 – Drink Machine Simulator
  2.  
  3. /********************************************************************************************
  4.  *
  5.  * DRINK MACHINE SIMULATOR
  6.  *
  7.  * ------------------------------------------------------------------------------------------
  8.  * This program simulates a soft drink vending machine. A structure is used to store the
  9.  * following data for each drink:
  10.  *
  11.  * - Drink name
  12.  * - Drink cost
  13.  * - Number of drinks currently in the machine
  14.  *
  15.  * The program creates an array of five structures and initializes them with the following
  16.  * data (all starting with 20 drinks each):
  17.  *
  18.  * Cola $0.75
  19.  * Root Beer $0.75
  20.  * Lemon-Lime $0.75
  21.  * Grape Soda $0.80
  22.  * Cream Soda $0.80
  23.  *
  24.  * The program then enters a loop that:
  25.  * 1) Displays the list of drinks and a "Quit" option.
  26.  * 2) Allows the user to select a drink or quit the program.
  27.  * 3) If a drink is selected:
  28.  * - Asks the user to insert an amount of money.
  29.  * - Validates the amount (0.00 – 1.00, not negative).
  30.  * - If the drink is sold out, displays a message.
  31.  * - If not enough money is inserted, displays a message and returns all money.
  32.  * - Otherwise, dispenses the drink, shows the change, subtracts one drink from
  33.  * the machine, and adds the drink cost to the machine's total earnings.
  34.  * 4) Repeats until the user chooses to quit.
  35.  *
  36.  * When the user chooses to quit, the program displays the total amount of money the machine
  37.  * has earned.
  38.  * ------------------------------------------------------------------------------------------
  39.  *
  40.  * INPUT
  41.  * choice : User's drink menu choice (1–5 for drinks, 0 to quit)
  42.  * money : Amount of money inserted (0.00 – 1.00)
  43.  *
  44.  * OUTPUT
  45.  * For each purchase:
  46.  * - Messages about sold-out or insufficient funds (if applicable)
  47.  * - Amount of change returned
  48.  * At the end:
  49.  * - Total amount of money earned by the machine
  50.  *
  51.  * INPUT VALIDATION
  52.  * - Drink selection must be 0–5.
  53.  * - When the user enters an amount of money, do not accept negative values or values
  54.  * greater than $1.00.
  55.  *
  56.  ********************************************************************************************/
  57.  
  58. #include <iostream>
  59. #include <iomanip>
  60. #include <string>
  61. using namespace std;
  62.  
  63. //------------------------------------------------------------
  64. // Structure for a drink type in the machine
  65. //------------------------------------------------------------
  66. struct Drink {
  67. string name; // Drink name
  68. double cost; // Drink cost
  69. int count; // Number of drinks in machine
  70. };
  71.  
  72. // Function prototype
  73. void showMenu(const Drink drinks[], int size);
  74.  
  75. int main()
  76. {
  77. const int NUM_DRINKS = 5;
  78.  
  79. // Initialize the drink machine
  80. Drink drinks[NUM_DRINKS] = {
  81. {"Cola", 0.75, 20},
  82. {"Root Beer", 0.75, 20},
  83. {"Lemon-Lime", 0.75, 20},
  84. {"Grape Soda", 0.80, 20},
  85. {"Cream Soda", 0.80, 20}
  86. };
  87.  
  88. int choice; // User's menu choice
  89. double money; // Money inserted
  90. double change; // Change to return
  91. double totalEarnings = 0; // Total amount earned by the machine
  92.  
  93. cout << fixed << setprecision(2);
  94.  
  95. do {
  96. showMenu(drinks, NUM_DRINKS);
  97.  
  98. cout << "Enter your choice (1-5, or 0 to quit): ";
  99. cin >> choice;
  100.  
  101. // Validate menu choice
  102. while (choice < 0 || choice > NUM_DRINKS) {
  103. cout << " **ERROR** Please enter a value from 0 to " << NUM_DRINKS << ": ";
  104. cin >> choice;
  105. }
  106.  
  107. if (choice == 0) {
  108. // Quit
  109. break;
  110. }
  111.  
  112. int index = choice - 1; // Array index (0–4)
  113.  
  114. // Check if drink is sold out
  115. if (drinks[index].count == 0) {
  116. cout << "\nSorry, " << drinks[index].name << " is SOLD OUT.\n\n";
  117. continue; // Skip to next loop iteration
  118. }
  119.  
  120. // Ask user for money
  121. cout << "Insert money for " << drinks[index].name
  122. << " (cost $" << drinks[index].cost << ", max $1.00): ";
  123. cin >> money;
  124.  
  125. // Validate money amount (0.00 – 1.00, not negative)
  126. while (money < 0.0 || money > 1.0) {
  127. cout << " **ERROR** Amount must be between $0.00 and $1.00. Re-enter: ";
  128. cin >> money;
  129. }
  130.  
  131. // Check if enough money was inserted
  132. if (money < drinks[index].cost) {
  133. cout << "\nNot enough money inserted. Returning $" << money << ".\n\n";
  134. } else {
  135. // Successful purchase
  136. change = money - drinks[index].cost;
  137. drinks[index].count--; // One less drink in the machine
  138. totalEarnings += drinks[index].cost;
  139.  
  140. cout << "\nDispensing " << drinks[index].name << "...\n";
  141. cout << "Your change is $" << change << ".\n\n";
  142. }
  143.  
  144. } while (true);
  145.  
  146. // Display total money earned
  147. cout << "\nTotal money earned by the machine: $" << totalEarnings << endl;
  148.  
  149. return 0;
  150. }
  151.  
  152. //------------------------------------------------------------
  153. // Displays the drink menu
  154. //------------------------------------------------------------
  155. void showMenu(const Drink drinks[], int size)
  156. {
  157. cout << "\nDRINK MACHINE MENU\n";
  158. cout << "-----------------------------------------\n";
  159.  
  160. for (int i = 0; i < size; ++i) {
  161. cout << (i + 1) << ". " << left << setw(12) << drinks[i].name
  162. << " $" << drinks[i].cost
  163. << " (" << drinks[i].count << " left)\n";
  164. }
  165.  
  166. cout << "0. Quit\n\n";
  167. }
  168.  
Success #stdin #stdout 0.01s 5324KB
stdin
1
1.00
4
1.00
5
0.50
2
0.75
0
stdout
DRINK MACHINE MENU
-----------------------------------------
1. Cola         $0.75   (20 left)
2. Root Beer    $0.75   (20 left)
3. Lemon-Lime   $0.75   (20 left)
4. Grape Soda   $0.80   (20 left)
5. Cream Soda   $0.80   (20 left)
0. Quit

Enter your choice (1-5, or 0 to quit): Insert money for Cola (cost $0.75, max $1.00): 
Dispensing Cola...
Your change is $0.25.


DRINK MACHINE MENU
-----------------------------------------
1. Cola         $0.75   (19 left)
2. Root Beer    $0.75   (20 left)
3. Lemon-Lime   $0.75   (20 left)
4. Grape Soda   $0.80   (20 left)
5. Cream Soda   $0.80   (20 left)
0. Quit

Enter your choice (1-5, or 0 to quit): Insert money for Grape Soda (cost $0.80, max $1.00): 
Dispensing Grape Soda...
Your change is $0.20.


DRINK MACHINE MENU
-----------------------------------------
1. Cola         $0.75   (19 left)
2. Root Beer    $0.75   (20 left)
3. Lemon-Lime   $0.75   (20 left)
4. Grape Soda   $0.80   (19 left)
5. Cream Soda   $0.80   (20 left)
0. Quit

Enter your choice (1-5, or 0 to quit): Insert money for Cream Soda (cost $0.80, max $1.00): 
Not enough money inserted. Returning $0.50.


DRINK MACHINE MENU
-----------------------------------------
1. Cola         $0.75   (19 left)
2. Root Beer    $0.75   (20 left)
3. Lemon-Lime   $0.75   (20 left)
4. Grape Soda   $0.80   (19 left)
5. Cream Soda   $0.80   (20 left)
0. Quit

Enter your choice (1-5, or 0 to quit): Insert money for Root Beer (cost $0.75, max $1.00): 
Dispensing Root Beer...
Your change is $0.00.


DRINK MACHINE MENU
-----------------------------------------
1. Cola         $0.75   (19 left)
2. Root Beer    $0.75   (19 left)
3. Lemon-Lime   $0.75   (20 left)
4. Grape Soda   $0.80   (19 left)
5. Cream Soda   $0.80   (20 left)
0. Quit

Enter your choice (1-5, or 0 to quit): 
Total money earned by the machine: $2.30