Your C++ code appears to be a simple program for taking food orders from a menu, calculating prices, applying member discounts, and displaying an order summary. The code structure looks fine, and it seems to achieve the intended functionality. However, I noticed a couple of things you might want to consider:
Member Discounts: It looks like you're applying a flat discount of 20% if the customer is a member. You might want to check if this is the desired behavior, as typically, discounts are applied to the individual items rather than the total order.
Quantity Handling: You're using totalAllQuantity to keep track of the quantity of items ordered. However, you might want to keep track of the quantity of each item separately so that you can calculate the total price for each item and then sum them up for the order total.
Price Calculation: Currently, you're using a fixed price for each menu item. It's common to have a more dynamic pricing structure, where the prices are read from a data source (like a file or database) to allow for easy changes in pricing without modifying the code.
Indentation and Formatting: While the code is functional, it's a good practice to follow consistent code formatting and indentation to make the code more readable and maintainable.
Input Validation: You might want to consider adding input validation to ensure that the user enters valid choices, quantities, and responses.
Handling Currency Values: When dealing with currency values, it's often better to use integer types (like int or long) to represent amounts in cents to avoid floating-point precision issues.
Here is corrected code:
#include <iostream>
#include <iomanip>
using namespace std;
struct MenuItem {
string name;
float price;
};
MenuItem menuItems[] = {
{"Ebi tempura udon", 15.00},
{"Japanese curry with rice", 20.00},
{"Sukiyaki", 18.00},
{"Chicken dumplings", 12.00},
{"Chinese Fried rice", 8.00},
{"Mee Tarik with sliced beef", 15.80},
{"Rose tteokbokki", 15.00},
{"Cheese Ramyeon", 10.00},
{"Rabokki", 17.00}
};
float calculatePrice(int itemIndex, int quantity) {
if (itemIndex >= 0 && itemIndex < sizeof(menuItems) / sizeof(menuItems[0])) {
return menuItems[itemIndex].price * quantity;
}
return 0.0;
}
float applyMemberDiscount(float totalPrice, char answer) {
if (answer == 'y' || answer == 'Y') {
return totalPrice * 0.8; // 20% discount for members
}
return totalPrice;
}
int main() {
char answer, menu, member;
float totalPrice, sumTotal = 0.0;
int itemIndex, quantity;
cout << "*** Welcome to irenic cafe ^^ ***";
do {
cout << "\nDo you want to start an order? (y/n): ";
cin >> answer;
if (answer == 'n' || answer == 'N') {
break;
}
cout << "\nJ: Japanese cuisine, C: Chinese cuisine, K: Korean cuisine";
cout << "\nPlease take a look at our menu (J/C/K): ";
cin >> menu;
if (menu == 'J' || menu == 'C' || menu == 'K') {
cout << "\nYour selected dish: " << menuItems[itemIndex].name << endl;
cout << "Enter the quantity: ";
cin >> quantity;
} else {
cout << "\nInvalid choice. Please select a valid cuisine from the menu (J/C/K)." << endl;
continue;
}
cout << "Are you a member? (y/n): ";
cin >> member;
totalPrice = calculatePrice(itemIndex, quantity);
totalPrice = applyMemberDiscount(totalPrice, member);
sumTotal += totalPrice;
cout << "Do you want to add more order? (y/n): ";
cin >> answer;
} while (answer != 'n' && answer != 'N');
cout << "\nYour order summary:" << endl;
cout << "-------------------" << endl;
cout << "Total price: RM " << fixed << setprecision(2) << sumTotal << endl;
cout << "Thank you for ordering with us!" << endl;
return 0;
}