Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ std::cin object

Reference for std::cin: the >> operator, whitespace skipping, reading until failure, get, peek, ignore, clearing errors and mixing >> with getline.

std::cin (header <iostream>) is the global std::istream connected to standard input. The extraction operator >> reads a value of the type on its right: it skips leading whitespace, then takes as many characters as form a valid value of that type, and stops at the first character that does not fit.

If nothing valid can be read, the stream enters a failed state and the variable is zeroed (since C++11) or left as it was. Every later read does nothing until you clear the error. That makes cin easy to use for tidy input and treacherous for anything else, so always check the result.

C++
int a, b;
if (std::cin >> a >> b) {
    // both numbers were read
}

Try it

C++ Reading until the input runs out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Mixing extraction with getline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
extern std::istream cin;

std::istream& operator>>(std::istream& is, T& value);   // many overloads

int  get();                                    // one character or EOF
std::istream& get(char& c);
int  peek();                                   // look without consuming
std::istream& ignore(std::streamsize n = 1, int delim = EOF);
std::istream& read(char* buf, std::streamsize count);
std::istream& putback(char c);
std::istream& ws(std::istream&);               // manipulator: skip whitespace
Name Description
value The variable to fill: any arithmetic type, char, std::string, bool, a char array, or a type with its own operator>>.
n, delim ignore discards up to n characters, stopping after it removes delim. Pass std::numeric_limits<std::streamsize>::max() for "no limit".
buf, count Raw read of exactly count characters, no whitespace handling.

Returns: the operators and most members return the stream, which converts to false when it has failed. get() and peek() return the character as an int, or std::char_traits<char>::eof() (usually -1) at the end.

Errors / undefined behavior: parse failure sets failbit; reaching the end sets eofbit as well. Reading into a char array with >> and no width is unsafe (buffer overflow); use std::string. An integer that is too large for the type sets failbit and stores the maximum value of the type.

Usage notes

  • Always test the read. while (cin >> x) and if (cin >> x) are right; while (!cin.eof()) runs one extra time with a stale value. See stream states.
  • Recover from bad input with cin.clear() to reset the flags, then cin.ignore(max, '\n') to throw away the rest of the line.
  • >> into a std::string reads one whitespace-delimited word. For a whole line use std::getline.
  • Leftover newline. After cin >> n, the Enter key's '\n' is still unread. The next getline returns an empty line unless you ignore it or call std::ws.
  • bool input accepts 0 and 1, or true / false after std::boolalpha.
  • Speed. For large inputs call std::ios::sync_with_stdio(false); std::cin.tie(nullptr); once at the start.
  • Prompts. cin is tied to cout, so a prompt written without a newline still appears before the program waits for input.
  • Hex and octal. Use std::hex / std::oct; std::dec is the default and does not understand 0x prefixes.
  • Parsing unknown text is more robust with getline followed by std::from_chars or std::stoi than with >> directly.