//Sam Partovi CS1A Second Practicum /******************************************************************************* * SIMULATE VENDING MACHINE * * This program simulates a vending machine with specified drinks, with given * costs and quantity of drinks stored. The program displays a menu of available * drinks for selection. -------------------------------------------------------------------------------- *INPUT * SIZE : Number of drinks available * name : Drink name * cost : Cost ($) of drink * supply : Drink quantity in machine * choice : Selected menu option * moneyInserted : Money ($) paid to machine * *OUTPUT * machineEarnings : Money ($) earned by machine * Menu of available drinks. *******************************************************************************/ #include <iostream> #include <iomanip> #include <string> using namespace std; const int SIZE = 5; //INPUT - Number of drinks available struct Drinks { string name; //INPUT - Drink name float cost; //INPUT - Cost ($) of drink int supply; //INPUT - Drink quantity in machine }; //Function prototype for DisplayMenu void DisplayMenu(Drinks drink[SIZE], int choice); int main() { //Assign drink information to each drink Drinks drink[SIZE] = { {"Cola", 0.75, 20}, {"Root Beer", 0.75, 20}, {"Lemon-Lime", 0.75, 20}, {"Grape Soda", 0.80, 20}, {"Cream Soda", 0.80, 20} }; int choice; //INPUT - Selected menu option float moneyInserted; //INPUT - Money ($) paid to machine float machineEarnings; //OUTPUT - Money ($) earned by machine moneyInserted = 0; machineEarnings = 0; do { //Call DisplayMenu function DisplayMenu(drink, choice); //Prompt for drink selection cin >> choice; //Prompt for moneyInserted if drink is available if (choice != 99 && drink[choice - 1].supply > 0) { cout << "\nInsert money: "; cin >> moneyInserted; } //Display error if drink is out of stock if (drink[choice - 1].supply <= 0 && choice != 99) { cout << "\nError: Item is out of stock."; } //Dispense drink if money has been inserted if (moneyInserted > 0) { if(drink[choice - 1].cost <= moneyInserted) { cout << "\nDispensing " << drink[choice - 1].name << "..." << endl; //Display change to be returned cout << fixed << setprecision(2) << "Your change is $" << moneyInserted - drink[choice - 1].cost << ".\n" << endl; //Add cost of chosen drink to total machine earnings machineEarnings += drink[choice - 1].cost; //Decrement drink quantity in machine drink[choice - 1].supply--; } //Display error if moneyInserted is not enough for chosen drink else cout << "\nError: Not enough money inserted."; } //Display error if invalid amount of money inserted else cout << "\nError: Must insert more than $0."; } while (choice != 99); //Terminate loop if 99 is entered //Display total machine earnings cout << fixed << setprecision(2) << "\nMachine earned $" << machineEarnings; return 0; } /******************************************************************************* *Function definition for DisplayMenu: * This function displays a menu of available drinks and allows for drink * selection. ******************************************************************************/ void DisplayMenu(Drinks drink[SIZE], int choice) { cout << "Menu:" << endl; //Display each drink in a numbered list for(int i = 0; i < SIZE; i++) { cout << i + 1 << ". " << drink[i].name << endl; } cout << "Enter 99 to Quit" << endl; }
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
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