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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 10;
struct Drink {
string Name;
double Cost;
int numDrinks;
};
void fillMachine (Drink machine[], int &numitems);
void getChoice (Drink machine[], int numitems, int &i, int &choice);
void transaction (Drink machine[], int &numitems, int &i, int &choice);
int main()
{
int i = 0;
int numitems = 0;
int choice = 0;
Drink machine[SIZE];
fillMachine (machine, numitems);
getChoice (machine, numitems, i, choice);
transaction (machine, numitems, i, choice);
return 0;
}
void fillMachine (Drink machine[], int &numitems)
{
string mydrink;
// Get the drink name
cout << "Enter the first drink, Q to quit: ";
getline(cin, mydrink);
while (mydrink != "Q" && numitems < SIZE)
{
machine[numitems].Name = mydrink;
// Get the price
cout << "Enter the price: ";
cin >> machine[numitems].Cost;
// Get the quantity
cout << "Enter the quantity: ";
cin >> machine[numitems].numDrinks;
cin.ignore();
numitems++;
cout << "Enter the next drink, Q to quit: ";
getline(cin, mydrink);
}
}
void getChoice (Drink machine[], int numitems, int &i, int &choice)
{
cout << endl;
for (i=0;i<numitems;i++)
cout << (i+1) << ") " << machine[i].Name << setw(8) << setprecision (3) << machine[i].Cost << endl;
cout << (i+1) << ") Exit" << endl;
cout << endl << "Choose one: ";
cin >> choice;
if (choice > (numitems+1))
cout << "Bad choice." << endl;
cout << endl;
}
void transaction (Drink machine[], int &numitems, int &i, int &choice)
{
double money;
double change;
double total;
while (choice != i +1)
{
i = choice - 1;
if (machine[i].numDrinks < 1)
cout << "This item is not in stock." << endl;
if (machine[i].numDrinks > 0 )
{
cout << "Enter an amount of money" << endl;
cin >> money;
while (money < machine[i].Cost)
{
cout << "That's not enough money" << endl;
cin >> money;
}
while (money > 1)
{
cout << "Enter at least " << machine[i].Cost << " and no more than 1 dollar." << endl;
cin >> money;
}
cout << endl << "Enjoy your beverage!" << endl << endl;
change = money - machine[i].Cost;
cout << "Change calculated: " << setprecision (3) << change << endl;
money = money - change;
total = money + total;
machine[i].numDrinks = machine[i].numDrinks - 1;
cout << "There are " << machine[i].numDrinks << " drinks of that type left" << endl;
}
getChoice (machine, numitems, i, choice);
}
cout << "Total earnings: $" << setprecision (3) << total << endl;
}
|