C++ Basics: Input and Output

Use cin and cout to read from the keyboard and print to the screen, and format output with iomanip tools.

cout and the << Operator

cout (character output) sends data to the standard output stream (the terminal). The insertion operator << chains values together in a single statement.

cout

cout is a global object defined in <iostream>; every value you chain with << is converted to text and sent to the terminal.

  • cout << value; prints value to standard output
  • Chain multiple values: cout << a << " " << b;
  • You can output any built-in type: int, double, char, bool, string
  • cout buffers output; use endl or flush to ensure it appears immediately

Basic cout Usage

C++

Print an integer and a double with labels.

cin and the >> Operator

cin (character input) reads from the standard input stream (the keyboard). The extraction operator >> reads one whitespace-delimited token at a time.

cin

cin >> skips leading whitespace and reads until the next whitespace, so it reads one word or number at a time.

  • cin >> variable; reads one token into variable
  • Chain reads: cin >> a >> b; reads two space-separated values
  • cin fails silently if the input does not match the variable's type
  • After a failed read, cin enters an error state, call cin.clear() to recover

Reading Multiple Inputs

C++

Chain >> to read two integers in one statement.

endl vs \n

Both move the cursor to the next line, but endl also flushes the output buffer. In a tight loop, prefer \n for performance.

endl vs \\n

Flushing the buffer after every line is slow; use \\n inside loops and endl only when you need guaranteed immediate output.

  • endl: newline + buffer flush (slower, use for debug or final output)
  • \n: newline only, no flush (faster inside loops)
  • In competitive programming, \n is always preferred for speed
  • cout.flush() explicitly flushes without adding a newline

endl vs \\n in a Loop

C++

Use \\n inside loops; flush once at the end with endl.

Formatting Output

The <iomanip> header provides manipulators like setw and setprecision to control column width and decimal precision.

setw and setprecision

setw sets the minimum field width for the next output only; setprecision with fixed sets decimal places for all subsequent floating-point output.

  • setw(n): pad the next value to at least n characters wide (right-aligned by default)
  • setprecision(n): significant digits (or decimal places with fixed)
  • fixed: use fixed decimal notation instead of scientific notation
  • left / right: change alignment within the field set by setw

Formatted Output

C++

Control decimal precision and column alignment.

getline() Function

getline(cin, str) reads an entire line including spaces into a string, stopping at the newline character. Use it whenever the input may contain spaces.

getline

After using cin >> to read a value, call cin.ignore() before getline to discard the leftover newline in the buffer.

  • Syntax: getline(cin, myString);
  • Reads until newline; newline is consumed but not stored
  • Use cin.ignore() after cin >> before calling getline to clear the newline
  • Can specify a custom delimiter: getline(cin, str, ',')

getline for Full Lines

C++

Read a name with spaces from standard input.

Reading Multiple Inputs

You can chain cin >> to read several values at once. Users may separate them with spaces or newlines, cin treats both as delimiters.

Multiple cin Reads

Chained >> reads are efficient and clear; declare variables close to where you read them so type mismatches are easy to spot.

  • cin >> a >> b >> c reads three values in one statement
  • Input can be on one line (space-separated) or multiple lines
  • If a read fails (wrong type), all subsequent reads in the chain also fail
  • Use cin.fail() to check if a read succeeded

Reading Mixed Types

C++

Read an integer and a double from the same input line.

Knowledge Check

1. Which operator sends data to cout?

2. What is the difference between endl and \n?

3. Which function reads an entire line including spaces?

4. What does setw(10) do?

5. Which header is needed for setw and setprecision?