我开始C++编程,并且必须做大量的输入验证。我发现这个功能似乎普遍适用,但在某个方面遇到麻烦;如果我输入-90,程序不会给出错误。我的问题是: 1.如何添加输入不能为< = 0的情况? 2.是否有更好的方式来限制用户的输入?也许C++中的库?C++输入验证
感谢您的任何帮助或建议。
#include <ios> // Provides ios_base::failure
#include <iostream> // Provides cin
template <typename T>
T getValidatedInput()
{
// Get input of type T
T result;
cin >> result;
// Check if the failbit has been set, meaning the beginning of the input
// was not type T. Also make sure the result is the only thing in the input
// stream, otherwise things like 2b would be a valid int.
if (cin.fail() || cin.get() != '\n')
{
// Set the error state flag back to goodbit. If you need to get the input
// again (e.g. this is in a while loop), this is essential. Otherwise, the
// failbit will stay set.
cin.clear();
// Clear the input stream using and empty while loop.
while (cin.get() != '\n')
;
// Throw an exception. Allows the caller to handle it any way you see fit
// (exit, ask for input again, etc.)
throw ios_base::failure("Invalid input.");
}
return result;
}
使用
inputtest.cpp
#include <cstdlib> // Provides EXIT_SUCCESS
#include <iostream> // Provides cout, cerr, endl
#include "input.h" // Provides getValidatedInput<T>()
int main()
{
using namespace std;
int input;
while (true)
{
cout << "Enter an integer: ";
try
{
input = getValidatedInput<int>();
}
catch (exception e)
{
cerr << e.what() << endl;
continue;
}
break;
}
cout << "You entered: " << input << endl;
return EXIT_SUCCESS;
}
用于指定T的有效范围的可选参数? – crashmstr 2014-09-04 12:54:57
您在'getValidatedInput'中的错误处理是错误的。如果输入来自文件(由于重定向)并且文件已结束,该怎么办? – 2014-09-04 12:58:09
'-90'是一个有效的'int',所以你的函数失败会有些令人吃惊......据说,有很多方法可以解析* specific *输入以获得正确性。至于“通用验证”,我甚至不知道为什么你需要这样的功能。阅读输入,检查'cin',你有你的验证... – DevSolar 2014-09-04 12:58:28