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