std::cin (C++)

std::cin is C++'s standard input stream, used to read typed input from the user. You extract values from it with the >> extraction operator.

C++

cin automatically skips leading whitespace and stops reading at the next space or newline, so reading into a string only grabs a single word. Use std::getline() when you need a whole line.

Like cout, cin is declared in <iostream> and lives in the std namespace.

Syntax

std::cin >> variable;

Examples

C++

#include <iostream>

int main() {
    int n;
    std::cout << "Enter a number: ";
    std::cin >> n;
    std::cout << "You typed " << n << std::endl;
    return 0;
}

Output:

Enter a number: 7
You typed 7

Learn it in a lesson

See also

Back to the Dictionary