1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct Drink
{
string drinkName;
double drinkCost;
int numOfDrinks;
};
int getChoice(Drink D[]); // Get D = drink of choice
void transaction(Drink D[], int choice, double& price);
int main ()
{
Drink D[5] ={{"Cola ",.75,1},{"Root Beer",.75,20},{"Lemon-Lime",.75,20},{"Grape Soda",.80,20},{"Cream Soda",.80,20}};
double moneyMachineEarned = 0.0;
int choice;
while (true)
{
choice = getChoice (D);
if (choice == 6) break;
if (choice > 6 or choice < 1)
{
cout << endl;
cout << "Invalid selection! Please try again." << endl << endl;
}
else
{
transaction (D, choice-1, moneyMachineEarned);
}
}
cout << endl;
cout << "The total amount of money the machined earned is " << moneyMachineEarned << endl << endl;
system ("pause");
return 0;
}
int getChoice (Drink D[]) // Get D = drink of choice
{
int choice;
cout << endl << endl;
cout << "************** List of Drink Choices ***********************" << endl;
cout << "Drink Name Cost Number of Drinks in machine " << endl;
cout << "************************************************************" << endl;
for (int index = 0; index < 5; index++)
// Display drink choices, cost per drink, and number of drinks available
cout << (index + 1) << ". "
<< D [index].drinkName << "\t\t"
<< D [index].drinkCost << "\t" << setprecision(2) << fixed
<< D [index].numOfDrinks << endl;
cout << (6) << ". " << "Quit" << endl << endl;
cout << "Please enter the number for your drink of choice, or enter (6) to quit: ";
cin >> choice;
return choice;
}
void transaction (Drink D[], int choice, double& price)
{
if (D[choice].numOfDrinks == 0)
{
cout << endl;
cout << "That drink is sold-out! Please select again, or enter (6) to quit: ";
return;
}
double amount;
cout << endl;
cout << "Please enter the amount of money deposited for your drink selection: $ ";
cin >> amount;
cout << endl;
while (amount < 0 || amount > 1)
{
cout << "Amount entered was invalid. Please enter an amount between $0.00 and $1.00: $ ";
cin >> amount;
cout << endl;
}
while (amount < D[choice].drinkCost)
{
cout << "Amount entered was insufficient. Please enter an amount to purchase drink: $ ";
cin >> amount;
cout << endl;
}
if (amount >= D[choice].drinkCost)
cout << "Thank you, your change is: $ " << (amount - D [choice].drinkCost) << endl;
D [choice].numOfDrinks--;
price = price + D [choice].drinkCost;
}
|