How do you make cin typesafe?

Posted by cactusbin on Stack Overflow See other posts from Stack Overflow or by cactusbin
Published on 2010-06-08T02:59:19Z Indexed on 2010/06/08 3:02 UTC
Read the original article Hit count: 267

Filed under:
|

It is well known that cin is not typesafe (e.g. cin >> integer; and entering "fifty five" will cause it to flip out). I have seen many not-so-elegant ways to hand this, such as getlining a string and using sstream to convert it to a number, or looping with cin.fail() and clearing the stream and reentering it, etc. Is there any library or anyway to overload the inserter operator to make cin automatically typesafe?

© Stack Overflow or respective owner

Related posts about c++

Related posts about cin