In C++: create an extra file (menu.cpp) for actual implementation and let menu.h only contain prototypes/declarations. The code works fine, just need help organizing/optimizing the code. Please read the code. Here are the 3 current files that run the program. main.cpp #include"menu.h" int main() { vector food; readMenuFromFile("menu.txt",options); assignPriceForEachMenuItem(priceForEachOption); cout<< "Welcome to our restaurant, choose what would you like to eat: "<< endl; for(unsigned int i = 0; i < options.size(); i++){ cout << options[i] << "\t" << "$" << priceForEachOption[i] << endl; } print_menu(); int choice = get_verify_choice(); while (choice >= 0){ if (choice == 0) take_order(); if(!check_for_exit()) { display_bill(); break; } if (choice == 1) break; } } menu.h #ifndef MENU_H #define MENU_H #include #include #include #include #include #include using std::vector; using std::array; using std::string; using std::cout; using std::cin; using std::endl; const array choices = {"Take Order","Exit"}; const array customer_category = {"Senior citizen","Preferred member","Educator","Veteran"}; double total = 0.0; double discount_rate = 0.0; double tax_rate = 0.089; vector options; vector priceForEachOption; void readMenuFromFile(string fileName,vector& options){ std::ifstream in(fileName); string item; while (std::getline(in, item)){ options.push_back(item); } } void assignPriceForEachMenuItem(vector& menuItemsprice){ menuItemsprice.push_back(4.99); menuItemsprice.push_back(1.50); menuItemsprice.push_back(4.20); menuItemsprice.push_back(4.10); menuItemsprice.push_back(5.34); menuItemsprice.push_back(8.99); menuItemsprice.push_back(1.30); menuItemsprice.push_back(9.50); menuItemsprice.push_back(7.25); menuItemsprice.push_back(2.50); } bool check_for_exit(){ // Ask if they want to leave and leave on "n" // Returns true if they want to exit, false if still want to continue string leave = "n"; // Leave flag - checked at end, "n" leaves loop cout << "\nDo you want to order something else? "; cin >> leave; if (leave[0] == 'n' || leave[0] == 'N'){ return false; } return true; } void print_menu(){ // prints out our main menu and the availble choices cout << "\t\t\tPlease select an option from the following choices:\n\n"; for (int i = 0; i <= choices.size() - 1; i++){ cout << "\t[" << i << "] " << choices[i] << "\n"; } cout << "Enter selection [ ]\b\b"; } std::string take_order(){ std::string food; int n; cout << "Enter name of the plate: "; cin >> food; cout << "Enter the quantity: "; cin >> n; cout << "Your order is " << n << " "<< food << endl; // calculate the total based on the order bool found = false; int i; for ( i = 0; i < options.size(); i++){ options[i].erase(options[i].find_last_not_of("\n\r") + 1); if (options[i].compare(food) == 0){ found = true; break; } } if (!found){ std::cout << "Plate not found, please re-run the program"; exit(0); } float price = priceForEachOption[i]; total += price * n; return food; } int customer_category_value(){ char ch; int val; // ask the user if they are applicable for discount cout << "\nAre you applicable for discount ? [y / n] = "; cin >> ch; // if no then return zero if(ch == 'n' || ch == 'N') return 0; // if yes, then ask which category they belong to for (int i = 0; i <= customer_category.size() - 1; i++){ // display the category name with value cout << "\t[" << (i + 1) << "] " << customer_category[i] << "\n"; } cout << "Enter selection [ ]\b\b"; cin >> val; // return the category value return val; } void display_bill(){ // print the total bill value cout << "\n\nThe subtotal amount is $" << total; int choice = customer_category_value(); // based on the customer category, calculate the discount if(choice == 1) discount_rate = 0.05; if(choice == 2) discount_rate = 0.08; if(choice == 3) discount_rate = 0.025; if(choice == 4) discount_rate = 0.04; double net = total - (total * discount_rate); // deduct the tax from final amount net += net * tax_rate; // display the net amount cout << "The total bill amount is $" << net < cout<<"Give payment"< double payment; cin>>payment; if(net==payment){ cout<<"no change"< } else if(payment<=0){ cout<<"Insufficient payment: You have to give $"< } else{ cout<<"Your change is $"<< (payment-net) << endl; } std::ofstream myfile ("reciept.txt"); if (myfile.is_open()){ myfile << "Total Cost:$" << total << endl; myfile << "Discount:$" << (total * discount_rate) << endl; myfile << "Tax:$" << net * tax_rate << endl; myfile << "Total bill:$" << net << endl; myfile.close(); } else cout << "Unable to open file"; } int get_verify_choice(){ int temp; cin >> temp; // if temp is invalid while (temp >= choices.size()){ cout << "Invalid choice, please enter choice again\n"; cout << "Enter selection [ ]\b\b"; cin >> temp; } // if temp is valid return it return temp; } #endif menu.txt sushi coke cookie salad chicken pasta brownie beef pizza pie
Computer Science
In C++: create an extra file (menu.cpp) for actual implementation and let menu.h only contain prototypes/declarations. The code works fine, just need help organizing/optimizing the code. Please read the code.
Here are the 3 current files that run the program.
main.cpp
#include"menu.h"
int main()
{
readMenuFromFile("menu.txt",options);
assignPriceForEachMenuItem(priceForEachOption);
cout<< "Welcome to our restaurant, choose what would you like to eat: "<< endl;
for(unsigned int i = 0; i < options.size(); i++){
cout << options[i] << "\t" << "$" << priceForEachOption[i] << endl;
}
print_menu();
int choice = get_verify_choice();
while (choice >= 0){
if (choice == 0)
take_order();
if(!check_for_exit()) {
display_bill();
break;
}
if (choice == 1)
break;
}
}
menu.h
#ifndef MENU_H
#define MENU_H
#include
#include
#include
#include
#include
#include
using std::vector;
using std::array;
using std::string;
using std::cout;
using std::cin;
using std::endl;
const array choices = {"Take Order","Exit"};
const array customer_category = {"Senior citizen","Preferred member","Educator","Veteran"};
double total = 0.0;
double discount_rate = 0.0;
double tax_rate = 0.089;
vector options;
vector priceForEachOption;
void readMenuFromFile(string fileName,vector& options){
std::ifstream in(fileName);
string item;
while (std::getline(in, item)){
options.push_back(item);
}
}
void assignPriceForEachMenuItem(vector& menuItemsprice){
menuItemsprice.push_back(4.99);
menuItemsprice.push_back(1.50);
menuItemsprice.push_back(4.20);
menuItemsprice.push_back(4.10);
menuItemsprice.push_back(5.34);
menuItemsprice.push_back(8.99);
menuItemsprice.push_back(1.30);
menuItemsprice.push_back(9.50);
menuItemsprice.push_back(7.25);
menuItemsprice.push_back(2.50);
}
bool check_for_exit(){
// Ask if they want to leave and leave on "n"
// Returns true if they want to exit, false if still want to continue
string leave = "n"; // Leave flag - checked at end, "n" leaves loop
cout << "\nDo you want to order something else? ";
cin >> leave;
if (leave[0] == 'n' || leave[0] == 'N'){
return false;
}
return true;
}
void print_menu(){
// prints out our main menu and the availble choices
cout << "\t\t\tPlease select an option from the following choices:\n\n";
for (int i = 0; i <= choices.size() - 1; i++){
cout << "\t[" << i << "] " << choices[i] << "\n";
}
cout << "Enter selection [ ]\b\b";
}
std::string take_order(){
std::string food;
int n;
cout << "Enter name of the plate: ";
cin >> food;
cout << "Enter the quantity: ";
cin >> n;
cout << "Your order is " << n << " "<< food << endl;
// calculate the total based on the order
bool found = false;
int i;
for ( i = 0; i < options.size(); i++){
options[i].erase(options[i].find_last_not_of("\n\r") + 1);
if (options[i].compare(food) == 0){
found = true;
break;
}
}
if (!found){
std::cout << "Plate not found, please re-run the program";
exit(0);
}
float price = priceForEachOption[i];
total += price * n;
return food;
}
int customer_category_value(){
char ch;
int val;
// ask the user if they are applicable for discount
cout << "\nAre you applicable for discount ? [y / n] = ";
cin >> ch;
// if no then return zero
if(ch == 'n' || ch == 'N')
return 0;
// if yes, then ask which category they belong to
for (int i = 0; i <= customer_category.size() - 1; i++){
// display the category name with value
cout << "\t[" << (i + 1) << "] " << customer_category[i] << "\n";
}
cout << "Enter selection [ ]\b\b";
cin >> val;
// return the category value
return val;
}
void display_bill(){
// print the total bill value
cout << "\n\nThe subtotal amount is $" << total;
int choice = customer_category_value();
// based on the customer category, calculate the discount
if(choice == 1)
discount_rate = 0.05;
if(choice == 2)
discount_rate = 0.08;
if(choice == 3)
discount_rate = 0.025;
if(choice == 4)
discount_rate = 0.04;
double net = total - (total * discount_rate);
// deduct the tax from final amount
net += net * tax_rate;
// display the net amount
cout << "The total bill amount is $" << net <
cout<<"Give payment"<
double payment;
cin>>payment;
if(net==payment){
cout<<"no change"<
}
else if(payment<=0){
cout<<"Insufficient payment: You have to give $"<
}
else{
cout<<"Your change is $"<< (payment-net) << endl;
}
std::ofstream myfile ("reciept.txt");
if (myfile.is_open()){
myfile << "Total Cost:$" << total << endl;
myfile << "Discount:$" << (total * discount_rate) << endl;
myfile << "Tax:$" << net * tax_rate << endl;
myfile << "Total bill:$" << net << endl;
myfile.close();
}
else cout << "Unable to open file";
}
int get_verify_choice(){
int temp;
cin >> temp;
// if temp is invalid
while (temp >= choices.size()){
cout << "Invalid choice, please enter choice again\n";
cout << "Enter selection [ ]\b\b";
cin >> temp;
}
// if temp is valid return it
return temp;
}
#endif
menu.txt
sushi
coke
cookie
salad
chicken
pasta
brownie
beef
pizza
pie
Step by step
Solved in 2 steps