Sunrise
Sunrise

Reputation: 1321

Why cin doesn't recognize enter key, as cin.get does?

For example, if write those statements in code:

char a[10];
char b[10];
cin>>a;
cin>>b; 

cin>>b; doesn't see Enter key that was pressed after typing, for example, Hello

but when instead cin>>b; write cin.get(b, 10); then cin.get(b, 10); reads Enter key from previous statement.

Upvotes: 0

Views: 550

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 71989

Working under the assumption that a and b are arrays of char here, because otherwise your question does not make sense.

get is an "unformatted" input function, meant to read the input as it comes into the stream. That's why it reads the newline.

>> is a "formatted" input function, meant to read a specific type of data in a natural way. In particular, >> with a char array reads a single word, i.e. a sequence of characters not containing whitespace. This is why it stops reading when it encounters the newline, which is whitespace.

Upvotes: 6

Related Questions