问题描述
好的,我正在制作一个C ++文件处理项目。它中有这个变量:int roll。
它应该有3位数。我想过使用像if(roll> 99 || roll< 1000)这样的方法,但它没用。它显然是091作为2位数字,003作为1位数字。那么,如果我想将像'001'这样的数字视为3位数字,如何检查输入是否是整数变量'a'的3位数字?
我尝试过:
我想过使用像if这样的方法(roll> 99 || roll< 1000 )但它没用。它显然需要091作为2位数字,003作为1位数字。
Okay, so I am making a C++ file handling project. There's this variable in it: int roll.
It should have 3 digits. I thought of using methods like, if(roll>99||roll<1000) but it's not useful. It takes 091 as a 2 digit number, 003 as 1-digit, obviously. So, How do I check if the input is a 3-digit number for an integer variable 'a', if I want to consider numbers like '001' as 3 digit numbers too?
What I have tried:
I thought of using methods like, if(roll>99||roll<1000) but it's not useful. It takes 091 as a 2 digit number, 003 as 1-digit, obviously.
推荐答案
// Skip any non-digits because strtol will skip leading white spaces
// and accept signs. This won't work for negative numbers.
while (*szInput && !isdigit(*szInput))
++szInput;
char *stop;
long num = strtol(szInput, &stop, 10);
if (stop - szInput == 3)
{
// Has three digits
}
这篇关于如果我想将“001”之类的数字视为3位数字,如何检查输入是否为整数变量“a”的3位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!