如何创建不接受浮点值的整数验证函数
How to make an integer validation function that does not accept floating point values?
getRegionTotal()
是我现在用于验证的函数。它的效果很好,因为如果用户输入"二十"或 -7 之类的内容,它不会接受,它会继续要求新值,直到它得到一个有效的值。但是,如果用户输入 60.7 作为北部地区的事故数量,它将接受 60 并删除 .7 部分。然后,当它询问南部地区的事故数量时,它将给出常规指示和更具体的指示。
//These will hold the number of accidents in each region last year
int northTotal = 0;
int southTotal = 0;
int eastTotal = 0;
int westTotal = 0;
int centralTotal = 0;
//passing 0 for northTotal, southTotal etc. because main doesn't know
//values of them until the function returns a value. When it returns a value
//it will go into the variables on the left. getRegionTotal will get the number
//of accidents for a region from the user and prompt the user using the string that
//is in the first argument.
northTotal = getRegionTotal("North", northTotal);
southTotal = getRegionTotal("South", southTotal);
eastTotal = getRegionTotal("East", eastTotal);
westTotal = getRegionTotal("West", westTotal);
centralTotal = getRegionTotal("Central", centralTotal);
int getRegionTotal(string regionName, int regionTotal)
{
//instructs user to enter number of accidents reported in a particular region
cout << "nNumber of automobile accidents reported in " << regionName << " " << cityName << ": ";
//while regionTotal is not an integer or regionTotal is negative
while (!(cin >> regionTotal) || (regionTotal < 0) )
{
//give user more specific instructions
cout << "nPlease enter a positive whole number for the number ofn";
cout << "automobile accidents in " << regionName << " " << cityName << ": ";
cin.clear(); //clear out cin object
cin.ignore(100, 'n'); //ignore whatever is in the cin object
//up to 100 characters or until
// a new line character
}
//returns a valid value for the number of accidents for the region
return regionTotal;
}
解析整行并确保已使用整行。
使用iostreams:
#include <iostream>
#include <sstream>
#include <string>
for (std::string line; std::getline(std::cin, line); )
{
std::istringstream iss(line);
int result;
if (!(iss >> result >> std::ws && iss.get() == EOF))
{
// error, die. For example:
std::cout << "Unparsable input: '" << line << "'n";
continue;
}
// else use "result"
}
使用标准翻译:
#include <errno>
#include <cstdlib>
char const * input = line.c_str(); // from above, say
char * e;
errno = 0;
long int result = std::strtol(input, &e, 10);
if (e == input || *e != ' ' || errno != 0)
{
// error
}
这两种方法基本上是相同的,但前者可能更"惯用C++"。也就是说,如果您已经有一个现有的字符串,strtol
-方法是一个很好的选择,因为它为您提供了精确的错误处理:您是否消耗了整个字符串(如果没有,则e
指向下一个字符(;您是否使用了任何字符串(如果没有,则e
指向开头(;是否有溢出或下溢(检查errno
(。另一方面,iostreams 方法允许您使用尾随空格(感谢 >> std::ws
(,而 strtol
解决方案则没有。
还有std::stol
它包装了strtol
(同样适用于strtoull
/strtod
等(,但它在错误时抛出异常,我相信异常不是构建正常行为控制流的正确工具,例如读取用户输入。此外,您无法控制这些包装器的操作方式;例如,即使他们不使用整个字符串(但不告诉你他们走了多远(,也成功了,并且您无法指定数字基数。
相关文章:
- 添加存储在向量中的大整数的函数出现问题
- C++模板函数,用于比较任何无符号整数和有符号整数
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 整数键映射到头文件中的成员函数指针
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 反转整数的一部分(一半)的函数
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- constexpr log10 整数函数
- 如何检测除整数以外的任何内容是否传递给我的类构造函数?
- 如何使用 binary_search STL 函数在嵌套类中搜索整数?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- C++:为什么我的掷骰子函数在掷骰子数量时只返回偶数整数?
- 从函数参数 [C++] 复制整数数组
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 模函数,避免C++整数溢出
- 通过规范化 // 函数来调整数组
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 没有返回值的整数函数