C++ Program
Published 11/3/2025, 12:29:03 AM
Of course. Here is the C++ program and the corresponding flowcharts to compute the final shopping cart total based on the rules you've provided.
C++ Program
This program calculates the final price for a shopping cart based on item quantity, VIP membership status, and shipping rules.
#include <iostream>
#include <string>
#include <iomanip>
// Function to calculate the final price
void calculateFinalPrice() {
// --- 1. Input ---
double itemPrice = 0.0;
int quantity = 0;
char isMemberChar;
bool isMember = false;
std::cout << "Enter the price per item: ";
std::cin >> itemPrice;
std::cout << "Enter the quantity of items: ";
std::cin >> quantity;
std::cout << "Is the customer a VIP member? (y/n): ";
std::cin >> isMemberChar;
isMember = (isMemberChar == 'y' || isMemberChar == 'Y');
// --- 2. Input Validation ---
if (quantity < 0 || itemPrice < 0) {
std::cerr << "Error: Invalid input. Quantity and price cannot be negative." << std::endl;
return;
}
// --- 3. Discount Calculation ---
double baseDiscountRate = 0.0;
if (quantity >= 6 && quantity <= 10) {
baseDiscountRate = 0.05; // 5% discount
} else if (quantity > 10) {
baseDiscountRate = 0.10; // 10% discount
}
double vipDiscountRate = 0.0;
if (isMember) {
vipDiscountRate = 0.05; // Extra 5% for VIPs
}
double totalDiscountRate = baseDiscountRate + vipDiscountRate;
// --- 4. Price Calculation ---
double subtotal = itemPrice * quantity;
double discountedTotal = subtotal * (1 - totalDiscountRate);
// --- 5. Shipping Fee Calculation ---
double shippingFee = 0.0;
if (!isMember) {
if (discountedTotal <= 5000.0) {
shippingFee = 100.0; // 100 pesos shipping fee
}
// Otherwise, shipping is free
}
// VIP members always get free shipping
double finalTotal = discountedTotal + shippingFee;
// --- 6. Output ---
std::cout << "\n--- Receipt ---" << std::endl;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Subtotal: " << subtotal << " pesos" << std::endl;
std::cout << "Total Discount Applied: " << (totalDiscountRate * 100) << "%" << std::endl;
std::cout << "Price after Discount: " << discountedTotal << " pesos" << std::endl;
std::cout << "Shipping Fee: " << shippingFee << " pesos" << std::endl;
std::cout << "-----------------" << std::endl;
std::cout << "Final Total: " << finalTotal << " pesos" << std::endl;
}
int main() {
calculateFinalPrice();
return 0;
}
Key Features of the Code
- Input Gathering: Prompts the user to enter the item price, quantity, and membership status.
- Input Validation: Checks if the quantity or price is negative and displays an error if true.
- Nested Logic: Uses
if-else if-elsestatements to determine the base discount from the quantity. - Logical Operators: Combines conditions (e.g.,
quantity >= 6 && quantity <= 10) to define the discount tiers. - VIP Discount: A separate
ifstatement checks the membership status to apply an additional discount, demonstrating layered logic. - Shipping Calculation: A nested
ifchecks for non-membership status first, then checks the total amount to decide the shipping fee. - Formatted Output: Uses
<iomanip>to present the final prices in a clean, readable format with two decimal places.
Flowcharts
Here are two flowcharts that visualize the program's logic: a high-level (main logic) flowchart and a detailed (calculation logic) flowchart.
1. High-Level Flowchart
This chart shows the overall structure and sequence of the program.
graph TD
A[Start] --> B{Input itemPrice, quantity, isMember};
B --> C{quantity < 0?};
C -- Yes --> D[Display Error];
C -- No --> E[Calculate Discounts];
E --> F[Calculate Subtotal and Discounted Total];
F --> G[Calculate Shipping Fee];
G --> H[Calculate Final Total];
H --> I{Print Receipt};
I --> J[End];
D --> J;
| Shape | Description |
|---|---|
| [Start] / [End] | Ovals representing the start and end points of the program. |
| {Input / Print} | Parallelograms representing data input from the user or output to the screen. |
| [Process] | Rectangles representing calculations or data manipulation. |
| {Decision?} | Diamonds representing a point where a decision is made (e.g., an if statement). |
2. Detailed Calculation Flowchart
This chart provides a step-by-step breakdown of the discount and shipping fee calculations.
graph TD
subgraph Detailed Calculation Logic
A[Start Calculation] --> B{quantity >= 6 AND quantity <= 10?};
B -- Yes --> C[baseDiscount = 0.05];
B -- No --> D{quantity > 10?};
D -- Yes --> E[baseDiscount = 0.10];
D -- No --> F[baseDiscount = 0.0];
C --> G{isMember == true?};
E --> G;
F --> G;
G -- Yes --> H[totalDiscount = baseDiscount + 0.05];
G -- No --> I[totalDiscount = baseDiscount];
H --> J[discountedTotal = (itemPrice * quantity) * (1 - totalDiscount)];
I --> J;
J --> K{isMember == false?};
K -- No --> L[shippingFee = 0];
K -- Yes --> M{discountedTotal > 5000?};
M -- Yes --> N[shippingFee = 0];
M -- No --> O[shippingFee = 100];
L --> P[finalTotal = discountedTotal + shippingFee];
N --> P;
O --> P;
P --> Q[End Calculation];
end
This detailed view clearly illustrates how the nested if conditions and logical operators work together to compute the final price according to the specified business rules.