Listing the cart
- C++ course
- Beginner
- Lesson 51/116
- 6 min
Write the function that prints the cart items formatted.
Listing the cart
To print the products in the cart we will write a printCart function. This function walks the items vector and writes each product's name, price and quantity on one line.
You walk each product with range-based for; on each iteration you print std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;. Because the function does not modify members, adding const at the end is a good habit; the compiler then prevents accidental changes.
Formatted output
In printCart, while each product is written on a single line, spaces separate the fields. Writing std::cout << a << " " << b separates a and b with a space. std::endl moves the cursor to the next line.
The formatting choice drives how readable the output is. When column alignment is needed you use std::setw from <iomanip>; in this project we will just separate with spaces. Writing three fields on one line lets you scan the cart quickly.
Practice Write the printCart function
Add a printCart function to the Cart struct. Its signature must be void printCart() const. In the body walk each product with range-based for and print with std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;. main already adds two products and calls printCart. The output must be two lines: Notebook 25 2 and Pen 10 3.
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);
}
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);
cart.printCart();
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;
}
void printCart() const {
for (const auto& p : items) {
std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;
}
}
};
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);
cart.printCart();
return 0;
}Expected output
Notebook 25 2 Pen 10 3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does printCart do?
cart.printCart();- Adds a product
- Computes the total
- Removes a product
- Prints the cart to the screen Correct answer
Explanation: printCart writes the products in the cart to the screen.
2 Match each format part to its meaning
| Term | Meaning |
|---|---|
p.name |
Product name |
p.price |
Unit price |
p.quantity |
Quantity |
std::endl |
Line end |
3 Write the printCart body
void printCart() const {
for (const auto& p : items) {
std::cout << ____ << " " << p.price << " " << p.quantity << std::endl;
}
}
void printCart() const {
for (const auto& p : items) {
std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;
}
}
Explanation: p.name goes at the start of the line.