Diffrence between cin and assignment
Consider the following 2 code snippets:
Case 1:
#include <iostream>
int main()
{
int i=0;
char c='a';
i=c;
cout << i << endl; //Retuens ASCII value of 'a'
return 0;
}
Case 2:
#include <iostream>
int main()
{
cout << "Enter integer value" << endl;
int i=-1;
cin >> i; //Assume user enters 'a'
cout << i << endl; //prints -1 on screen
return 0;
}
In Case 1, when we use assignment the ASCII equivalent of 'a'
is assigned to int i
, but in Case 2 int i
is -1
. Why is the behavior different in both the cases? Is it by design? Is it possible (with standard functions) to input ASCII value using cin
when characters are input for integer variables?
// I understand that cin is failing. What I wou开发者_StackOverflow社区ld like to know is: why cin fails when char is entered, if assignment properly assigns ascii value?
If you enter 'a'
, then cin >> i
fails, because type of i
is int
. So what it prints is just garbage value.
You can check this by writing this:
if ( cin >> i )
{
cout << i << endl; //on successful read this will be printed!
}
else
{
cout << "cannot read 'a' from input stream";
}
It will print this:
cannot read 'a' from input stream
When the user enters 'a'
, the operator implementation tries to convert this to an integer, and fails.
In response to the comment (which seems to be the main point of your question):
yes, I understand that cin is failing. What I would like to know is: why cin fails when char is entered, if assignment properly assigns ascii value?
'a'
is not "an ASCII value". In the C language, it is a character literal.
But cin
doesn't know what a character literal is. As far as cin
can tell, the user entered this:
const char* inputValue = "'a'";
cin
is not a C++ compiler. It is much simpler, and you might argue much stupider. When you tell it to stream in an integer, it is going to stream in an integer (and only an integer). If that integer happens to correspond to a character in the ASCII table, so be it, but the user must enter it as 85
.
The fact that the stream input operators are "stupid" is actually a good thing, because:
- They execute faster
- It is simpler for an experienced programmer to understand what goes on under the hood
- You can be darn sure the user entered a number (fewer bugs)
The stream input operators (>>
) aren't just used for console input. They are also used for file input, which needs to be fast, and secure.
If you want to get the ASCII-code of a character you read from cin
, just read into a char
and cast to int
afterwards:
char c;
if(cin >> c) {
cout << static_cast<int>(c) << std::endl;
}
At first example you call operator<< with value type "int". char c='a';
In c you have a-symbol. When you call i=a;
i now is ASCII-code of symbol(convert from char to int). So cout << i output ASCII code.
At second example i - integer, when you call cin >> i
, it want to read integer. To read some character you can use smth like this
char c;
cin >> c;
Now in c will be code of your input character
i believe the reason it fails is that cin >>
attempts to format for you; so, if you enter "10" (the string) on stdin
, it will be able to put that into an int. if you enter "a" and try to put it to an int
, it gives an error because "a" is not a valid number.
think of using cin >>
more as doing input formatting without format strings and with more robust error handling; like a scanf that when you passed "%s" to it would fail if you tried to use an int as the variable to take your string parameter.
精彩评论