Guest User

Untitled

a guest
Jan 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Vending Machine V3
  4. //
  5. // Created by Zach Mather on 01/12/2017.
  6. // Copyright © 2017 Zach Mather. All rights reserved.
  7.  
  8.  
  9.  
  10.  
  11. // This was built in Xcode so run in Xcode for marking
  12.  
  13.  
  14.  
  15. #include <iostream>
  16.  
  17. using namespace std;
  18.  
  19. int main() {
  20. float money;
  21. float dolla;
  22. float money1;
  23. string drinks[10] = {"Cola","Sprite","7-up","Pepsi","Water","Iron-Bru","Vimto","Lucizade", "Lilt", "Dr-Pepper"};
  24. int price = 1;
  25. string crisps[10] = {"Galaxy", "Cadbury", "Freddo" , "Snickers", "Twix","KitKat" ,"M&M's","Wispa", "Marsbar", "Double Decker"};
  26. double priceC[] = {0.65,0.55,0.55,0.60,0.80,0.75,0.60,0.55,0.55,0.65};
  27. string choc[10] = {"Cheese & Onion" ,"Prawn Cocktail","Ready Salted", "Salt & Vinegar", "Flaming Hot" ,"Sweet Chilli","BBQ", "Nacho Cheese" , "Pickeled Onion", "Paprika"};
  28. double priceChoc[] = {0.85,0.80,0.75,0.80,0.85,0.80,0.75,0.80,0.75,0.75};
  29. // using the arays allowed me to easily output a name of the drink and not have to use individual switch statments for each drink chosen
  30.  
  31. string finalChoice[50];
  32. int arrayCounter = 0;
  33.  
  34.  
  35.  
  36.  
  37. int food;
  38. // this is the users choice for each item, the max value it can be is 10 as there are 10 choices.
  39. int choice;
  40. bool flag1 = true;
  41. bool flag2 = true;
  42. bool flag3 = true;
  43. // bool flag 1 and 2 are used to loop the switch incase the user enters a number bigger than 10
  44. // flag 3 is used to loop the whole code if the user wants another snack and has enough money
  45.  
  46. float bal;
  47. // bal changes depending on what the user buys and is then updated to become to new balance if the code is run again if the user wants another snack
  48. char answer;
  49. // canswer is the users answer if they want another snack again or not
  50.  
  51.  
  52.  
  53. cout << "Welcome to SnackTastic, The worlds best vending machine" << endl;
  54. cout << "Please insert your money by typing in the amout you have" << endl;
  55. cin >> money;
  56. while (cin.fail()){
  57. cin.clear();
  58. cin.ignore();
  59. cout << "Please enter a number" << endl;
  60. cin >> money;
  61. }
  62. // this stops the user from entering a letter instead of a number.
  63.  
  64. if (money < 1){
  65. while (money < 1){
  66.  
  67. money1 = money;
  68. cout << "Sorry you dont have enough money for a snack" << endl;
  69. cout << "Your balance is £" << money << endl;
  70. cout << "You need atleast £1 to buy a snack" << endl;
  71. cout << "Please insert some more money" << endl;
  72. cin >> dolla;
  73. while (cin.fail()){
  74. cin.clear();
  75. cin.ignore();
  76. cout << "Please enter a number" << endl;
  77. cin >> dolla;
  78. }
  79. money = money1+dolla;
  80. }
  81. }
  82. // allows the user to enter more money
  83. while (flag3 == true) {
  84. flag1 = true;
  85. flag2 = true;
  86. // this sets the bools to true again after the code has done a cycle, without this the user cannot order another snack
  87.  
  88.  
  89.  
  90.  
  91. cout << "You have £" << money << endl;
  92.  
  93. cout << "Would you like a Drink or Chocolate or Crisps" << endl;
  94. cout << "1 = Drink, 2 = Chocolate, 3 = Crisps " << endl;
  95.  
  96. cin >> choice;
  97.  
  98. if (choice > 3 || choice < 1)
  99. {
  100. cout << "Please enter a number btween 1 and 3" << endl;
  101. flag3 = true;
  102. continue;
  103. }
  104.  
  105. while (cin.fail()){
  106. cin.clear();
  107. cin.ignore();
  108. cout << "Please enter a number" << endl;
  109. cin >> choice;
  110. }
  111. // this stops the user from breaking the switch by entering a letter instead of a number
  112.  
  113.  
  114.  
  115. while (flag2 == true) {
  116. // this repeats the switch is the users enters the defult bracket
  117.  
  118.  
  119. switch (choice) {
  120.  
  121.  
  122.  
  123. // Drink
  124. case 1:
  125. while (flag1 == true) {
  126. cout << "Please choose a type of Drink by entering the number." << endl;
  127. cout << "1 = Cola, 2 = Sprite, 3 = 7-up, 4 = Pepsi, 5 = Water" << endl;
  128. cout << "6 = Iron-Bru, 7 = Vimto, 8 = Lucizade, 9 = Lilt, 10 = Dr-Pepper" << endl;
  129.  
  130.  
  131. cin >> food;
  132. while (cin.fail()){
  133. cin.clear();
  134. cin.ignore();
  135. cout << "Please enter a number between 1 and 10" << endl;
  136. cin >> food;
  137. }
  138.  
  139.  
  140. if ( food <= 10 && food > 0){
  141. cout << "You chose a " << drinks[food-1] << " that was £" << price << endl;
  142. finalChoice[arrayCounter] = drinks[food-1];
  143. arrayCounter++;
  144. bal = money - price;
  145. cout << "Your new balance is £" << bal << endl;
  146. flag1 = false;
  147. flag2 = false;
  148. }else{
  149. cout << "Error, please choose a number between 1 and 10" << endl;
  150.  
  151. }
  152. }
  153. break;
  154.  
  155.  
  156. // Chocolate
  157. case 2:
  158. while (flag1 == true){
  159. cout << "Please choose a flavour of Chocolate by entering the number." << endl;
  160. cout << "1 = Galaxy, 2 = Cadbury, 3 = Freddo, 4 = Snickers, 5 = Twix" << endl;
  161. cout << "6 = KitKat, 7 = M&M's, 8 = Wispa, 9 = Marsbar, 10 = Double Decker" << endl;
  162. cin >> food;
  163. while (cin.fail()){
  164. cin.clear();
  165. cin.ignore();
  166. cout << "Please enter a number between 1 and 10" << endl;
  167. cin >> food;
  168. }
  169. if ( food<= 10 && food > 0){
  170. cout << "You chose a " << crisps[food-1] << " that was £" << priceChoc[food-1] << endl;
  171. finalChoice[arrayCounter] = crisps[food-1];
  172. arrayCounter++;
  173. bal = money - priceChoc[food-1];
  174. cout << "Your new balance is £" << bal << endl;
  175. flag1 = false;
  176. flag2 = false;
  177. }else{
  178. cout << "Error, please choose a number between 1 and 10" << endl;
  179.  
  180. }
  181. }
  182. break;
  183.  
  184.  
  185. // Crisp
  186.  
  187.  
  188. case 3:
  189. while (flag1 == true){
  190. cout << "Please choose a flavour of Crisp by entering the number." << endl;
  191. cout << "1 = Cheese & Onion, 2 = Prawn Cocktail, 3 = Ready Salted" << endl;
  192. cout << "4 = Salt & Vinegar, 5 = Flaming Hot, 6 = Sweet Chilli" << endl;
  193. cout << "7 = BBQ, 8 = Nacho Cheese, 9 = Pickeled Onion, 10 = Paprika" << endl;
  194. cin >> food;
  195. while (cin.fail()){
  196. cin.clear();
  197. cin.ignore();
  198. cout << "Please enter a number between 1 and 10" << endl;
  199. cin >> food;
  200. }
  201. if ( food<= 10 && food > 0 ){
  202. cout << "You chose " << choc[food-1] << " flavour crisps that was £" << priceC[food-1] << endl;
  203. finalChoice[arrayCounter] = choc[food-1];
  204. arrayCounter++;
  205. bal = money - priceC[food-1];
  206. cout << "Your new balance is £" << bal << endl;
  207. flag1 = false;
  208. flag2 = false;
  209. }else{
  210. cout << "Error, please choose a number between 1 and 10" << endl;
  211.  
  212. }
  213. }
  214. break;
  215. // if the number is not within the parameter
  216. default:
  217. cout << "Error, please chose a number between 1 and 3" << endl;
  218.  
  219. break;
  220.  
  221. }
  222.  
  223. // if (bal < 1){
  224. // while (money < 1){
  225. //
  226. // money1 = money;
  227. // cout << "Sorry you dont have enough money for a snack" << endl;
  228. // cout << "Your balance is " << money << endl;
  229. // cout << "You need atleast £1 to buy a snack" << endl;
  230. // cout << "Please insert some more money" << endl;
  231. // cin >> dolla;
  232. // while (cin.fail()){
  233. // cin.clear();
  234. // cin.ignore();
  235. // cout << "Please enter a number" << endl;
  236. // cin >> dolla;
  237. // }
  238. // money = money1+dolla;
  239. // }
  240. // }
  241. //
  242. // return 0;
  243. // }
  244.  
  245. money = bal;
  246. cout << "Would you like another snack?" << endl;
  247. cout << "Y or N" << endl;
  248. cin >> answer;
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. if (toupper(answer) == 'Y' && money >0.55) {
  256. // toupper is a very useful as it means the user doesn't have to enter a uppercase letter, the program will still work
  257.  
  258. flag3 = true;
  259.  
  260. }
  261.  
  262. else if (toupper(answer) == 'Y' && money < 1) {
  263.  
  264. while (money < 1){
  265.  
  266. money1 = money;
  267. cout << "Sorry you dont have enough money for a snack" << endl;
  268. cout << "Your balance is " << money << endl;
  269. cout << "You need atleast £1 to buy a snack" << endl;
  270. cout << "Please insert some more money" << endl;
  271. cin >> dolla;
  272. while (cin.fail()){
  273. cin.clear();
  274. cin.ignore();
  275. cout << "Please enter a number" << endl;
  276. cin >> dolla;
  277. }
  278. money = money1+dolla;
  279. }
  280. flag3 = true;
  281. }
  282.  
  283. else if (toupper(answer) == 'N') {
  284. flag3 = false;
  285. cout << "Your change is £" << bal << endl;
  286. cout << "Enjoy your ";
  287. for (int x = 0; x<50; x++){
  288. if (finalChoice[x]!=""){
  289. if(arrayCounter == 1)
  290. {
  291. cout << finalChoice[x] << "";
  292. if (choice == 3){
  293. cout << " crisps";
  294. }
  295. }
  296. else
  297. {
  298. cout << finalChoice[x];
  299.  
  300. if(finalChoice[x+2] != "")
  301. {
  302. cout << ", ";
  303. }
  304. else if(finalChoice[x+1] != "")
  305. {
  306. cout << " and ";
  307. }
  308.  
  309. }
  310. }
  311. }cout << endl;
  312.  
  313.  
  314. }
  315.  
  316. }
  317.  
  318.  
  319.  
  320.  
  321. }
  322. return 0;
  323.  
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment