【问题标题】:C++ How to check the number of the input from ShellC ++如何检查Shell的输入数量
【发布时间】:2015-04-03 18:56:11
【问题描述】:
void test() {
    int i ,j;
    cout << "enter the i and j" << endl;
    cin >> i >> j;
    if (j <= 5 && j > 0 && i > 0 && i <= 9) {
        cout << "right" <<endl;
    } else {
        cout << "error" << endl;
        test();
    }
}

int main(int argc, const char * argv[]) {
    test();
}

如何检查命令行输入是否完全正确?

下面是一些错误测试,我们应该运行 else 部分中的代码。

富ags

但是命令行中的结果是28行错误信息。 但我想要的只是一行代码显示“错误”

有什么问题?

另一个

下面是我的 C++ 代码:

void test(int array[], int length) {
    int index;  // the index of heap array that human want to modify
    int num;  // the number of heap in the index position
    cout << "input the index and num" << endl << flush;
    string si,sj;
    try{
        cin >> si >> sj;
        index = stoi(sj);
        num = stoi(si);
    }catch(std::exception e){
        cout << "error, try again" << endl;
        test(array, length);
    }
    if (index <= length && index > 0 && num > 0 && num <= array[index - 1]) {
        array[index - 1] -= num;
        // print(array, length);
    } else {
        cout << "error, try again" << endl;
        test(array, length);
    }
}

现在有一个shell来运行这段代码,但是在shell中,存在如下输入:

输入索引和数字 2 1

这是正确的

输入索引和数字2

它只有 1 个值,程序在这里阻塞等待另一个输入,我应该弄清楚并输出“错误,再试一次”

输入索引和数字 1 2 3

这也是不正确的,因为有超过 2 个输入值。同样,我应该弄清楚并输出“错误,重试”

如何处理?

【问题讨论】:

  • 首先检查一下:cin &gt;&gt; i &gt;&gt; j; 你怎么知道这些提取成功了?如果他们没有没有,接下来的行的后果是什么?我> ?请注意,如果提取失败,您将陷入兔子洞,陷入无限循环,因为cin 永远不会清除其错误状态。
  • 您可以组合ala if (cin &gt;&gt; i &gt;&gt; j &amp;&amp; j &lt;= 5 &amp;&amp; j &gt; 0 &amp;&amp; i &gt; 0 &amp;&amp; i &lt;= 9) { ...,因此只有在输入解析也有效时才会输入if。除非您先致电cin.clear(),否则再次致电test() 将不起作用;然后cin.ignore(some-bloody-big-number-from-numeric-limits); - 只需谷歌std::cin.ignore,您就会在示例中找到该死的大数字;-P。

标签: c++ shell command cin


【解决方案1】:

cin &gt;&gt; i &gt;&gt; j; 只是跳过前导空格,然后读取两个由空格分隔的格式化int 值。如果您在示例中输入更多内容,则其余部分将保留在输入流缓冲区中。如果您再次调用test()cin 将从该缓冲区读取。

您可以使用cin.ignore(numeric_limits&lt;streamsize&gt;::max()) 来解决该问题,因为它会清除缓冲区。

【讨论】:

    【解决方案2】:

    首先,您需要将输入读取为字符串,然后使用 std::stoi 检查错误将字符串转换为 int。假设您只需要“错误”消息,请使用 try-catch 块仅捕获 std::exception 并输出“错误”。 其次,要在出错的情况下重复调用 test(),您需要使用某种 lopp 并且不要从 test() 内部调用 test()。这种技术称为递归调用,用于其他目的,而不是简单的重复。 我已经修改了你的 test() 函数以返回 bool 值,它会在成功的情况下返回 true,在错误的情况下返回 false,然后从 while() 循环中调用它,如果返回 false,它将重复调用。 关于第二个问题,您需要分别输入数字,每个数字都在不同的行上。该程序将能够单独检查每个数字。见代码:

    #include <string>
    #include <iostream>
    bool test() {
        int i ,j;
        std::string si,sj;
        try{
            cout << "enter i:" << endl;
            cin >> si;
            i = std::stoi(si);
            cout << "enter j:" << endl;
            cin >> sj;
            j = std::stoi(sj);
        }catch(std::exception e){
            cout << "error";
            return false;
        }
        if (j <= 5 && j > 0 && i > 0 && i <= 9) {
            cout << "right" <<endl;
            return true;
        } else {
            cout << "error" << endl;
            return false;
        }
    }
    
    int main(int argc, const char * argv[]) {
        while(!test()){}
    }
    

    【讨论】:

    • 是的,非常感谢,它解决了我的问题,但是如何处理第二个关于输入数量的问题?
    • 如果必须在一行中获取一组数据,您需要:1.从输入中获取这一行,2.使用空格作为分隔符将其拆分为部分,3.检查数量零件并转换为int。您可以在这里找到并选择字符串拆分方法:stackoverflow.com/questions/236129/split-a-string-in-c
    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多