使用 ISdigit 通知任何检测到的非法字符
using isdigit to inform any detected illegal characters
我在使用 isdigit 时遇到问题
#include <iostream>
using namespace std;
int main() {
int x = 0;
int y = 1;
int ndCount[10];
for (x = 0; x < 10; x++,y++) {
cout << y << ". Enter a whole number: ";
cin >> ndCount[x];
if (!isdigit(ndCount[x])) {
cout << "Error, Please try again n";
}
}
return 0;
}
每当我尝试输入数字时,它仍然会注册为"错误请重试"。 另外,当我尝试使用字符时,它会自动出错
要检查用户输入的数字是否有效,请执行以下操作:
if(!(cin >> ndCount[x]))
throw std::runtime_error("not a number");
operator>>
设置无法分析值时的流failbit
。然后operator!
检查是否已设置failbit
。
您将值存储在需要将其存储在char
int
中,请尝试char ndCount[10];
这将检查您在控制台上输入的字符串上的每个数字。
当然,如果你想检查一个整数,你需要一种不同的方法。
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 使用 ISdigit 通知任何检测到的非法字符
- 建筑物32位openssl fips(nmake f ms ntdll.mak):宏中的非法字符