R.1 — iostream: printing and reading

📘 Appendix R⏱ about 1 min read✍️ Beginner friendly
Read one box at a time. No rush.

std::cout#

Print to the screen.

C++
std::cout << "Score: " << 10 << '\n';

std::cin#

Read from the keyboard into a variable. Stops at spaces.

C++
int age {};std::cin >> age;

std::getline#

Read a whole line, spaces included.

C++
std::string line {};std::getline(std::cin >> std::ws, line);

std::cerr#

Print errors and debug messages. Shows up right away.

C++
std::cerr << "Something went wrong!\n";

Formatting numbers#

Needs #include <iomanip>.

C++
std::cout << std::fixed << std::setprecision(2) << 3.14159;   // 3.14std::cout << std::setw(5) << 42;                              // "   42"std::cout << std::boolalpha << true;                          // true
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.