C ++:检查它是否是类中的数字
c++ : check if it is a Number in class
在此代码中,我想知道两个数字是Int 还是字符串:
#include <iostream>
using namespace std;
class calculate{
private:
bool checkNumbers(int num1, int num2){
if(isdigit(num1) && isdigit(num2)){
return true;
}else{
return false;
}
}
public:
void sum(int x, int y){
bool chek=checkNumbers(x, y);
if(chek==true){
cout<< "yes it is Number"<< endl;
}else{
cout<< "Nope! String"<<endl;
}
}
};
int main()
{
calculate calulate;
calulate.sum(3, 2);
return 0;
}
但是在运行代码之后,我只看到Nope!字符串,表示它是字符串。谁知道出了什么问题? 我用isdigit检查了数字,我只发送了两个数字
calulate.sum(3, 2);
但是什么都没有!! 谢谢
您的问题在于您对std::isdigit(int)
的使用
该函数旨在检查int
是否等效于 10 个char
表示的十进制数字之一,0123456789
。但是,此函数仅适用于 48-57int
值。
int
应为char
的整数值。如果你想让它解析true
,使用 48-57,其中48
是0
,49
是1
,等等......
请注意,如果你向它传递一个char
,如"3",你的函数自然会解析为 true,就像一个评论者所说的那样。这是因为char(3) == int(51)
.
例如:
isdigit('1'); // This is true, because '1' is a "char" digit
isdigit(49); // This is true
isdigit(1); // This is false
isdigit(60); // This is false
num1
和num2
被std::isdigit
解释为字符。通常,这意味着 ASCII 字符。
作为 ASCII 字符,2 和 3 是控制字符而不是数字,它们在"0"(0x30(到"9"(0x39(的范围内
相关文章:
- C ++:检查它是否是类中的数字
- 有没有办法检查用户输入是否是数字?
- 如何知道文本文件中的输入是否是 C++ 中的有效数字
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何以更有效的方式检查一个数字是否是素数?
- 编写一个函数,用递归函数检查数字是否是正方形
- 如何判断变量是否是一个复杂的数字C
- 这是确定两个数字是否具有相同奇偶校验的正确方法
- 更好的算法来检查一个数字是否既不是素数也不是单个素数的幂
- 如何在不使用数学的情况下知道一个数字是否是回文
- 检查数字是否是素数,哪个是质数(C ++)
- 我可以检查给定的数字是否可以是具有n个术语的任何算术进程的总和
- C++检查从文件中读取的最后内容是否是数字
- 检查数字是否是素数
- 我正在尝试做一个循环,检查输入是否是一个数字,如果没有再次询问
- 查找一个数字是否是其他一些数字的公共倍数
- 检查数字是否是C++中的回文
- 我需要看看两个数字是否是倍数
- 检查数字是否是自同构的
- 使用不同的函数(C++)测试一个数字是否是回文