C++20 sections · 1024 units
Open in Course

String Input with cin

Reading single words

Read a string with cin >> s;. This reads until whitespace (space, tab, newline). For "John Doe", it reads only "John". The space stops reading; "Doe" remains in the buffer. This matches how cin handles numbers.

Reading stops at whitespace. Write cin >> first >> last; to read "John Doe" into two variables: first gets "John", last gets "Doe". Use cin >> for word-by-word data or single tokens.

For commands, usernames, or space-separated data, this works. For full lines with spaces, you need a different approach.