{ }codelper

Learn › C++

Basics · 03

Reading input

Read text and numbers from the keyboard (standard input).

To solve problems you need to read input (standard input).
The example below reads a name on one line and a number on the next, then greets the name and prints twice the number.

#include <iostream>#include <string> int main() {    std::string name;    int n;    std::getline(std::cin, name);    std::cin >> n;    std::cout << "Hi, " << name << "!" << '\n';    std::cout << n * 2 << '\n';}
stdin
Lara
3
stdout
Hi, Lara!
6

Key points

  • Whatever you read is always a string.
    To use it as a number, convert it.
  • You can read input a whole line at a time, or one space-separated token (a word or a number) at a time.
  • For several numbers on one line, split the line at the spaces and convert each piece.
  • When you don't know how many lines there are, read until the input ends.
    Each language signals "the end" in its own way.

How each language writes it

PythonA line is input(), convert with int(...).
Several numbers on a line: map(int, input().split()).
Until the end: for line in sys.stdin:
JavaScriptThe usual way is to read everything at once with require("fs").readFileSync(0, "utf8") and split lines with split("\n").
Convert with Number(...).
For word-by-word reading, .trim().split(/\s+/) is safer
TypeScriptSame as JavaScript
JavaRead with Scanner's nextLine() and convert with Integer.parseInt(...).
There is also nextInt() for a single number.
Until the end: while (sc.hasNextInt())
C#Read with Console.ReadLine() and convert with int.Parse(...).
Several numbers: Split(' ').
Until the end: while ((line = Console.ReadLine()) != null)
C++std::cin >> n skips spaces and newlines and reads one token straight into a number.
A whole line: std::getline(std::cin, s).
Until the end: while (std::cin >> x)
Cscanf("%d", &n) — the & before the variable is required.
%s reads only one word, up to a space; for a whole line use fgets.
Until the end: while (scanf("%d", &x) == 1)
CraykonA line is Console.ReadLine(), convert with .ToNumber().
Several numbers on a line: Console.ReadNumbers() (gives a list of numbers).
Until the end: until Console.ReadLine() returns null

Common mistakes

Reading a line right after a numberJava · C++

int n = sc.nextInt();
String name = sc.nextLine();

→ name is an empty string "", not the next line: it only read the newline left after the number

As in this example, read whole lines with nextLine() from the start and convert with Integer.parseInt.
C++ getline after cin >> n has the same problem.

Converting an empty line to a numberJavaScript · TypeScript

Number("")

→ Silently 0, which hides the mistake of reading an empty line

A newline at the end of the input leaves one extra empty string at the end.
.trim() before splitting.

How languages differ
What you read is a string, so calculating without converting goes wrong.
In JavaScript, adding 1 to the input 3 gives "3" + 1 → "31", and in Python input() + 1 is a TypeError.

Practice