我正在开发一个小程序,使用std::cin
一个接一个地请求4个整数。我正在使用一个函数来请求整数,并传入允许作为参数的最大值。要检查值是否为整数,请使用std::cin.fail
。调用函数的代码如下所示。
cout << "Specify source number (1 - 1024)\n";
source = validate(1024);
cout << "Specify destination number (1 - 1024)\n"; // For all except data, the value is equal to the value returned by the validate function
destination = validate(1024); // The maximum possible value is passed in as an argument in each of the cases.
cout << "Specify type number (1 - 10)\n"; // User is prompted to enter the desired values.
type = validate(10);
cout << "Source port number (1 - 1024)\n";
port = validate(1024);
,验证功能代码如下所示。
int validate(int max) {
int value; // Initialise variable to hold the value.
for (;;) { // Loop forever until correct input is entered.
cin >> value;
if (cin.fail()) { // If cin fails to receive a value matching the declared data type.
cout << "Characters are not permitted.\n"; // Notify the user of error.
cin.clear(); // Clear the error flag within cin.
cin.ignore(10000,'\n'); // Ignore the newline character in the buffer to prevent an infinite loop.
}
else if (value > max || value == 0) {
cout << "The value you have entered is not valid, please enter a number between 1 and " << max << "\n";
cin.clear(); // Clear the error flag within cin.
cin.ignore(10000, '\n'); // Ignore the newline character in the buffer to prevent an infinite loop.
}
else
break;
}
return value; // Return the validated value.
}
如果用户只输入一个整数,或只是一个字母,它可以正常验证,这可以正常工作,我遇到的问题是,如果用户输入例如34d
或22p
。我收到错误characters are not permitted
,但随后退出该函数并转到下一个请求。
尝试重新排列,所有种类似乎无法解决它。 任何帮助非常感谢
答案 0 :(得分:0)
即使您成功,您要做的是ignore
其余部分:
int validate(int max) {
int value; // Initialise variable to hold the value.
for (;;) { // Loop forever until correct input is entered.
cin >> value;
if (cin.fail()) { // If cin fails to receive a value matching the declared data type.
cout << "Characters are not permitted.\n"; // Notify the user of error.
cin.clear(); // Clear the error flag within cin.
cin.ignore(10000,'\n'); // Ignore the newline character in the buffer to prevent an infinite loop.
}
else if (value > max || value == 0) {
cout << "The value you have entered is not valid, please enter a number between 1 and " << max << "\n";
cin.clear(); // Clear the error flag within cin.
cin.ignore(10000, '\n'); // Ignore the newline character in the buffer to prevent an infinite loop.
}
else {
cin.ignore(10000, '\n'); // Ignore the newline character in
break;
}
}
return value; // Return the validated value.
}
如果你认为这太重复了,你可以这样做:
struct ignore_till_eol {
ignore_till_eol(std::istream& cin): input(cin) {}
~ignore_till_eol() { input.clear(); input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }
std::istream& input;
};
int validate(int max_value) {
for(;;) {
ignore_till_eol ignore(cin);
int v;
if( cin >> v && 0 < v && v <= max_value ) return v;
if( cin )
cout << "Value is invalid, must be between 1 and " << max_value << "\n";
else
cout << "Invalid characters in input\n";
}
}
或
int validate(int max_value) {
for(;;) {
ignore_till_eol ignore(cin);
int v;
if( !(cin >> v) )
cout << "Invalid characters in input\n";
else if( 0 < v && v <= max_value )
return v;
else
cout << "Value is invalid, must be between 1 and " << max_value << "\n";
}
}