C++ Online Compiler
Example: Simple Console Bill in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Simple Console Bill #include <iostream> #include <iomanip> // Required for formatting output int main() { // Step 1: Define item details std::string item1Name = "Laptop"; int item1Qty = 1; double item1Price = 1200.00; std::string item2Name = "Mouse"; int item2Qty = 2; double item2Price = 25.00; std::string item3Name = "Keyboard"; int item3Qty = 1; double item3Price = 75.00; // Step 2: Calculate total for each item double item1Total = item1Qty * item1Price; double item2Total = item2Qty * item2Price; double item3Total = item3Qty * item3Price; // Step 3: Calculate Subtotal, Tax, and Grand Total double subtotal = item1Total + item2Total + item3Total; double taxRate = 0.08; // 8% tax double taxAmount = subtotal * taxRate; double grandTotal = subtotal + taxAmount; // Step 4: Print the bill header std::cout << "----------------------------------------\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"; // Step 5: Print individual item details std::cout << std::left << std::setw(15) << item1Name << std::right << std::setw(5) << item1Qty << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item1Price << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item1Total << "\n"; std::cout << std::left << std::setw(15) << item2Name << std::right << std::setw(5) << item2Qty << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item2Price << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item2Total << "\n"; std::cout << std::left << std::setw(15) << item3Name << std::right << std::setw(5) << item3Qty << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item3Price << std::right << std::setw(10) << std::fixed << std::setprecision(2) << item3Total << "\n"; std::cout << "----------------------------------------\n"; // Step 6: Print summary totals 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"; return 0; }
Output
Clear
ADVERTISEMENTS