首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于cin.fail的CIN验证

基于cin.fail的CIN验证
EN

Stack Overflow用户
提问于 2014-04-25 10:02:05
回答 1查看 1.3K关注 0票数 1

我正在开发一个小程序,它使用std::cin一个接一个地要求4个整数。我使用一个函数来请求整数,并传入允许作为参数的最大值。若要检查该值是否为整数,请使用std::cin.fail。调用函数的代码如下所示。

代码语言:javascript
复制
    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);

验证函数代码如下所示。

代码语言:javascript
复制
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.
}

如果用户只输入一个整数,或者只输入一个字母,它就会正常工作。我遇到的问题是,如果用户输入了34d22p。我得到了一个错误:characters are not permitted,但是它退出了函数并转移到下一个请求。

试着重新安排,所有的事情似乎都解决不了。任何帮助都非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-25 11:07:04

您想要做的是对行的其余部分进行ignore,即使您成功了:

代码语言:javascript
复制
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.
}

如果你认为这是重复的,你可以这样做:

代码语言:javascript
复制
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";
  }
}

代码语言:javascript
复制
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";
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23289927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档