Question: #include #include using namespace std; const int NUM_FRUITS = 3; const int NUM_DRIED_GOODS = 3; const int NUM_DAIRY = 3; const int MAX_LOGIN_ATTEMPTS = 3;
#include
using namespace std;
const int NUM_FRUITS = 3; const int NUM_DRIED_GOODS = 3; const int NUM_DAIRY = 3; const int MAX_LOGIN_ATTEMPTS = 3;
struct Item { string name; double price; int quantity; }; void displayCategory(string category, Item items[], int numitems) { cout << category << endl; for (int i = 0; i < numitems; i++) { int quantity = items[i].quantity; int originalquantity = numitems - quantity; if (quantity > 0) { cout << i + 1 << ". " << items[i].name << ", quantity in cart: " << quantity << " - $" << items[i].price << endl; } else { cout << i + 1 << ". " << items[i].name << " - $" << items[i].price << endl; } } }
void addtoCart(Item items[], int numItems, double &total, int &totalQuantity) { int selection, quantity; cout << "Enter the number of the item you want to add to your cart: "; cin >> selection; cout << "Enter the quantity: "; cin >> quantity; if (selection > 0 && selection <= numItems) { items[selection-1].quantity += quantity; total += items[selection-1].price * quantity; totalQuantity += quantity; cout << "Added " << quantity << " " << items[selection-1].name << " to cart." << endl; } else { cout << "Invalid Selection. Please try again." << endl; } }
bool login() { string username, password; cout <<"Welcome to George's supermarket!!Murah Murah!! Your username is admin and password is password, Thank you"; cout <<" Enter your username: "; cin >> username; cout << "Enter your password: "; cin >> password; return username == "admin" && password == "password"; }
void receipt(double total, string paymentMethod, string creditcardInformation, string email, Item fruits[], int NUM_FRUITS, Item dried_goods[], int NUM_DRIED_GOODS, Item dairy[], int NUM_DAIRY, int totalQuantity) { cout << "Your receipt " << endl; cout << "Your payment method: " << paymentMethod << endl; cout << "Your credit card information: "<
double paymentAmount; cout << "Total Cost: " << total << endl; cout << "Enter PaymentAmount: "; if (paymentAmount >= total) { double change = paymentAmount - total; cout << "Thank you! Payment successful. Your change is RM" << change << endl; for (int i = 0; i < NUM_FRUITS; i++) { fruits[i].quantity = 0; } for (int i = 0; i < NUM_DRIED_GOODS; i++) { dried_goods[i].quantity = 0; } for (int i = 0; i < NUM_DAIRY; i++) { dairy[i].quantity = 0; } total = 0.0; totalQuantity = 0; } else { cout << "Payment unsuccessful. Payment amount is less than the total cost" << endl; } }
int main() { Item fruits[NUM_FRUITS] = {{"Apple", 0.99}, {"Banana", 0.59}, {"Orange", 0.89}}; Item dried_goods[NUM_DRIED_GOODS] = {{"Raisins", 4.99}, {"Almonds", 9.99}, {"Cashews", 8.99}}; Item dairy[NUM_DAIRY] = {{"Milk", 2.99}, {"Cheese", 3.99}, {"Yogurt", 1.99}}; double total = 0.0; int attempts = 0; int totalQuantity = 0; bool continueShopping = true;
while (attempts < MAX_LOGIN_ATTEMPTS) { if (login()) { while (continueShopping) { int selection = 0; while (selection != 4) { cout << "Select a category:" << endl; cout << "1. Fruits" << endl; cout << "2. Dried goods" << endl; cout << "3. Dairy" << endl; cout << "4. Complete purchase and generate receipt" << endl; cin >> selection; switch (selection) { case 1: displayCategory("Fruits", fruits, NUM_FRUITS); addtoCart(fruits, NUM_FRUITS, total, totalQuantity); break; case 2: displayCategory("Dried goods", dried_goods, NUM_DRIED_GOODS); addtoCart(dried_goods, NUM_DRIED_GOODS, total, totalQuantity); break; case 3: displayCategory("Dairy", dairy, NUM_DAIRY); addtoCart(dairy, NUM_DAIRY, total, totalQuantity); break; case 4: if (total == 0.0) { cout << "Your cart is empty. Please add items to your cart before completing your purchase." << endl; } else { for (int i = 0; i < NUM_FRUITS; i++) { totalQuantity += fruits[i].quantity; } for (int i = 0; i < NUM_DRIED_GOODS; i++) { totalQuantity += dried_goods[i].quantity; } for (int i = 0; i < NUM_DAIRY; i++) { totalQuantity += dairy[i].quantity; }
bool paymentSuccess = false; double paymentAmount; string paymentMethod, creditcardInformation, email; cout << "Enter your payment method (e.g. Visa): "; cin >> paymentMethod; cout << "Enter your credit card information: "; cin >> creditcardInformation; cout << "Enter your email address: "; cin >> email; receipt(total, paymentMethod, creditcardInformation, email, fruits, NUM_FRUITS, dried_goods, NUM_DRIED_GOODS, dairy, NUM_DAIRY, totalQuantity); total = 0.0; totalQuantity = 0; cout << "Do you want to continue shopping? (Y/N)"; char choice; cin >> choice; continueShopping = (choice == 'Y' || choice == 'y'); } break; default: cout << "Invalid selection. Please try again." << endl; break; } } return 0; } else { cout << "Incorrect username or password. Please try again." << endl; attempts++; } } cout << "Too many login attempts. Exiting." << endl; return 0; }
Please help. dont know why can't compile!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
