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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <string>
using namespace std;
enum { plain_egg, bacon_and_egg, muffin, french_toast, fruit_basket, cereal, coffe, tea };
struct Item
{
string name;
int count = 0;
double price;
Item(string n, double p) : name(n), price(p)
{}
};
void showMenu(Item*, const int);
string checkMostPurchasedItem(Item*, const int);
void pause();
int main()
{
const int MAX_ITEMS = 8;
const int MAX_PURCHASES = 10;
Item items[MAX_ITEMS] = { {"Plain egg", 1.45},
{"Bacon and egg", 2.45},
{"Muffin", 0.99},
{"French Toast", 1.99},
{"Fruit basket", 2.49},
{"Cereal", 0.69},
{"Coffee", 0.50},
{"Tea", 0.75} };
int purchasedItems[MAX_PURCHASES] = { -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1 };
double totalPrice = 0;
cout << "\n You can buy up to " << MAX_PURCHASES << " items. Enter \"0\" when you want to cash out";
for (int count = 0; count < MAX_PURCHASES; count ++)
{
int choice;
do
{
showMenu(items, MAX_ITEMS);
cin >> choice;
choice--;
if (choice == -1)
break;
cout << "\n Incorrect input! Try again..";
for (int index = plain_egg; index <= tea; index ++)
{
if (choice == index)
{
purchasedItems[count] = index;
totalPrice += items[index].price;
items[index].count++;
break;
}
}
}
while (choice < -1 || choice > 7);
if (choice != -1)
cout << "\t\t\t Item bought!";
else
break;
}
cout << "\n\n You have purchased the following items: \n";
for (int count = 0; count < MAX_PURCHASES; count ++)
{
if (purchasedItems[count] != -1)
{
cout << "\n_ " << items[purchasedItems[count]].name;
}
else
break;
}
cout << checkMostPurchasedItem(items, MAX_ITEMS);
cout << "\n\n Total is: $" << totalPrice;
cout.precision(3);
cout << "\n Total with taxes is $" << totalPrice + (totalPrice *.07)
<< "\n Thank you. Come again.\n";
pause();
return 0;
}
void showMenu(Item item[], const int MAX)
{
cout << "\n\n ___MENU___\n";
for (int count = 0; count < MAX; count ++)
{
cout << "\n (" << count + 1 << ")_" << item[count].name << " ";
for (int index = item[count].name.size(); index < 30; index ++)
cout << ".";
cout << " $" << item[count].price;
}
cout << "\n\n Choice: ";
return;
}
string checkMostPurchasedItem(Item item[], const int MAX)
{
int highest = 0;
string highestName;
for (int count = 0; count < MAX; count ++)
{
if (item[count].count > highest)
{
highest = item[count].count;
highestName = item[count].name;
}
}
if (highest != 0)
{
string temp = "\n\n\n The Item that was the most bought was ";
temp += highestName;
temp += ",\n you bought it ";
temp.append(to_string(highest));
temp += " times!";
return temp;
}
else
return ("");
}
void pause()
{
cout << "\n\n\n Press ENTER to exit...";
cin.sync();
cin.get();
cout << "\n\n\n\n";
return;
}
| |