uint8_t不接受两位数字输入 [英] uint8_t does not take two digit inputs

查看:199
本文介绍了uint8_t不接受两位数字输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试代码

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

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

When I execute (with my inputs), this is what I get

1
Input is 1
2
Input is 2
12
Input is 1
Input is 2
0
Input is 0
3  
Input is 3

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

Problem1: It takes input 12 as two separate inputs

问题2:a == 0无效的情况

Problem2: Condition if a==0 doesn't work

可能是什么问题?

推荐答案

uint8_tunsigned chartypedef.这意味着将从cin中读取一个字符.

uint8_t is a typedef for an unsigned char. This means that one character will be read from cin.

读取"0"时,它实际上是字符'0'的ascii值,通常为48,该值不为零,因此相等性检查失败.

When "0" is read it is actually the ascii value of the character '0' commonly 48, which is not zero hence the equality check fails.

这篇关于uint8_t不接受两位数字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆