// Elaine Torrezn #13 CS1A Chapter 11 – Drink Machine Simulator
/********************************************************************************************
*
* DRINK MACHINE SIMULATOR
*
* ------------------------------------------------------------------------------------------
* This program simulates a soft drink vending machine. A structure is used to store the
* following data for each drink:
*
* - Drink name
* - Drink cost
* - Number of drinks currently in the machine
*
* The program creates an array of five structures and initializes them with the following
* data (all starting with 20 drinks each):
*
* Cola $0.75
* Root Beer $0.75
* Lemon-Lime $0.75
* Grape Soda $0.80
* Cream Soda $0.80
*
* The program then enters a loop that:
* 1) Displays the list of drinks and a "Quit" option.
* 2) Allows the user to select a drink or quit the program.
* 3) If a drink is selected:
* - Asks the user to insert an amount of money.
* - Validates the amount (0.00 – 1.00, not negative).
* - If the drink is sold out, displays a message.
* - If not enough money is inserted, displays a message and returns all money.
* - Otherwise, dispenses the drink, shows the change, subtracts one drink from
* the machine, and adds the drink cost to the machine's total earnings.
* 4) Repeats until the user chooses to quit.
*
* When the user chooses to quit, the program displays the total amount of money the machine
* has earned.
* ------------------------------------------------------------------------------------------
*
* INPUT
* choice : User's drink menu choice (1–5 for drinks, 0 to quit)
* money : Amount of money inserted (0.00 – 1.00)
*
* OUTPUT
* For each purchase:
* - Messages about sold-out or insufficient funds (if applicable)
* - Amount of change returned
* At the end:
* - Total amount of money earned by the machine
*
* INPUT VALIDATION
* - Drink selection must be 0–5.
* - When the user enters an amount of money, do not accept negative values or values
* greater than $1.00.
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//------------------------------------------------------------
// Structure for a drink type in the machine
//------------------------------------------------------------
struct Drink {
string name; // Drink name
double cost; // Drink cost
int count; // Number of drinks in machine
};
// Function prototype
void showMenu(const Drink drinks[], int size);
int main()
{
const int NUM_DRINKS = 5;
// Initialize the drink machine
Drink drinks[NUM_DRINKS] = {
{"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; // User's menu choice
double money; // Money inserted
double change; // Change to return
double totalEarnings = 0; // Total amount earned by the machine
cout << fixed << setprecision(2);
do {
showMenu(drinks, NUM_DRINKS);
cout << "Enter your choice (1-5, or 0 to quit): ";
cin >> choice;
// Validate menu choice
while (choice < 0 || choice > NUM_DRINKS) {
cout << " **ERROR** Please enter a value from 0 to " << NUM_DRINKS << ": ";
cin >> choice;
}
if (choice == 0) {
// Quit
break;
}
int index = choice - 1; // Array index (0–4)
// Check if drink is sold out
if (drinks[index].count == 0) {
cout << "\nSorry, " << drinks[index].name << " is SOLD OUT.\n\n";
continue; // Skip to next loop iteration
}
// Ask user for money
cout << "Insert money for " << drinks[index].name
<< " (cost $" << drinks[index].cost << ", max $1.00): ";
cin >> money;
// Validate money amount (0.00 – 1.00, not negative)
while (money < 0.0 || money > 1.0) {
cout << " **ERROR** Amount must be between $0.00 and $1.00. Re-enter: ";
cin >> money;
}
// Check if enough money was inserted
if (money < drinks[index].cost) {
cout << "\nNot enough money inserted. Returning $" << money << ".\n\n";
} else {
// Successful purchase
change = money - drinks[index].cost;
drinks[index].count--; // One less drink in the machine
totalEarnings += drinks[index].cost;
cout << "\nDispensing " << drinks[index].name << "...\n";
cout << "Your change is $" << change << ".\n\n";
}
} while (true);
// Display total money earned
cout << "\nTotal money earned by the machine: $" << totalEarnings << endl;
return 0;
}
//------------------------------------------------------------
// Displays the drink menu
//------------------------------------------------------------
void showMenu(const Drink drinks[], int size)
{
cout << "\nDRINK MACHINE MENU\n";
cout << "-----------------------------------------\n";
for (int i = 0; i < size; ++i) {
cout << (i + 1) << ". " << left << setw(12) << drinks[i].name
<< " $" << drinks[i].cost
<< " (" << drinks[i].count << " left)\n";
}
cout << "0. Quit\n\n";
}