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 104 105 106 107 108
|
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
const int NUM_DRINKS = 6;
struct Drink
{
string name;
double price;
int num;
};
Drink Beverage;
int GetChoice(Drink m[])
{
int choice;
cout << " Please pick a drink" << endl;
while(choice<1||choice>6){
cout << " Drink Options: Prices" << endl;
cout << " 1. Cola : .75cents " << endl;
cout << " 2. Root Beer : .75cents " << endl;
cout << " 3. Lemon Lime : .75cents " << endl;
cout << " 4. Grape Soda : .80cents " << endl;
cout << " 5. Cream Soda : .80cents " << endl;
cout << " 6. I'm Leaving BYE." << endl;
cin >> choice;
if (choice < 1 || choice > 6)
cout << "Please enter a number 1-6. "<<endl;
}
return choice;
}
void Transaction(Drink m[], int choice, double &earnings)
{
float moneyRecieved;
float change;
cout << " Please enter an amount no more then a dollar ";
cin >> moneyRecieved;
if (moneyRecieved < 0 || moneyRecieved > 1)
{
cout << " This vending machine only allows for change and bills up to a dollar and no less than 0 cents ";
}
if (moneyRecieved >= Beverage.price)
{
cout << "Thank You!! Your drink will be coming right out ";
}
{
change = moneyRecieved - m[choice - 1].price;
cout << "Here is your change " << change << " Thank you come agian ";
earnings = moneyRecieved - change;
}
}
int main()
{
int choice = 0;
double earnings = 0.0;
Drink machine[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 }
};
cout << fixed << showpoint << setprecision(2);
do
{
choice = GetChoice(machine);
if (choice != NUM_DRINKS)
Transaction(machine, choice, earnings);
} while (choice != NUM_DRINKS || choice != 6);
cout << "Total money earned: $" << earnings << endl;
return 0;
}
|