C++ Online Compiler
Example: Modular Bill Generator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Modular Bill Generator #include <iostream> #include <vector> #include <string> #include <iomanip> // Define a structure for an item struct BillItem { std::string name; int quantity; double price; double itemTotal; // Total for this specific item }; // Function to get item details from user void getItemDetails(BillItem& item, int itemCounter) { std::cout << "\nEnter details for Item " << itemCounter << ":\n"; std::cout << "Enter item name: "; std::cin.ignore(); // Consume the newline character left by previous cin std::getline(std::cin, item.name); std::cout << "Enter quantity: "; std::cin >> item.quantity; std::cout << "Enter price per item: $"; std::cin >> item.price; item.itemTotal = item.quantity * item.price; } // Function to calculate all totals void calculateTotals(const std::vector<BillItem>& items, double& subtotal, double& taxAmount, double& grandTotal, double taxRate = 0.08) { subtotal = 0.0; for (const auto& item : items) { subtotal += item.itemTotal; } taxAmount = subtotal * taxRate; grandTotal = subtotal + taxAmount; } // Function to print the entire bill void printBill(const std::vector<BillItem>& items, double subtotal, double taxAmount, double grandTotal, double taxRate = 0.08) { std::cout << "\n----------------------------------------\n"; std::cout << std::setw(20) << "YOUR RECEIPT\n"; std::cout << "----------------------------------------\n"; std::cout << std::left << std::setw(15) << "Item" << std::right << std::setw(5) << "Qty" << std::right << std::setw(10) << "Price" << std::right << std::setw(10) << "Total\n"; std::cout << "----------------------------------------\n"; for (const auto& item : items) { std::cout << std::left << std::setw(15) << item.name << std::right << std::setw(5) << item.quantity << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item.price << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item.itemTotal << "\n"; } std::cout << "----------------------------------------\n"; std::cout << std::left << std::setw(25) << "Subtotal:" << std::right << std::setw(15) << std::fixed << std::setprecision(2) << subtotal << "\n"; std::cout << std::left << std::setw(25) << "Tax (" << (taxRate * 100) << "%):" << std::right << std::setw(15) << std::fixed << std::setprecision(2) << taxAmount << "\n"; std::cout << "----------------------------------------\n"; std::cout << std::left << std::setw(25) << "Grand Total:" << std::right << std::setw(15) << std::fixed << std::setprecision(2) << grandTotal << "\n"; std::cout << "----------------------------------------\n"; std::cout << "Thank You!\n"; } int main() { std::vector<BillItem> items; double subtotal = 0.0; double taxAmount = 0.0; double grandTotal = 0.0; double taxRate = 0.08; char addMore = 'y'; int itemCounter = 1; // Loop to get item details while (addMore == 'y' || addMore == 'Y') { BillItem currentItem; getItemDetails(currentItem, itemCounter); // Call function to get details items.push_back(currentItem); std::cout << "Add another item? (y/n): "; std::cin >> addMore; itemCounter++; } // Calculate all totals using a function calculateTotals(items, subtotal, taxAmount, grandTotal, taxRate); // Print the bill using a function printBill(items, subtotal, taxAmount, grandTotal, taxRate); return 0; }
Output
Clear
ADVERTISEMENTS