You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++初学者求助:如何实现用户输入数组元素替代硬编码初始化?

Hey there! Let's fix that issue you're having with swapping out the hardcoded array for user input. The error you're seeing happens because in standard C++, you can't declare an array without specifying its size (either explicitly or via an initializer list) and then try to populate it later. Since you know you need exactly 5 values, here are two straightforward solutions tailored for a beginner:

Solution 1: Fixed-Size Array (Simple & Direct)

Since you're only needing 5 elements, just declare the array with an explicit size of 5 upfront. Then loop through each index to read user input into the array. Here's your modified main() function:

int main() {
    const int ARRAY_SIZE = 5;
    int arr[ARRAY_SIZE]; // Explicitly define the array's size
    
    cout << "Please enter 5 integers (separate with spaces or press enter after each):\n";
    for (int i = 0; i < ARRAY_SIZE; ++i) {
        cin >> arr[i]; // Read each value into the array
    }
    
    int n = ARRAY_SIZE; // Or keep using sizeof(arr)/sizeof(arr[0]) if you prefer
    findTriplets(arr, n);
    return 0;
}

This works because we tell the compiler exactly how much memory to allocate for the array right away. No more errors from undefined array size!

Solution 2: Use std::vector (Flexible for Future Changes)

If you think you might want to adjust the number of input values later, using a std::vector is a better habit to pick up early. Vectors handle dynamic sizing automatically, which makes them more versatile than fixed arrays. Here's how to adjust your full code:

First, update the findTriplets function to accept a vector (it's cleaner than passing a raw array):

#include <vector> // Don't forget to include this header!

// Modified function to take a vector of integers
void findTriplets(const vector<int>& arr) {
    bool found = false;
    int n = arr.size(); // Get the vector's size directly
    for (int i = 0; i < n - 1; ++i) {
        unordered_set<int> s;
        for (int j = i + 1; j < n; ++j) {
            int x = -(arr[i] + arr[j]);
            if (s.find(x) != s.end()) {
                printf("%d %d %d\n", x, arr[i], arr[j]);
                found = true;
            } else {
                s.insert(arr[j]);
            }
        }
    }
    if (!found) {
        cout << " No Triplet Found" << endl;
    }
}

Then update main() to use the vector:

int main() {
    const int INPUT_COUNT = 5;
    vector<int> arr(INPUT_COUNT); // Create a vector with 5 elements
    
    cout << "Please enter 5 integers:\n";
    for (int i = 0; i < INPUT_COUNT; ++i) {
        cin >> arr[i];
    }
    
    findTriplets(arr);
    return 0;
}

Quick Note

Don't forget to add #include <vector> at the top of your code if you go with the vector solution—otherwise the compiler won't recognize std::vector.

Either approach will solve your problem. If you're just starting out, the fixed-size array is easier to wrap your head around, but getting comfortable with vectors early will pay off big time in more complex C++ projects!

内容的提问来源于stack exchange,提问作者user9229888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:31:49