
Daily C++ Exercises: Lesson 3
By: a guest on
Aug 3rd, 2012 | syntax:
C++ | size: 2.74 KB | hits: 18 | expires: Never
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
using namespace std;
struct menuItemType {
string name;
double price;
};
menuItemType* GetData();
void ShowMenu(menuItemType* menuArrP);
void PrintCheck( vector<int>& choices, menuItemType* menuArrP );
int main() {
menuItemType* menuList = GetData();
vector<int> selections = ShowMenu(menuList);
int input;
vector<int> selections;
while ( cin >> input ) {
cout << "Enter the number for the item you'd like, then Enter. Press '0' and Enter to end your selections: ";
cin >> input;
if (input >= 1 && input <= 7) {
selections.push_back(input);
}
else if (input != 0) {
cout << "Invalid seleciton. Please try again: ";
}
}
PrintCheck( selections, menuList);
system("PAUSE");
return 0;
}
menuItemType* GetData() {
menuItemType* mip = new menuItemType[7];
menuItemType plainEgg;
plainEgg.name = "1. Plain Egg";
plainEgg.price = 1.45;
mip[0] = plainEgg;
menuItemType baconEgg;
baconEgg.name = "2. Bacon and Egg";
baconEgg.price = 2.45;
mip[1] = baconEgg;
menuItemType muffin;
muffin.name = "3. Muffin";
muffin.price = 0.99;
mip[2] = muffin;
menuItemType frenchToast;
frenchToast.name = "4. French Toast";
frenchToast.price = 1.99;
mip[3] = frenchToast;
menuItemType fruitBasket;
fruitBasket.name = "5. Fruit Basket";
fruitBasket.price = 2.49;
mip[4] = fruitBasket;
menuItemType cereal;
cereal.name = "6. Cereal";
cereal.price = 0.50;
mip[5] = cereal;
menuItemType tea;
tea.name = "7. Tea";
tea.price = 0.75;
mip[6] = tea;
return mip;
}
void ShowMenu(menuItemType* menuArrP) {
int menuSize = 7;
for (int i = 0; i < menuSize; ++i) {
cout.precision(2);
cout.width(30);
cout << setfill('.') << internal << fixed << left <<
menuArrP[i].name << right << menuArrP[i].price << endl;
}
}
void PrintCheck (vector<int>& choices, menuItemType* menuArrP) {
const double tip = 0.05;
double bill = 0;
for (int i = 0; i < choices.size(); ++i) {
bill += menuArrP[ choices[i]-1 ].price;
}
bill += bill * tip;
cout.precision(2);
cout << "Your total, with tax, is: $" << bill << endl;
}