输入一个字母给'int'
Input a letter to 'int'
代码:
int a;
cin>>a;
cout<<a<<endl;
那我就用g++ test.cpp
,运行吧。然后我给变量a输入一个字母'b'。输出为 0。
但是,当我测试其他代码时:
cout<<int('b')<<endl; // output: 98
为什么?有什么不同?
您尝试进行的输入操作失败。由于 a
是一个 int
,因此 cin
需要一个 int
。因为它得到一个 char
它失败了。您可以通过将代码更改为:
来对此进行测试
int a;
cin>>a;
if(!cin)
cout << "input failed";
else
cout<<a<<endl;
输入:
a
输出:
input failed
看到这个live example
当您使用 'b' 来计算一个整数值时,您的程序认为您输入了一个无效数字,因此它不会通过 int('b') 赋值。仍然使用 a
的初始值
不确定这是否是您所发现的:
char a;
cin >> a;
cout << a << endl;
尝试将 int
更改为 char
,
当你输入3
时,它会输出3
;
当你输入 b
时,它会输出 b
;
如果你想输出ascii,你可以让cout像:
cout << (int) a << endl;
然后输入b
会得到98
std::cin
是一个对象,std::istream
的一个实例。 std::istream
已重载 >>
以支持 a variety of types。其中一种类型是 &int
。当>>
左边有一个std::istream
,右边有一个整型引用时,就调用方法istream& operator>> (int& val)
。该方法的概念性实现如下。
- 将 0 存入累加器
- 读取输入的一个字符
- 如果字符是0-9,将其十进制值加到累加器
- 如果不是,return累加器的值
- Return 到第 2 步
当您提供 'b' 作为 istream& operator>> (int& val)
的输入时,它会立即将 "accumulated" 0 存储在您的 int
变量中。示例:
#include <iostream>
int main (const int argc, const char * const argv[]) {
int b = 100;
std::cout << b << std::endl;
std::cin >> b;
std::cout << b << std::endl;
return 0;
}
执行:
100
b
0
至于转换,当您将值 'b'
转换为整数时,内存中已经有一个值为 98 的字节,然后将其打印为整数。当您使用 >>
时,内存中的结果值为 0,然后您将其打印为整数。
If extraction fails (e.g. if a letter was entered where a digit is
expected), value is left unmodified and failbit is set. (until C++11)
If extraction fails, zero is written to value and failbit is set. If
extraction results in the value too large or too small to fit in
value, std::numeric_limits::max() or std::numeric_limits::min()
is written and failbit flag is set. (since C++11)
来自 https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
代码:
int a;
cin>>a;
cout<<a<<endl;
那我就用g++ test.cpp
,运行吧。然后我给变量a输入一个字母'b'。输出为 0。
但是,当我测试其他代码时:
cout<<int('b')<<endl; // output: 98
为什么?有什么不同?
您尝试进行的输入操作失败。由于 a
是一个 int
,因此 cin
需要一个 int
。因为它得到一个 char
它失败了。您可以通过将代码更改为:
int a;
cin>>a;
if(!cin)
cout << "input failed";
else
cout<<a<<endl;
输入:
a
输出:
input failed
看到这个live example
当您使用 'b' 来计算一个整数值时,您的程序认为您输入了一个无效数字,因此它不会通过 int('b') 赋值。仍然使用 a
的初始值不确定这是否是您所发现的:
char a;
cin >> a;
cout << a << endl;
尝试将 int
更改为 char
,
当你输入3
时,它会输出3
;
当你输入 b
时,它会输出 b
;
如果你想输出ascii,你可以让cout像:
cout << (int) a << endl;
然后输入b
98
std::cin
是一个对象,std::istream
的一个实例。 std::istream
已重载 >>
以支持 a variety of types。其中一种类型是 &int
。当>>
左边有一个std::istream
,右边有一个整型引用时,就调用方法istream& operator>> (int& val)
。该方法的概念性实现如下。
- 将 0 存入累加器
- 读取输入的一个字符
- 如果字符是0-9,将其十进制值加到累加器
- 如果不是,return累加器的值
- Return 到第 2 步
当您提供 'b' 作为 istream& operator>> (int& val)
的输入时,它会立即将 "accumulated" 0 存储在您的 int
变量中。示例:
#include <iostream>
int main (const int argc, const char * const argv[]) {
int b = 100;
std::cout << b << std::endl;
std::cin >> b;
std::cout << b << std::endl;
return 0;
}
执行:
100
b
0
至于转换,当您将值 'b'
转换为整数时,内存中已经有一个值为 98 的字节,然后将其打印为整数。当您使用 >>
时,内存中的结果值为 0,然后您将其打印为整数。
If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. (until C++11)
If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits::max() or std::numeric_limits::min() is written and failbit flag is set. (since C++11)
来自 https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt