Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

#include <iostream>

#include <string>

#include <iomanip>

const int MAX_PRODUCTS = 5;

struct Product {

std::string name;

double price;

};

void printMenu(const Product products[], int count) {

std::cout << "Available Products:\n";

for (int i = 0; i < count; ++i) {

std::cout << i + 1 << ". " << products[i].name << " - $" << products[i].price << "\n";

void selectProducts(const Product products[], int quantities[], int count) {

int choice, quantity;

while (true) {

std::cout << "Enter product number to add to bill (0 to finish): ";

std::cin >> choice;

if (choice == 0) break;

if (choice < 1 || choice > count) {


std::cout << "Invalid choice. Please try again.\n";

continue;

std::cout << "Enter quantity for " << products[choice - 1].name << ": ";

std::cin >> quantity;

if (quantity < 0) {

std::cout << "Quantity cannot be negative. Please try again.\n";

continue;

quantities[choice - 1] += quantity;

void printReceipt(const Product products[], const int quantities[], int count) {

double total = 0.0;

std::cout << "\nReceipt:\n";

std::cout << "--------------------------------\n";

for (int i = 0; i < count; ++i) {

if (quantities[i] > 0) {

double cost = quantities[i] * products[i].price;

std::cout << products[i].name << " x " << quantities[i]

<< " = $" << std::fixed << std::setprecision(2) << cost << "\n";

total += cost;

}
std::cout << "--------------------------------\n";

std::cout << "Total: $" << std::fixed << std::setprecision(2) << total << "\n";

int main() {

Product products[MAX_PRODUCTS] = {

{"Apple", 0.50},

{"Banana", 0.30},

{"Orange", 0.80},

{"Milk", 1.20},

{"Bread", 1.50}

};

int quantities[MAX_PRODUCTS] = {0};

printMenu(products, MAX_PRODUCTS);

selectProducts(products, quantities, MAX_PRODUCTS);

printReceipt(products, quantities, MAX_PRODUCTS);

return 0;

You might also like