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
|
#include <iostream>
// Name and price of a menu item
struct Item {
std::string name;
int price;
};
void miniShop(char &);
void processFoodCategory(Item[], std::string *, int &, int *);
void sale(char, std::string *, int &, int *);
Item drinks[] = {
{ "Coffee", 15 },
{ "Soft Drinks", 25 },
{ "Alcohol", 65},
{ "", 0} // indicates end
};
Item foods[] = {
{ "French Fries", 125 },
{ "Sizzling", 150 },
{ "Chicken Curry", 175},
{ "", 0} // indicates end
};
Item desserts[] = {
{ "Ice Cream", 225 },
{ "Fruit Salad", 250 },
{ "Bake Mac", 275},
{ "", 0} // indicates end
};
int
main()
{
char selection = '\0';
std::string items = " ";
int money = 500;
int price = 0;
miniShop(selection);
switch (selection) {
case '1':
processFoodCategory(drinks, &items, price, &money);
break;
case '2':
processFoodCategory(foods, &items, price, &money);
break;
case '3':
processFoodCategory(desserts, &items, price, &money);
break;
}
sale(selection, &items, price, &money);
std::cout << "Your money is " << money;
}
void
miniShop(char &select)
{
do {
std::cout << "\t\t\tWELCOME TO MY MINISHOP" << std::endl
<< "\t\t\t[1]Drinks" << std::endl
<< "\t\t\t[2]Foods" << std::endl
<< "\t\t\t[3]Desserts" << std::endl;
std::cin >> select;
} while (select != '1' && select != '2' && select != '3');
}
// Given an array of items, print a menu of them and prompt for
// a selection. If there is enough money then set the price and name.
void processFoodCategory(Item items[],
std::string * i_ptr,
int &price,
int *m_ptr)
{
int select = 0;
int i;
do {
for (i=0; items[i].name.size(); ++i) {
std::cout << "[" << i+1 << "] "
<< items[i].name << " = $" << items[i].price << '\n';
}
std::cin >> select;
} while (select < 1 || select > i);
// decrement select so it can be used as an index.
--select;
if (items[select].price <= *m_ptr) {
price = items[select].price;
*i_ptr = items[select].name;
} else {
std::cout << "You dont have enough money thank you\n";
}
}
void
sale(char selection, std::string * i_ptr, int &price, int *m_ptr)
{
do {
std::
cout << "Are you sure you want to buy " << *i_ptr << "? Y/N " <<
std::endl;
std::cin >> selection;
} while (selection != 'y' && selection != 'Y' && selection != 'N'
&& selection != 'n');
switch (selection) {
case 'y':
case 'Y':
*m_ptr -= price;
break;
case 'n':
case 'N':
std::cout << "Bye";
break;
}
}
|