C++ Tutorial

C++ Project: Shopping Cart

A cart of line items with name, quantity, and price. Print a receipt and the grand total.

What you will build

A cart is a vector of line items. Each line has a name, a quantity, and a unit price. The receipt prints every line with an extended price (quantity times unit price) and a grand total. Money uses two decimal places through <iomanip>.

Items are hardcoded so the receipt is stable. Click Try it in C++ and run it at/cpp/try. Do not use the Python editor or the HTML preview for this program.

A line item struct

Name is a string. Quantity is an int. Price is a double for the unit cost. The extended total is not stored; you compute it when you print so quantity changes cannot leave a stale field behind.

Example

#include <iostream>
#include <string>
using namespace std;

struct Line {
  string name;
  int qty;
  double price;
};

double extended(const Line& item) {
  return item.qty * item.price;
}

int main() {
  Line tea;
  tea.name = "Green tea";
  tea.qty = 2;
  tea.price = 3.50;
  cout << tea.name << " x" << tea.qty << " = " << extended(tea) << endl;
  return 0;
}

Two teas at 3.50 is 7. Default printing may show 7 rather than 7.00. Formatting comes next.

Two decimal places

Currency needs fixed and setprecision(2). Set them once before the money columns.setw still applies only to the next value, so call it on each cell of the receipt.

Example

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  cout << fixed << setprecision(2);
  cout << setw(8) << 3.5 << endl;
  cout << setw(8) << 7.0 << endl;
  cout << setw(8) << 12.45 << endl;
  return 0;
}

Open /cpp/try with Try it in C++. You should see 3.50,7.00, and 12.45, each in an 8-character field.

A vector of lines

Push each item onto the cart. A loop prints names. The grand total is the sum of extended for every row. Pass the cart by const reference into the receipt function.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Line {
  string name;
  int qty;
  double price;
};

double extended(const Line& item) {
  return item.qty * item.price;
}

int main() {
  vector<Line> cart;
  cart.push_back({"Green tea", 2, 3.50});
  cart.push_back({"Oat bun", 1, 2.25});
  cart.push_back({"Soup", 3, 4.00});
  double total = 0.0;
  for (const Line& item : cart) {
    total = total + extended(item);
    cout << item.name << endl;
  }
  cout << "lines " << cart.size() << endl;
  cout << "raw total " << total << endl;
  return 0;
}

Complete receipt

Print a header, one row per item with quantity, unit price, and line total, then a grand total. Skip items whose quantity is not positive so a zero-qty leftover does not show as a free product. This cart has three real lines.

Example

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

struct Line {
  string name;
  int qty;
  double price;
};

double extended(const Line& item) {
  return item.qty * item.price;
}

void printReceipt(const vector<Line>& cart) {
  cout << fixed << setprecision(2);
  cout << left << setw(14) << "Item" << right
       << setw(5) << "Qty"
       << setw(10) << "Price"
       << setw(10) << "Total" << endl;

  double grand = 0.0;
  for (const Line& item : cart) {
    if (item.qty <= 0) {
      continue;
    }
    double lineTotal = extended(item);
    grand = grand + lineTotal;
    cout << left << setw(14) << item.name << right
         << setw(5) << item.qty
         << setw(10) << item.price
         << setw(10) << lineTotal << endl;
  }
  cout << left << setw(14) << "Grand total" << right
       << setw(5) << ""
       << setw(10) << ""
       << setw(10) << grand << endl;
}

int main() {
  vector<Line> cart;
  cart.push_back({"Green tea", 2, 3.50});
  cart.push_back({"Oat bun", 1, 2.25});
  cart.push_back({"Soup", 3, 4.00});
  printReceipt(cart);
  return 0;
}

Line totals are 7.00, 2.25, and 12.00. The grand total is 21.25. Change soup to quantity 0 and that row disappears; the total drops to 9.25.

Common mistakes

  • Storing money as int without a plan. This tutorial uses double and two displayed digits. For a later version you can store cents as integers; do not mix both in one receipt.
  • Printing the unit price as the line total. Multiply by quantity every time.
  • Forgetting fixed. Then setprecision(2) means two significant digits, and 12.00 can become 12 or scientific notation on other values.
  • Left-aligning numbers and right-aligning names. Names read well on the left; money lines up on the right.

Practice

  1. Add a fourth item and confirm the grand total includes it.
  2. Print a tax line of 8 percent of the grand total, then a new amount due.
  3. If the cart is empty, print Cart is empty instead of a header and a zero total.

That is the last C++ project on this hub. Return to C++ Projects or open/cpp/try and change the demo data on any program you already compiled.

FAQ: C++ Project: Shopping Cart

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ cart project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ cart project in this C++ C++ lesson (C++ Project: Shopping Cart).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.