C++20 sections · 1024 units
Open in Course

Reading char Values

Character input

I use char for single characters: char grade; cin >> grade; reads one character. If the user types A and presses Enter, grade becomes 'A' and the newline stays in the buffer.

The cin >> operator skips leading whitespace before reading a character. If the buffer contains X, it skips the spaces and reads X. This differs from strings, where spaces matter.

If you type multiple characters like ABC, cin >> grade; reads only 'A', leaving BC plus the newline in the buffer. This causes unexpected behavior when you read the next value.