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.
int a, b; if (std::cin >> a >> b) { // both numbers were read }
Try it
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.
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
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)andif (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, thencin.ignore(max, '\n')to throw away the rest of the line. >>into astd::stringreads one whitespace-delimited word. For a whole line usestd::getline.- Leftover newline. After
cin >> n, the Enter key's'\n'is still unread. The nextgetlinereturns an empty line unless youignoreit or callstd::ws. boolinput accepts0and1, ortrue/falseafterstd::boolalpha.- Speed. For large inputs call
std::ios::sync_with_stdio(false); std::cin.tie(nullptr);once at the start. - Prompts.
cinis tied tocout, so a prompt written without a newline still appears before the program waits for input. - Hex and octal. Use
std::hex/std::oct;std::decis the default and does not understand0xprefixes. - Parsing unknown text is more robust with
getlinefollowed bystd::from_charsorstd::stoithan with>>directly.