Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. /* Chetan Grewal
  2. Lab 4
  3. Description:
  4.  
  5. */
  6.  
  7. #include<iostream>
  8. #include<string>
  9. #include<iomanip>
  10. using namespace std;
  11.  
  12. // Sets up struct
  13. struct drinkInfo{
  14. string name;
  15. double price;
  16. int quantity;
  17. };
  18.  
  19. //Money calculation function
  20. void moneyCalc(double &due, double &total);
  21.  
  22. int main(){
  23.  
  24. drinkInfo drinks[5]; // Makes array for the struct
  25. int choice = 0; // Drink option user selects
  26. double due;
  27. double totalPaid = 0;
  28. // Set values of all the drinks
  29. drinks[0] = {"Cola", .75, 5};
  30. drinks[1] = {"Root Beer", .75, 20};
  31. drinks[2] = {"Lemon-Lime", .75, 20};
  32. drinks[3] = {"Grape Soda", .80, 20};
  33. drinks[4] = {"Cream Soda", .80, 20};
  34.  
  35. // Loop runs as long as option 6 isn't selected
  36. while(choice !=6){
  37. cout << "\n\n";
  38. for(int i=0; i<5; i++){ //Display Options
  39. cout << (i+1) << ") " << drinks[i].name << "\t$";
  40. cout << fixed << setprecision(2) << drinks[i].price << "\t";
  41. cout << drinks[i].quantity << "\n";
  42. }
  43. cout << "6) ";
  44. cout << "Leave the drink machine\n";
  45.  
  46. // Ask user to choose drink or quit
  47. cout << "\nPlease choice an option: ";
  48. cin >> choice;
  49. // Display error if invald choice
  50. while(choice <1 || choice > 6){
  51. cout << "\nError, please enter a valid choice: ";
  52. cin >> choice;
  53. }
  54.  
  55. // Switch executes similar program for various drinks
  56. switch(choice){
  57. case 1 :
  58. // Displays error if the drink quantity is 0
  59. if(drinks[0].quantity == 0){
  60. cout << drinks[0].name << " is out of stock\n\n";
  61. break; // Breaks out to let user pick different drink
  62. }
  63. due = drinks[0].price; // Sets drink price to a variable
  64. moneyCalc(due, totalPaid); // Money calculation program
  65. totalPaid += due; // Adds money earned to total
  66. drinks[0].quantity -= 1; // Takes 1 drink out of total avaible
  67. break; // Breaks out of switch when done
  68. // Code works the same, but for different drink for cases 2-5
  69. case 2:
  70. if(drinks[1].quantity == 0){
  71. cout << drinks[1].name << " is out of stock\n\n";
  72. break;
  73. }
  74. due = drinks[1].price;
  75. moneyCalc(due, totalPaid);
  76. totalPaid += due;
  77. drinks[1].quantity -= 1;
  78. break;
  79. case 3:
  80. if(drinks[2].quantity == 0){
  81. cout << drinks[2].name << " is out of stock\n\n";
  82. break;
  83. }
  84. due = drinks[2].price;
  85. moneyCalc(due, totalPaid);
  86. totalPaid += due;
  87. drinks[2].quantity -= 1;
  88. break;
  89. case 4:
  90. if(drinks[3].quantity == 0){
  91. cout << drinks[3].name << " is out of stock\n\n";
  92. break;
  93. }
  94. due = drinks[3].price;
  95. moneyCalc(due, totalPaid);
  96. totalPaid += due;
  97. drinks[3].quantity -= 1;
  98. break;
  99. case 5:
  100. if(drinks[4].quantity == 0){
  101. cout << drinks[4].name << " is out of stock\n\n";
  102. break;
  103. }
  104. due = drinks[4].price;
  105. moneyCalc(due, totalPaid);
  106. totalPaid += due;
  107. drinks[4].quantity -= 1;
  108. break;
  109. // Breaks out of switch to exit
  110. case 6:
  111. break;
  112. }
  113. }
  114. // Display total earned
  115. cout << "Total money earned this session: " << totalPaid;
  116. cout << "\n\n";
  117. return 0;
  118. }
  119.  
  120. void moneyCalc(double &due, double &total){
  121. bool paid = 0;
  122. double payment;
  123. double change;
  124. while(paid==0){
  125. if(due==0){
  126. paid = 1;
  127. }
  128. // Ask user for money
  129. cout << "\nThe drink costs $" << due << ".";
  130. cout << "\nPlease insert money for drink: ";
  131. cin >> payment;
  132. // Display error if wrong amount entered
  133. while(payment < 0 || payment > 1){
  134. cout << "\nError, enter amount between $0.00 and $1.00: ";
  135. cin >> payment;
  136. }
  137. // If payment is over the amount due
  138. // user recieves change
  139. if(payment > due){
  140. change = payment - due;
  141. cout << "\nYour change is " << change;
  142. due = 0;
  143. // If the payment is exact, no change recieved
  144. } else{
  145. due -= payment;
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement