Question
#include #include #include #include using namespace std; int getData(string *&items, double *&prices){ ifstream in(Ch9_Ex5Data.txt); if(in.is_open()){ int size; string temp; in >> size; items = new
#include
using namespace std;
int getData(string *&items, double *&prices){ ifstream in("Ch9_Ex5Data.txt"); if(in.is_open()){ int size; string temp; in >> size; items = new string[size]; prices = new double[size]; getline(in, temp); for(int i = 0; i < size; ++i){ getline(in, items[i]); getline(in, temp); prices[i] = atof(temp.c_str()); } return size; } else{ cout << "Error. Can not open Ch9_Ex5Data.txt. Exiting..." << endl; exit(-1); } }
void showMenu(string *items, double *prices, int size){ cout << "Welcome to Johnny's Restaurant" << endl; cout << "Menu:" << endl; for(int i = 0; i < size; ++i){ cout << (i + 1) << ". " << items[i] << ": " << prices[i] << endl; } cout << (size + 1) << ". Exit" << endl; }
int getChoice(int size){ int choice; cout << "Enter your choice: "; cin >> choice; while(choice < 1 || choice > size + 1){ cout << "Invalid choice. Try again: "; cin >> choice; } return choice; }
void printCheck(string *items, double *prices, int *orders, int size){ double total = 0; for(int i = 0; i < size; ++i){ if(orders[i] > 0){ total += prices[i] * orders[i]; cout << orders[i] << " " << items[i] << " :$" << setprecision(2) << fixed << prices[i] << endl; } } cout << "Tax: $" << setprecision(2) << fixed << total * 0.05 << endl; cout << "Amount Due: $" << setprecision(2) << fixed << total * 1.05 << endl; }
int main(){ string *items = NULL; double *prices = NULL; int *orders = NULL; int size = getData(items, prices); orders = new int[size]; for(int i = 0; i < size; ++i){ orders[i] = 0; } int choice; showMenu(items, prices, size); while(true){ choice = getChoice(size); if(choice == size + 1) break; else orders[choice - 1]++; } printCheck(items, prices, orders, size); return 0; }
Redo Exercise 4 so that the customer can select multiple items of a particular type. A sample output in this case is:
Welcome to Johnny's Resturant ----Today's Menu---- 1: Plain Egg $1.45 2: Bacon and Egg $2.45 3: Muffin $0.99 4: French Toast $1.99 5: Fruit Basket $2.49 6: Cereal $0.69 7: Coffee $0.50 8: Tea $0.75 You can make up to 8 different selections Do you want to make selection Y/y (Yes), N/n (No): Y Enter item number: 1 How many orders: 3 Select another item Y/y (Yes), N/n (No): Y Enter item number: 8 How many orders: 1 Select another item Y/y (Yes), N/n (No): N Welcome to Johnny's Resturant 3 Plain Egg $4.35 1 Tea $0.75 Tax $0.26 Amount Due $5.35
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started