fork download
  1. //Maxwell Brewer CS1A Chapter 11, p. 648, #13
  2. //
  3. /*******************************************************************************
  4.  * VEND SOFT DRINKS
  5.  * _____________________________________________________________________________
  6.  * This program simulates the operations of a soft drink vending machine. It allows
  7.  * users to view available drinks, select a drink, insert money, and receive change.
  8.  * The program also tracks the machine's profits and updates the stock of drinks
  9.  * after each purchase.
  10.  *
  11.  * INPUT:
  12.  * - User inputs the amount of money to insert for their selected drink.
  13.  * - User selects a drink from a list by entering a number corresponding
  14.  * to the drink.
  15.  *
  16.  * OUTPUT:
  17.  * - Displays the change returned to the user.
  18.  * - Updates the number of available drinks and displays the current
  19.  * machine earnings.
  20.  * - Informs the user if a drink is sold out or if an invalid amount
  21.  * is entered.
  22.  *******************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. // Structure to represent a soft drink
  29. struct Drink {
  30. string name; // Name of the drink
  31. double cost; // Cost of a single drink
  32. int numberAvailable; // Number of available drinks
  33. };
  34.  
  35. // Function prototypes
  36. void initializeDrinks(Drink*&, unsigned&);
  37. void displayDrinks(const Drink*, const unsigned);
  38. double processRequest(Drink*, const unsigned);
  39. void release(Drink*&);
  40.  
  41. // Main function
  42. int main() {
  43. cout << "============================================\n";
  44. cout << "\t :: Soft Drink Machine ::\n";
  45. cout << "This program simulates a soft drink machine.\n";
  46. cout << "It displays the list of available drinks,\n";
  47. cout << "allows you to choose a drink, calculates the change,\n";
  48. cout << "and keeps track of the profit earned.\n";
  49. cout << "============================================\n";
  50.  
  51. // Variables
  52. int choice = 0;
  53. unsigned numDrinks = 0;
  54. double totalEarned = 0;
  55.  
  56. // Pointer for the drinks array
  57. Drink* drinks = nullptr;
  58.  
  59. // Initialize the drinks array
  60. initializeDrinks(drinks, numDrinks);
  61.  
  62. // Main loop
  63. do {
  64. displayDrinks(drinks, numDrinks);
  65. cout << "Enter the number of the drink you want [or 0 to quit]: ";
  66. cin >> choice;
  67.  
  68. if (choice < 0 || choice > numDrinks) {
  69.  
  70. cout << "Invalid choice! Please choose a valid number or 0 to quit.\n";
  71.  
  72. } else if (choice > 0 && choice <= numDrinks) {
  73. totalEarned += processRequest(drinks, choice - 1);
  74. }
  75.  
  76. cout << "============================================\n";
  77.  
  78. } while (choice != 0);
  79.  
  80. // Final earnings display
  81. cout << "Total money earned in this session: $"
  82. << setprecision(3) << totalEarned << "\n";
  83. cout << "--------------------------------------------\n";
  84. cout << "Thank you for using the vending machine!\n";
  85. cout << "============================================\n";
  86.  
  87. // Release dynamically allocated memory
  88. release(drinks);
  89. return 0;
  90. }
  91.  
  92. // Initialize the drinks array with data
  93. void initializeDrinks(Drink*& drinks, unsigned& size) {
  94. size = 5;
  95. drinks = new Drink[size];
  96.  
  97. // Drink names, prices, and quantities
  98. string names[] = {"Cola", "Root Beer", "Lemon-Lime", "Grape Soda",
  99. "Cream Soda"};
  100.  
  101. double prices[] = {0.75, 0.75, 0.75, 0.80, 0.80};
  102. int quantities[] = {20, 20, 20, 20, 20};
  103.  
  104. for (unsigned i = 0; i < size; ++i) {
  105. drinks[i].name = names[i];
  106. drinks[i].cost = prices[i];
  107. drinks[i].numberAvailable = quantities[i];
  108. }
  109. }
  110.  
  111. // Display available drinks
  112. void displayDrinks(const Drink* drinks, const unsigned size) {
  113. cout << setw(5) << left << "#" << setw(15) << left << "Drink Name"
  114. << "Cost" << setw(20) << right << "Available" << endl;
  115. cout << "--------------------------------------------\n";
  116.  
  117. for (int i = 0; i < size; i++) {
  118. cout << setw(5) << left << (i + 1) << setw(15) << left
  119. << drinks[i].name
  120. << setw(6) << right << fixed << setprecision(2) << drinks[i].cost
  121. << setw(10) << right << drinks[i].numberAvailable << endl;
  122. }
  123. cout << "--------------------------------------------\n";
  124. cout << "Enter the drink number to select or 0 to quit.\n";
  125. }
  126.  
  127. // Process user selection and return the earnings
  128. double processRequest(Drink* drinks, const unsigned index) {
  129. cout << "============================================\n";
  130. if (drinks[index].numberAvailable <= 0) {
  131. cout << "Sorry, this drink is sold out.\n";
  132. return 0;
  133. }
  134.  
  135. double moneyInserted;
  136. cout << "Enter the amount of money (max $1.00): ";
  137. cin >> moneyInserted;
  138.  
  139. while (moneyInserted != 0 && (moneyInserted > 1.00 ||
  140. moneyInserted < drinks[index].cost))
  141.  
  142. {
  143. cout << "Invalid amount. Please enter an amount between "
  144. << drinks[index].cost << " and 1.00 or 0 to cancel: ";
  145. cin >> moneyInserted;
  146. }
  147.  
  148. if (moneyInserted == 0) {
  149. return 0;
  150. }
  151.  
  152. cout << "Your change is: $" << fixed << setprecision(3)
  153. << (moneyInserted - drinks[index].cost) << endl;
  154.  
  155. drinks[index].numberAvailable--;
  156. return drinks[index].cost;
  157. }
  158.  
  159. // Release dynamically allocated memory, separate function helps with efficiency
  160. void release(Drink* &drinks) {
  161. delete[] drinks;
  162. drinks = nullptr;
  163. }
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
============================================
	     :: Soft Drink Machine ::
This program simulates a soft drink machine.
It displays the list of available drinks,
allows you to choose a drink, calculates the change,
and keeps track of the profit earned.
============================================
#    Drink Name     Cost           Available
--------------------------------------------
1    Cola             0.75        20
2    Root Beer        0.75        20
3    Lemon-Lime       0.75        20
4    Grape Soda       0.80        20
5    Cream Soda       0.80        20
--------------------------------------------
Enter the drink number to select or 0 to quit.
Enter the number of the drink you want [or 0 to quit]: ============================================
Total money earned in this session: $0.000
--------------------------------------------
Thank you for using the vending machine!
============================================