fork download
  1. //Mia Agramon CS1A Chapter 11, P.648, #13
  2. //
  3. /*******************************************************************************
  4.  * Drink Dispenser
  5.  * _____________________________________________________________________________
  6.  * This programs simulates a drink machine that dispense: cola, root beer,
  7.  * lemon-lime, grape soda, and cream soda.
  8.  *
  9.  * INPUT
  10.  * pick a drink
  11.  * payment
  12.  * OUTPUT
  13.  * drink
  14.  * change
  15.  * total amount of money earned
  16.  ******************************************************************************/
  17.  
  18. #include <iostream>
  19. #include <string>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. struct vendingMachine
  24. {
  25. string drinkName;
  26. float drinkCost;
  27. int amountDrinks;
  28. };
  29.  
  30. //Function Prototype
  31. void menu (vendingMachine soda[], int numDrinks, float &moneyEarned);
  32.  
  33. //Global Constants
  34. const int numDRINKS = 5;
  35.  
  36. int main()
  37. {
  38. //Initialize Variables
  39. int choice = 0;
  40. float payment;
  41. float change;
  42. float moneyEarned = 0;
  43.  
  44. vendingMachine drinks[numDRINKS] = {
  45. {"Cola", 0.75, 20},
  46. {"Root Beer", 0.75, 20},
  47. {"Lemon-Lime", 0.75, 20},
  48. {"Grape Soda", 0.80, 20},
  49. {"Cream Soda", 0.80, 20},
  50. };
  51.  
  52. cout << setprecision(2) << fixed << showpoint;
  53.  
  54. //Call Function
  55. menu(drinks, numDRINKS, moneyEarned);
  56.  
  57. return 0;
  58. }
  59.  
  60. //Menu Function
  61. void menu (vendingMachine soda[], int numDrinks, float &moneyEarned)
  62. {
  63. int choice;
  64. float payment;
  65. float change;
  66. cout << " MENU " << endl;
  67. for (int i = 0; i < numDrinks; i++)
  68. {
  69. cout << i + 1 << ". " << soda[i].drinkName << " - $" << soda[i].drinkCost << endl;
  70. }
  71.  
  72. cout << "Please enter the number of the drink you want: ";
  73. cin >> choice;
  74. cout << endl;
  75.  
  76. //Input Validation
  77. if (choice < 1 || choice > numDrinks)
  78. {
  79. cout << "Invalid" << endl;
  80. menu(soda, numDrinks, moneyEarned);
  81. return;
  82. }
  83.  
  84. cout << "Enter money: " << soda[choice - 1].drinkCost;
  85. cin >> payment;
  86. cout << endl;
  87.  
  88. if (payment < soda[choice - 1].drinkCost)
  89. {
  90. cout << "Please try again." << endl;
  91. menu(soda, numDrinks, moneyEarned);
  92. return;
  93. }
  94.  
  95. soda[choice - 1].amountDrinks--;
  96. change = payment - soda[choice - 1].drinkCost;
  97. moneyEarned += soda[choice - 1].drinkCost;
  98.  
  99. cout << "Enjoy your " << soda[choice - 1].drinkName << "!" << endl;
  100. cout << "Change :$" << change << endl;
  101. }
Success #stdin #stdout 0.01s 5268KB
stdin
1
1
stdout
	MENU	
1. Cola - $0.75
2. Root Beer - $0.75
3. Lemon-Lime - $0.75
4. Grape Soda - $0.80
5. Cream Soda - $0.80
Please enter the number of the drink you want: 
Enter money: 0.75
Enjoy your Cola!
Change :$0.25