fork download
  1. //Sam Partovi CS1A Second Practicum
  2. /*******************************************************************************
  3.  * SIMULATE VENDING MACHINE
  4.  *
  5.  * This program simulates a vending machine with specified drinks, with given
  6.  * costs and quantity of drinks stored. The program displays a menu of available
  7.  * drinks for selection.
  8. --------------------------------------------------------------------------------
  9. *INPUT
  10. * SIZE : Number of drinks available
  11. * name : Drink name
  12. * cost : Cost ($) of drink
  13. * supply : Drink quantity in machine
  14. * choice : Selected menu option
  15. * moneyInserted : Money ($) paid to machine
  16. *
  17. *OUTPUT
  18. * machineEarnings : Money ($) earned by machine
  19. * Menu of available drinks.
  20. *******************************************************************************/
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <string>
  24. using namespace std;
  25.  
  26. const int SIZE = 5; //INPUT - Number of drinks available
  27.  
  28. struct Drinks {
  29. string name; //INPUT - Drink name
  30. float cost; //INPUT - Cost ($) of drink
  31. int supply; //INPUT - Drink quantity in machine
  32. };
  33.  
  34. //Function prototype for DisplayMenu
  35. void DisplayMenu(Drinks drink[SIZE], int choice);
  36.  
  37. int main() {
  38.  
  39. //Assign drink information to each drink
  40. Drinks drink[SIZE] = {
  41. {"Cola", 0.75, 20},
  42. {"Root Beer", 0.75, 20},
  43. {"Lemon-Lime", 0.75, 20},
  44. {"Grape Soda", 0.80, 20},
  45. {"Cream Soda", 0.80, 20}
  46. };
  47.  
  48. int choice; //INPUT - Selected menu option
  49. float moneyInserted; //INPUT - Money ($) paid to machine
  50. float machineEarnings; //OUTPUT - Money ($) earned by machine
  51.  
  52. moneyInserted = 0;
  53. machineEarnings = 0;
  54.  
  55. do {
  56. //Call DisplayMenu function
  57. DisplayMenu(drink, choice);
  58.  
  59. //Prompt for drink selection
  60. cin >> choice;
  61.  
  62. //Prompt for moneyInserted if drink is available
  63. if (choice != 99 && drink[choice - 1].supply > 0) {
  64. cout << "\nInsert money: ";
  65. cin >> moneyInserted;
  66. }
  67.  
  68. //Display error if drink is out of stock
  69. if (drink[choice - 1].supply <= 0 && choice != 99) {
  70. cout << "\nError: Item is out of stock.";
  71.  
  72. }
  73.  
  74. //Dispense drink if money has been inserted
  75. if (moneyInserted > 0) {
  76. if(drink[choice - 1].cost <= moneyInserted) {
  77. cout << "\nDispensing " << drink[choice - 1].name << "..." << endl;
  78.  
  79. //Display change to be returned
  80. cout << fixed << setprecision(2) << "Your change is $"
  81. << moneyInserted - drink[choice - 1].cost << ".\n" << endl;
  82.  
  83. //Add cost of chosen drink to total machine earnings
  84. machineEarnings += drink[choice - 1].cost;
  85.  
  86. //Decrement drink quantity in machine
  87. drink[choice - 1].supply--;
  88. }
  89.  
  90. //Display error if moneyInserted is not enough for chosen drink
  91. else cout << "\nError: Not enough money inserted.";
  92. }
  93.  
  94. //Display error if invalid amount of money inserted
  95. else cout << "\nError: Must insert more than $0.";
  96. } while (choice != 99); //Terminate loop if 99 is entered
  97.  
  98. //Display total machine earnings
  99. cout << fixed << setprecision(2) << "\nMachine earned $" << machineEarnings;
  100.  
  101. return 0;
  102.  
  103. }
  104.  
  105. /*******************************************************************************
  106.  *Function definition for DisplayMenu:
  107.  * This function displays a menu of available drinks and allows for drink
  108.  * selection.
  109.  ******************************************************************************/
  110. void DisplayMenu(Drinks drink[SIZE], int choice) {
  111. cout << "Menu:" << endl;
  112.  
  113. //Display each drink in a numbered list
  114. for(int i = 0; i < SIZE; i++) {
  115. cout << i + 1 << ". " << drink[i].name << endl;
  116. }
  117. cout << "Enter 99 to Quit" << endl;
  118. }
Success #stdin #stdout 0.01s 5280KB
stdin
1
2
1
3
1
2
1
3
1
4
5
1
2
1
3
1
2
1
1
2
1
3
1
2
1
3
1
4
5
1
2
1
3
1
2
1
3
1
4
5
1
1
2
1
3
1
4
5
4
5
1
1
2
1
3
1
4
5
4
5
3
1
4
5
1
1
2
1
3
1
4
5
4
5
1
1
2
1
3
1
4
5
4
5
99
stdout
Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $1.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $2.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $1.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $2.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $3.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cream Soda...
Your change is $0.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $1.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $2.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $1.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $2.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $3.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cream Soda...
Your change is $0.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Cola...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Root Beer...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Lemon-Lime...
Your change is $0.25.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Insert money: 
Dispensing Grape Soda...
Your change is $4.20.

Menu:
1. Cola
2. Root Beer
3. Lemon-Lime
4. Grape Soda
5. Cream Soda
Enter 99 to Quit

Dispensing ...
Your change is $5.00.


Machine earned $32.10