uint8_t不需要两位数输入

Nic*_*ick 5 c++ uint

这是我的测试代码

#include<iostream>
using namespace std;
int main() 
{
    uint8_t a;
    while(1)
    {
        cin>>a;
        if(a == 0) break;
        cout<<"Input is "<<a<<endl;
    }
}  
Run Code Online (Sandbox Code Playgroud)

当我执行(使用我的输入)时,这就是我得到的

1
Input is 1
2
Input is 2
12
Input is 1
Input is 2
0
Input is 0
3  
Input is 3
Run Code Online (Sandbox Code Playgroud)

问题1:它将输入12作为两个独立的输入

问题2:条件是否= = 0不起作用

可能是什么问题?

hmj*_*mjd 6

uint8_t是一个typedef用于一个unsigned char.这意味着将读取一个字符cin.

"0"读取时,它实际上是字符的ascii值'0'48,这不是零,因此等式检查失败.

  • `uint8_t v = static_cast <uint8_t>(i);`其中`i`的值是`uint8_t`的有效值(0 - 255). (2认同)