Write a C program that simulates the purchase of a single it
Solution
Answer:-
#include <iostream>
using namespace std;
struct Drink
{
// Public constructor / with default constructor
Drink(string n, double c, int co)
{
name = n;
cost = c;
count = co;
}
Drink()
{
name = \" \";
cost = 0;
count = 0;
}
const int DRINKS = 5; // Max number of drinks
// New array using structure constructor to fill in data for all the drinks, costs, and inventory
int data[DRINKS] = {Drink(\"Coke\", .75, 20),
Drink(\"Root Beet\", .75, 20),
Drink(\"Orange Soda\", .75, 20),
Drink(\"Grape Soda\", .75, 20),
Drink(\"Bottled Water\", 1.00, 20)};
// Local variables
string name; // Name of the drink ie: Coke
double cost; // Cost ie: $.75
int count; // Number of items in vending machine
// Function prototypes
int displayChoices();
double buyDrink(int c); // Choice as argument so it knows what drink to process
double inputMoney(int c); // Choice as argument so it knows what drink to process
};
// Code implmentation for the functions
int Drink::displayChoices()
{
int choice;
do
{
cout << \"This is a vending machine program.\ \"
<< \"1. Coke\ \"
<< \"2. Root Beer\ \"
<< \"3. Orange Soda\ \"
<< \"4. Grape Soda\ \"
<< \"5. Bottled Water\ \"
<< \"6. Quit the Program\ \"
<< \"Enter choice: \";
cin >> choice;
// Validate the choice
while (choice < 1 || choice > 6)
{
cout << \"Please select items 1-6\"
<< \"Re-enter: \";
cin >> choice;
return choice;
}
} while (choice != 6);
}
double buyDrink(int choice)
{
cout << \"You selected #\" << choice << \".\"
}
double inputMoney(int choice)
{
int money,
total;
cout << \"You selected #\" << choice << \".\"
<< \"Enter 1 to insert a quarter or 0 to quit.\ \";
cin >> money;
while (money != 0);
{
total += .25;
cout << \"Total change entered: \" << total << \".\"
<< \"Enter 1 to insert a quarter or 0 to quit.\ \";
cin >> money;
}
return money;
}

