How do I limit the number of characters entered via cin?

c++

Solution

You can use `setw()`

 cin >> setw(2) >> var;

http://www.cplusplus.com/reference/iostream/manipulators/setw/

Sets the number of characters to be used as the field width for the next insertion operation.

Working example provided by @chris: http://ideone.com/R35NN

Problem

I wish to limit the number of characters that the user can enter, using `cin`. I might wish to limit it to two characters, for instance. How might I do this? My code looks like this: ``` cin >> var; ```

Original source