C++ Playground

A starter program to experiment with. Edit it here, press Copy, paste it into a file called main.cpp, and compile and run it with c++ -std=c++20 -Wall -Wextra main.cpp -o main && ./main. Lesson 2 shows how to install a compiler.

Try it yourself · main.cpp
Edit the code, copy it into main.cpp, then compile and run it with c++ -std=c++20 -Wall main.cpp -o main && ./main. How to set up a C++ compiler
Output
Backpack     1 x  39.90 =   39.90
Notebook     4 x   3.50 =   14.00
Pen         10 x   1.20 =   12.00
Total: 65.90

Ideas to try

  • Apply a 10% discount when the total is over 50 (lesson 10).
  • Read the items from a file instead of hard-coding them (lesson 44).
  • Count items under 5.00 with std::ranges::count_if and a lambda (lessons 24–25).
  • Turn Item into a class that rejects negative prices in its constructor (lesson 32).
  • Print with C++23's std::println instead of std::cout (lesson 50).

Compiling without installing anything

Online compilers such as Compiler Explorer (godbolt.org) compile and run C++ in your browser. Choose a recent GCC or Clang and add -std=c++20 to the compiler options.