如何检查输入的值是否为数字
How can you check if a value entered is numeric?
#include<iostream>
using namespace std;
int main()
{
double money;
cout << "Input the sum of money: ";
cin >> money;
....
我一直在尝试检查输入的值是否是数值,以便在输入另一个值(字母)时显示错误消息,并且代码将循环回以再次要求输入(钱)
while ( ( cin >> money ) == false )
{
...
}
您可以在输入后检查流的状态。例如
if ( !( std::cin >> money ) ) std::cout << "Oh, I made a mistake!n";
如果你想重复输入,你应该调用
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
不要忘记包含标题<limits>
相关文章:
- 检查输入是否不是整数或数字
- 用于检查数字是否有数字或可被该数字的值整除的程序
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 如何使用递归检查数字是否有重复数字?
- 查找数字是否为 2 的幂的时间复杂度
- 将 C 函数转换为 C++ 以检查数字是否有效
- C ++:检查它是否是类中的数字
- 检查数字是否为素数的算法
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 找到 x^n 的所有组合,并检查它们的总和是否等于一个不包括相同数字的数字
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 有没有办法检查用户输入是否是数字?
- 如何知道文本文件中的输入是否是 C++ 中的有效数字
- C++:这两种将数字写入矩阵的方式之间是否存在显着的速度差异?
- 如何检查cin是否与数组中的一组数字匹配
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- 检查字符串是否是数字,然后将该数字转换为 int?