Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Vending Machine V3
- //
- // Created by Zach Mather on 01/12/2017.
- // Copyright © 2017 Zach Mather. All rights reserved.
- // This was built in Xcode so run in Xcode for marking
- #include <iostream>
- using namespace std;
- int main() {
- float money;
- float dolla;
- float money1;
- string drinks[10] = {"Cola","Sprite","7-up","Pepsi","Water","Iron-Bru","Vimto","Lucizade", "Lilt", "Dr-Pepper"};
- int price = 1;
- string crisps[10] = {"Galaxy", "Cadbury", "Freddo" , "Snickers", "Twix","KitKat" ,"M&M's","Wispa", "Marsbar", "Double Decker"};
- double priceC[] = {0.65,0.55,0.55,0.60,0.80,0.75,0.60,0.55,0.55,0.65};
- string choc[10] = {"Cheese & Onion" ,"Prawn Cocktail","Ready Salted", "Salt & Vinegar", "Flaming Hot" ,"Sweet Chilli","BBQ", "Nacho Cheese" , "Pickeled Onion", "Paprika"};
- double priceChoc[] = {0.85,0.80,0.75,0.80,0.85,0.80,0.75,0.80,0.75,0.75};
- // 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
- string finalChoice[50];
- int arrayCounter = 0;
- int food;
- // this is the users choice for each item, the max value it can be is 10 as there are 10 choices.
- int choice;
- bool flag1 = true;
- bool flag2 = true;
- bool flag3 = true;
- // bool flag 1 and 2 are used to loop the switch incase the user enters a number bigger than 10
- // flag 3 is used to loop the whole code if the user wants another snack and has enough money
- float bal;
- // 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
- char answer;
- // canswer is the users answer if they want another snack again or not
- cout << "Welcome to SnackTastic, The worlds best vending machine" << endl;
- cout << "Please insert your money by typing in the amout you have" << endl;
- cin >> money;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number" << endl;
- cin >> money;
- }
- // this stops the user from entering a letter instead of a number.
- if (money < 1){
- while (money < 1){
- money1 = money;
- cout << "Sorry you dont have enough money for a snack" << endl;
- cout << "Your balance is £" << money << endl;
- cout << "You need atleast £1 to buy a snack" << endl;
- cout << "Please insert some more money" << endl;
- cin >> dolla;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number" << endl;
- cin >> dolla;
- }
- money = money1+dolla;
- }
- }
- // allows the user to enter more money
- while (flag3 == true) {
- flag1 = true;
- flag2 = true;
- // this sets the bools to true again after the code has done a cycle, without this the user cannot order another snack
- cout << "You have £" << money << endl;
- cout << "Would you like a Drink or Chocolate or Crisps" << endl;
- cout << "1 = Drink, 2 = Chocolate, 3 = Crisps " << endl;
- cin >> choice;
- if (choice > 3 || choice < 1)
- {
- cout << "Please enter a number btween 1 and 3" << endl;
- flag3 = true;
- continue;
- }
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number" << endl;
- cin >> choice;
- }
- // this stops the user from breaking the switch by entering a letter instead of a number
- while (flag2 == true) {
- // this repeats the switch is the users enters the defult bracket
- switch (choice) {
- // Drink
- case 1:
- while (flag1 == true) {
- cout << "Please choose a type of Drink by entering the number." << endl;
- cout << "1 = Cola, 2 = Sprite, 3 = 7-up, 4 = Pepsi, 5 = Water" << endl;
- cout << "6 = Iron-Bru, 7 = Vimto, 8 = Lucizade, 9 = Lilt, 10 = Dr-Pepper" << endl;
- cin >> food;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number between 1 and 10" << endl;
- cin >> food;
- }
- if ( food <= 10 && food > 0){
- cout << "You chose a " << drinks[food-1] << " that was £" << price << endl;
- finalChoice[arrayCounter] = drinks[food-1];
- arrayCounter++;
- bal = money - price;
- cout << "Your new balance is £" << bal << endl;
- flag1 = false;
- flag2 = false;
- }else{
- cout << "Error, please choose a number between 1 and 10" << endl;
- }
- }
- break;
- // Chocolate
- case 2:
- while (flag1 == true){
- cout << "Please choose a flavour of Chocolate by entering the number." << endl;
- cout << "1 = Galaxy, 2 = Cadbury, 3 = Freddo, 4 = Snickers, 5 = Twix" << endl;
- cout << "6 = KitKat, 7 = M&M's, 8 = Wispa, 9 = Marsbar, 10 = Double Decker" << endl;
- cin >> food;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number between 1 and 10" << endl;
- cin >> food;
- }
- if ( food<= 10 && food > 0){
- cout << "You chose a " << crisps[food-1] << " that was £" << priceChoc[food-1] << endl;
- finalChoice[arrayCounter] = crisps[food-1];
- arrayCounter++;
- bal = money - priceChoc[food-1];
- cout << "Your new balance is £" << bal << endl;
- flag1 = false;
- flag2 = false;
- }else{
- cout << "Error, please choose a number between 1 and 10" << endl;
- }
- }
- break;
- // Crisp
- case 3:
- while (flag1 == true){
- cout << "Please choose a flavour of Crisp by entering the number." << endl;
- cout << "1 = Cheese & Onion, 2 = Prawn Cocktail, 3 = Ready Salted" << endl;
- cout << "4 = Salt & Vinegar, 5 = Flaming Hot, 6 = Sweet Chilli" << endl;
- cout << "7 = BBQ, 8 = Nacho Cheese, 9 = Pickeled Onion, 10 = Paprika" << endl;
- cin >> food;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number between 1 and 10" << endl;
- cin >> food;
- }
- if ( food<= 10 && food > 0 ){
- cout << "You chose " << choc[food-1] << " flavour crisps that was £" << priceC[food-1] << endl;
- finalChoice[arrayCounter] = choc[food-1];
- arrayCounter++;
- bal = money - priceC[food-1];
- cout << "Your new balance is £" << bal << endl;
- flag1 = false;
- flag2 = false;
- }else{
- cout << "Error, please choose a number between 1 and 10" << endl;
- }
- }
- break;
- // if the number is not within the parameter
- default:
- cout << "Error, please chose a number between 1 and 3" << endl;
- break;
- }
- // if (bal < 1){
- // while (money < 1){
- //
- // money1 = money;
- // cout << "Sorry you dont have enough money for a snack" << endl;
- // cout << "Your balance is " << money << endl;
- // cout << "You need atleast £1 to buy a snack" << endl;
- // cout << "Please insert some more money" << endl;
- // cin >> dolla;
- // while (cin.fail()){
- // cin.clear();
- // cin.ignore();
- // cout << "Please enter a number" << endl;
- // cin >> dolla;
- // }
- // money = money1+dolla;
- // }
- // }
- //
- // return 0;
- // }
- money = bal;
- cout << "Would you like another snack?" << endl;
- cout << "Y or N" << endl;
- cin >> answer;
- if (toupper(answer) == 'Y' && money >0.55) {
- // toupper is a very useful as it means the user doesn't have to enter a uppercase letter, the program will still work
- flag3 = true;
- }
- else if (toupper(answer) == 'Y' && money < 1) {
- while (money < 1){
- money1 = money;
- cout << "Sorry you dont have enough money for a snack" << endl;
- cout << "Your balance is " << money << endl;
- cout << "You need atleast £1 to buy a snack" << endl;
- cout << "Please insert some more money" << endl;
- cin >> dolla;
- while (cin.fail()){
- cin.clear();
- cin.ignore();
- cout << "Please enter a number" << endl;
- cin >> dolla;
- }
- money = money1+dolla;
- }
- flag3 = true;
- }
- else if (toupper(answer) == 'N') {
- flag3 = false;
- cout << "Your change is £" << bal << endl;
- cout << "Enjoy your ";
- for (int x = 0; x<50; x++){
- if (finalChoice[x]!=""){
- if(arrayCounter == 1)
- {
- cout << finalChoice[x] << "";
- if (choice == 3){
- cout << " crisps";
- }
- }
- else
- {
- cout << finalChoice[x];
- if(finalChoice[x+2] != "")
- {
- cout << ", ";
- }
- else if(finalChoice[x+1] != "")
- {
- cout << " and ";
- }
- }
- }
- }cout << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment