如何在C++程序中使用循环Switch语句实现含GST的商品计费?
C++ Program with Loop + Switch for Product Billing (Including GST)
Hey there! Let's put together that C++ billing program you need—using loops and switch statements to handle 3-4 products, calculate individual totals, and add GST at the end. I'll break this down clearly so you can follow along and adapt it to your specific needs.
Step 1: Core Requirements Recap
First, let's align on what we're building:
- Let users select from 3-4 products
- Input each product's unit price and quantity
- Calculate the total for each product (price × quantity)
- Sum all product totals into a subtotal
- Add GST to the subtotal for the final amount
- Use a loop to let users add multiple products until they're done
- Use a switch statement to handle product selection
Step 2: Full Working Code
Here's a complete, commented implementation that hits all your requirements:
#include <iostream> #include <iomanip> // For clean currency formatting using namespace std; int main() { // Define constants (easy to adjust later) const double GST_RATE = 0.10; // 10% GST - tweak this for your region const int EXIT_OPTION = 5; // Variables to track billing data int productChoice; double unitPrice, quantity, productTotal, subtotal = 0.0, gstAmount, finalTotal; // Print welcome/menu cout << "=== Company Product Billing System ===" << endl; cout << "Available Products:" << endl; cout << "1. Wireless Headphones" << endl; cout << "2. Portable Charger" << endl; cout << "3. Laptop Sleeve" << endl; cout << "4. USB-C Cable" << endl; cout << "5. Exit & Calculate Final Total" << endl; // Do-while loop: runs at least once, stops when user selects exit do { cout << "\nEnter your choice (1-5): "; cin >> productChoice; // Switch statement to handle each product/action switch(productChoice) { case 1: cout << "Enter unit price of Wireless Headphones: "; cin >> unitPrice; cout << "Enter quantity: "; cin >> quantity; productTotal = unitPrice * quantity; subtotal += productTotal; cout << "Added: $" << fixed << setprecision(2) << productTotal << endl; break; case 2: cout << "Enter unit price of Portable Charger: "; cin >> unitPrice; cout << "Enter quantity: "; cin >> quantity; productTotal = unitPrice * quantity; subtotal += productTotal; cout << "Added: $" << fixed << setprecision(2) << productTotal << endl; break; case 3: cout << "Enter unit price of Laptop Sleeve: "; cin >> unitPrice; cout << "Enter quantity: "; cin >> quantity; productTotal = unitPrice * quantity; subtotal += productTotal; cout << "Added: $" << fixed << setprecision(2) << productTotal << endl; break; case 4: cout << "Enter unit price of USB-C Cable: "; cin >> unitPrice; cout << "Enter quantity: "; cin >> quantity; productTotal = unitPrice * quantity; subtotal += productTotal; cout << "Added: $" << fixed << setprecision(2) << productTotal << endl; break; case EXIT_OPTION: cout << "\nCalculating your final bill..." << endl; break; default: cout << "Oops! Invalid choice—please enter a number between 1 and 5." << endl; } } while(productChoice != EXIT_OPTION); // Calculate GST and final total gstAmount = subtotal * GST_RATE; finalTotal = subtotal + gstAmount; // Print formatted final bill cout << "\n=== Final Bill Summary ===" << endl; cout << "Subtotal: $" << fixed << setprecision(2) << subtotal << endl; cout << "GST (" << GST_RATE * 100 << "%): $" << fixed << setprecision(2) << gstAmount << endl; cout << "Total Amount Due: $" << fixed << setprecision(2) << finalTotal << endl; return 0; }
Step 3: Key Features Explained
do-whileLoop: Guarantees the program runs at least once, and keeps accepting product entries until the user selects the exit option.- Switch Statement: Cleanly separates logic for each product—no messy nested if-else chains. The default case catches invalid inputs to prevent crashes.
- Currency Formatting:
fixedandsetprecision(2)ensure all monetary values display with two decimal places (standard for billing). - Adjustable Constants:
GST_RATEandEXIT_OPTIONare defined as constants so you can tweak them without digging through the entire code.
Customization Tips
- Replace the product names (Wireless Headphones, etc.) with your actual product lines.
- If you only need 3 products, remove case 4 and update the menu text accordingly.
- Change
GST_RATEto match your local tax rate (e.g.,0.05for 5% GST).
内容的提问来源于stack exchange,提问作者Muhammad Mohsin Amir
相关产品推荐
相关产品推荐

