结束数组输入(C++ 行业标准)

Ending array input (C++ industry-standards)

我编写的代码可以正常工作,但我觉得可能有更好的标准方法来结束我可能不知道的数组输入。有问题的代码在 getInputForArray() 中,如果在整数输入上检测到 cin.fail() ,则通过停止循环来实现。有没有更好的办法?谢谢大家

#include <iostream>
using namespace std;

int sumOfArray(int arr[], int arrlen);
void getInputForArray(int arr[], int arrlen);

int main() {
    const int LIST_SZ = 256;
    int mylist[LIST_SZ];
    // Zero out the array
    for (int i = 0; i < LIST_SZ; i++)
        mylist[i] = 0;
    // Get user input for array
    getInputForArray(mylist, LIST_SZ);
    // Print the sum of all the numbers in the array
    cout << "\nThe sum is: " << sumOfArray(mylist, LIST_SZ) << endl;
    return 0;
}

int sumOfArray(int arr[], int arrlen) {
    int total = 0;
    for (int i = 0; i < arrlen; i++)
        total = arr[i] + total;
    return total;
}

void getInputForArray(int arr[], int arrlen) {
    cout << "Input any non-numerical value to denote the end of your input\n";
    for (int i = 0; i < arrlen; i++) {
        if (!cin.fail()) {
            cout << "Please input a value for the [" << i
                 << "] position of the array: ";
            cin >> arr[i];
        }
    }
}

你可以尝试这样的事情。

void getInputForArray(int arr[], int arrlen) {
    char c = 0;
    size_t size = 0;
    cout << "Press escape (ESC) to stop inputting values" << endl;

    while(size < arrlen) {
        cout << "Enter a number for position " << size << " of the array: ";
        cin >> arr[size++];

        fflush(stdin);
        c = getchar();
        if(c == 27) { // check for the escape key
            break; // jump out of the loop
        }

        // Check non-numeric values.
        if(cin.fail()) {
            cout << "Error: numerical values only!" << endl;
            cin.clear(); // clear the input
            size--; // go back to previous position
        }
    }
}

或者,您可以通过将检查转义键的行 if(c == 27) 更改为 if(c == EOF) 来检查 EOF(文件结尾)。 EOF 是一个宏,因为它的值取决于系统。但是,我不推荐这种替代方法,因为每个系统的按键顺序都不同。 Ctrl+Z 用于基于 DOS 或 CP/M 的系统,Ctrl+D 用于类 Unix 系统,Ctrl+\ 用于 AmigaOS,可能还有其他系统。

其他答案解决了 EOF(或输入结束)选项。

OP 可以修改用户输入检查以在输入两次非数字输入时停止循环。我还考虑使用 std::vector 而不是原始数组的更好做法。

这是一个可能的实现:

std::vector<int> getDataFromInput( size_t max_data )
{
    using std::cout;

    std::vector<int> v;
    std::string value;

    bool stop_reading = false;

    while ( v.size() < max_data )
    {
        cout << "Please input a value for position [" << v.size() << "] of the vector: ";
        // input a string, if End of Input, stop reading
        if ( !(std::cin >> value) ) break;
        try
        {   // if the user entered a valid number, add it to the vector
            int num = stoi(value);
            v.push_back(num);
            stop_reading = false;
        }
        catch ( const std::invalid_argument &e )
        {
            if ( stop_reading ) break;
            cout << "You entered a non-numerical value.\nPlease, enter another "
                 << "non-numerical value to stop input or a number to continue\n";
            stop_reading = true;
        }
        catch ( const std::out_of_range &e )
        {
            cout << "The value entered can't be represented by an int.\n";
        }
    }

    return v;
}

编辑

正如 Lightness races in orbit 指出的:

It's more "industry standard" to stop input by sending EOF. You can usually do that from terminal with ^D (Linux/Mac) or ^Z (Windows).

因此您可以将我之前的代码片段简化为:

std::vector<int> getDataFromInput( size_t max_data )
{
    using std::cout;

    std::vector<int> v;
    std::string value;

    while ( v.size() < max_data )
    {
        cout << "Please input a value for position [" << v.size()
             << "] of the vector: ";
        if ( !(std::cin >> value) ) break;
        try
        {
            int num = stoi(value);
            v.push_back(num);
        }
        catch ( const std::invalid_argument &e )
        {
            cout << "You entered a non-numerical value.\n";
        }
        catch ( const std::out_of_range &e )
        {
            cout << "The value entered can't be represented by an int.\n";
        }
    }

    return v;
}