+4

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

Paige

6 Answers

New Answer

+3

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

+2

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

+1

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

0

Yes, this was helpful as well. Thanks again.

0

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?

-1

https://www.sololearn.com/Discuss/2072312/?ref=app