Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 限制控制台中输入的空格_C++_If Statement_Input_While Loop_Cin - Fatal编程技术网

C++ 限制控制台中输入的空格

C++ 限制控制台中输入的空格,c++,if-statement,input,while-loop,cin,C++,If Statement,Input,While Loop,Cin,我有3个cin用于ints int input1; cin >> input; int input2; cin >> input2; int input3 cin >> input3; 问题是,如果我在控制台中键入2 3 4,它将一次性输入所有3个。我怎样才能防止这种情况?如果他们这样做,可能会给他们一个警告。基本上输入验证错误。一种可能的解决方案: int strict_stoi(const string& s) { size_t en

我有3个cin用于ints

int input1;
cin >> input;

int input2;
cin >> input2;

int input3
cin >> input3;
问题是,如果我在控制台中键入2 3 4,它将一次性输入所有3个。我怎样才能防止这种情况?如果他们这样做,可能会给他们一个警告。基本上输入验证错误。

一种可能的解决方案:

int strict_stoi(const string& s)
{
    size_t end_pos;
    int num = stoi(s, &end_pos);
    for (size_t i=end_pos; i<s.length(); ++i)
    {
        if (!isspace(s[i]))
            throw invalid_argument("You have entered some garbage after the number!");
    }
    return num;
}

int read_number()
{
    string s;
    getline(cin, s);
    return strict_stoi(s);
}

int read_number_with_retry(const char* prompt)
{
    for (;;)
    {
        try
        {
            cout << prompt;
            return read_number();
        }
        catch (invalid_argument& ex)
        {
            cout << ex.what() << endl;
        }
    }
}

int test()
{
    int input1 = read_number_with_retry("Enter input #1: ");
    int input2 = read_number_with_retry("Enter input #2: ");
    int input3 = read_number_with_retry("Enter input #3: ");

    return 0;
}
如果您输入一个完全无效的参数,例如a,那么它将显示一条不太用户友好的无效stoi参数消息,但是如果您输入5 6,那么它将显示您在数字!之后输入了一些垃圾!。如果您想用用户友好的方式替换无效的stoi参数消息,那么当您在编号后发现垃圾时,您应该在编号异常后抛出您自己的垃圾,而不是抛出无效的\u参数异常。在这种情况下,您可以区分两个不同的错误:无效的\u参数将是只有在出现无效输入时才会抛出,例如a和arbage\u之后的\u。只有在出现其他类型的错误时才会抛出\u编号,因此您可以捕获两个不同的异常,并且可以在这两种情况下打印完全定制的消息。我将此操作留给您作为额外练习。

您可以执行以下操作:

#include <iostream>
#include <sstream>

int main() {
    while(true) {
        std::cout << "Enter a number [Enter to quit]: ";
        std::string line;
        getline(std::cin, line);
        if(line.empty()) break;
        else {
            std::stringstream input(line);
            int number;
            // Preceding white space number trailing white space:
            input >> number >> std::ws;
            if(input && input.eof()) {
                std::cout
                    << "The number surronded by possible white space is: "
                    << number
                    << '\n';
            }
            else {
                std::cout
                    << "The input line is invalid: "
                    << line
                    << '\n';
            }
        }
    }
}
如果你想严格点:

#include <iostream>
#include <iomanip>
#include <sstream>

...

// Number without preceding and trailing white space:
input >> std::noskipws >> number;

...