The code you've provided is mostly correct, but it's missing an #include directive for the limits header, which is required for numeric_limits<streamsize>::max(). You should also add an #include for the <cctype> or <ctype.h> header for the toupper function. After including these headers, the code should compile and work correctly.
Here's the updated code with the necessary includes:
#include <iostream>
#include <vector>
#include <iomanip>
#include <limits> // Include for numeric_limits
#include <cctype> // Include for toupper
using namespace std;
// Define the menu items and their prices as constants
const string MENU_ITEMS[5] = {"Espresso", "Americano", "Cappuccino", "Latte", "Mocha"};
const double MENU_PRICES[5] = {2.5, 3, 3.5, 4, 4.5};
// Function to display the menu
void displayMenu() {
cout << "Welcome to our Coffee Shop!" << endl;
cout << "---------------------------" << endl;
cout << "Menu:" << endl;
for (int i = 0; i < 5; i++) {
cout << i + 1 << ". " << MENU_ITEMS[i] << " - $" << MENU_PRICES[i] << endl;
}
}
// Function to process the user's choice
int processChoice() {
int choice;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore any remaining input
return choice;
}
// Function to validate the order size
bool isValidSize(char size) {
return (size == 'S' || size == 'M' || size == 'L');
}
// Function to calculate the price based on the item and size
double calculatePrice(int item, char size) {
double price = MENU_PRICES[item - 1];
if (size == 'M') {
price += 1; // Medium size costs $1 extra
} else if (size == 'L') {
price += 2; // Large size costs $2 extra
}
return price;
}
int main() {
vector<pair<string, double>> orders; // Vector to store the orders (item, price)
char addMore;
do {
displayMenu();
int item = processChoice();
if (item < 1 || item > 5) {
cout << "Invalid choice. Please try again." << endl;
continue;
}
char size;
cout << "Enter the size (S/M/L): ";
cin >> size;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore any remaining input
size = toupper(size); // Convert size to uppercase
if (!isValidSize(size)) {
cout << "Invalid size. Please try again." << endl;
continue;
}
double price = calculatePrice(item, size);
orders.push_back(make_pair(MENU_ITEMS[item - 1], price));
cout << "Order added successfully!" << endl;
cout << "Add more orders? (Y/N): ";
cin >> addMore;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore any remaining input
addMore = toupper(addMore); // Convert response to uppercase
} while (addMore == 'Y');
// Calculate the total payable amount
double total = 0;
for (const auto& order : orders) {
total += order.second;
}
cout << "Total payable amount: $" << fixed << setprecision(2) << total << endl;
cout << "Thank you for your order!" << endl;
return 0;
}
Make sure you compile and test the code to ensure it works as expected.