Calculating totals
- C++ course
- Beginner
- Lesson 50/116
- 6 min
Learn to compute the subtotal and the tax-inclusive total.
Calculating totals
To compute the subtotal of the products in the cart we will write a getSubtotal function. Each product contributes p.price * p.quantity; we walk all products and sum these to get the subtotal.
This is a classic accumulation pattern: start with double sum = 0;, walk each product with range-based for, and add with sum += p.price * p.quantity. Finally return sum; returns the result. The same approach will help you with stock counts, averages and many other calculations later.
Computing tax
The subtotal is the amount before tax and extra fees. To get the tax-inclusive total you use subtotal * (1 + taxRate); for an 18% tax you apply a 1.18 multiplier.
In this project getSubtotal returns only the subtotal; the taxed total can be computed in main. Separating the two makes it easy to update just the multiplier when the tax rate changes. This separation matters because tax rates vary from country to country.
Practice Write the getSubtotal function
Add a getSubtotal function to the Cart struct. Its signature must be double getSubtotal(). In the body start with double sum = 0;, walk each product with for (const auto& p : items), add with sum += p.price * p.quantity and return with return sum;. main already adds two products and prints getSubtotal. The output must be 80.
Starter code
#include <iostream>
#include <string>
#include <vector>
struct Product {
std::string name;
double price;
int quantity;
};
struct Cart {
std::vector<Product> items;
void addItem(Product p) {
items.push_back(p);
}
};
int main() {
Cart cart;
Product p1;
p1.name = "Notebook";
p1.price = 25.0;
p1.quantity = 2;
cart.addItem(p1);
Product p2;
p2.name = "Pen";
p2.price = 10.0;
p2.quantity = 3;
cart.addItem(p2);
std::cout << cart.getSubtotal() << std::endl;
return 0;
}Sample solution
#include <iostream>
#include <string>
#include <vector>
struct Product {
std::string name;
double price;
int quantity;
};
struct Cart {
std::vector<Product> items;
void addItem(Product p) {
items.push_back(p);
}
double getSubtotal() {
double sum = 0;
for (const auto& p : items) {
sum += p.price * p.quantity;
}
return sum;
}
};
int main() {
Cart cart;
Product p1;
p1.name = "Notebook";
p1.price = 25.0;
p1.quantity = 2;
cart.addItem(p1);
Product p2;
p2.name = "Pen";
p2.price = 10.0;
p2.quantity = 3;
cart.addItem(p2);
std::cout << cart.getSubtotal() << std::endl;
return 0;
}Expected output
80
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each concept to its meaning
| Term | Meaning |
|---|---|
Subtotal |
Pre-tax total |
Tax |
Tax rate |
Total |
Tax-inclusive total |
Quantity |
Item count |
2 Write the getSubtotal body
double sum = 0;
for (const auto& p : items) {
____;
}
return sum;
double sum = 0;
for (const auto& p : items) {
sum += p.price * p.quantity;
}
return sum;
Explanation: Each product contributes price * quantity.
3 What does subtotal mean?
// in a cart- Number of items
- Tax-inclusive total
- Pre-tax intermediate total Correct answer
- Discount amount
Explanation: Subtotal is the pre-tax intermediate total.