Character input doesn’t give an error

I was writing an extremely basic code of taking input from user and printing the output. int a, b; cout<<"Enter any two integers"<<endl; cin>>a>>b; cout<<a<<b; return 0; I fooled around a bit and tried inserting a character instead of an integer to see what kind of error message will pop up. For instance I inserted a=1 and b=w But it instead printed an error free result: 10 I tried variants of it and learned that it replaces characters with zeroes. Why? Why doesn’t it display an error?

11/17/2019 3:16:31 PM


6 Answers

New Answer


https://code.sololearn.com/c08p7DN1H4aJ/?ref=app The failbit will be set, if the input stream doesn’t contain an integer value.


I extended my code: If the failbit is set, the input stream can be read again. The fail state has to be cleared for that purpose. https://code.sololearn.com/c08p7DN1H4aJ/?ref=app


Thank you for answering! I’m not yet familiar with the “failbit” concept but your code made things clear.


Yes, this was helpful as well. Thanks again.


You have declare b as integer . So when you input b , compiler do casting character b into its ascii code which you will get on output screen. And it must be 98 right?