user16861763
user16861763

Reputation:

Two cins in a row, what exactly happens with whitespaces?

cin >> name;
cin >> age;
cout << name << age;

What exactly is happening here if I type a string, then some whitespace and a number? For example Something 20. Does it read Something then sees the whitespace and goes okay that's the end of this first line because a whitespace terminates the reading of the string, goes to the next input and reads 20?

But I also wanna be a bit more specific. Is it okay to say at first when I'm in the console typing Something, that's going into the standard input stream, then getting stored in the buffer and when I press that space it's like pressing enter? And that Something gets extracted and assigned to name? Then that 20 I type is like a whole new unrelated line because I pressed space earlier and so that gets extracted and assigned to age?

Upvotes: 2

Views: 143

Answers (1)

Light
Light

Reputation: 61

How they'll get extracted

The integer gets extracted via std::basic_istream::operator::>>:

Extracts values from an input stream

1-4 ) Extracts an integer value potentially skipping preceding whitespace. The value is stored to a given reference value.

This function behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling std::num_get::get().

The string gets extracted via std::basic_string::operator>>:

2 ) Behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, first clears str with str.erase(), then reads characters from is and appends them to str as if by str.append(1, c), until one of the following conditions becomes true:

  • N characters are read, where N is is.width() if is.width() > 0, otherwise N is str.max_size()
  • the end-of-file condition occurs in the stream is
  • std::isspace(c,is.getloc()) is true for the next character c in is (this whitespace character remains in the input stream).

And in FormattedInputFunction:

if ios_base::skipws flag is set on this input stream, extracts and discards characters from the input stream until one of the following becomes true:

  • the next available character on the input stream is not a whitespace character, as tested by the std::ctype facet of the locale currently imbued in this input stream. The non-whitespace character is not extracted.
  • the end of the stream is reached, in which case failbit and eofbit are set and if the stream is on for exceptions on one of these bits, ios_base::failure is thrown.

And as stated in Basic Input/Output from cplusplus.com:

...Note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed.

...

...cin extraction always considers spaces (whitespaces, tabs, new-line...) as terminating the value being extracted, and thus extracting a string means to always extract a single word, not a phrase or an entire sentence.

Testing

Compiling and testing your program with leading and trailing whitespaces via MSVC-v142 compiler:

            AA           123             some trailing whitespaces

Prints out:

AA123

Read also

Stackoverflow: Clarify the difference between input/output stream and input/output buffer

Learn cpp: Input and output streams

Upvotes: 3

Related Questions