我正在开发一个小程序,它使用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,但是它退出了函数并转移到下一个请求。
试着重新安排,所有的事情似乎都解决不了。任何帮助都非常感谢
发布于 2014-04-25 11:07:04
您想要做的是对行的其余部分进行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";
}
}https://stackoverflow.com/questions/23289927
复制相似问题