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

C++未声明标识符'data'问题及sums()函数编程实现问询

Fixing the "use of undeclared identifier 'data'" Error & Implementing the sums() Function

Hey there! Let's work through your problem together. First, that use of undeclared identifier 'data' error is super common—it just means you're trying to use a variable called data (like an array) that hasn't been declared in the part of the code where you're referencing it. Maybe you forgot to define the array before using it, or there's a typo in the variable name. No worries, we'll fix that as part of the full solution.

Below is a complete, working C++ program that meets all your requirements: a sums() function to calculate positive/negative sums and their counts, plus a main program that reads input values until 0 is entered.

Complete Working Code

#include <iostream>
using namespace std;

// Calculates sums and counts of positive/negative values in an array
// Uses reference parameters to return the four results directly
void sums(float arr[], int numElements, float &positiveSum, float &negativeSum, int &positiveCount, int &negativeCount) {
    // Initialize all result values to 0
    positiveSum = 0.0f;
    negativeSum = 0.0f;
    positiveCount = 0;
    negativeCount = 0;

    // Loop through each element in the array
    for (int i = 0; i < numElements; ++i) {
        if (arr[i] > 0) {
            positiveSum += arr[i];
            positiveCount++;
        } else if (arr[i] < 0) {
            negativeSum += arr[i];
            negativeCount++;
        }
        // Ignore 0 values since we stop input when 0 is entered
    }
}

int main() {
    const int MAX_SIZE = 10;
    float inputArray[MAX_SIZE];
    int elementCount = 0;
    float userInput;

    cout << "Enter real numbers (enter 0 to stop, max 10 values):\n";
    while (elementCount < MAX_SIZE) {
        cin >> userInput;
        if (userInput == 0.0f) {
            break; // Exit loop when user enters 0
        }
        inputArray[elementCount] = userInput;
        elementCount++;
    }

    // Variables to store results from sums()
    float posSum, negSum;
    int posCount, negCount;

    // Call the sums function to compute values
    sums(inputArray, elementCount, posSum, negSum, posCount, negCount);

    // Print out the results
    cout << "\n--- Calculation Results ---\n";
    cout << "Sum of positive values: " << posSum << "\n";
    cout << "Number of positive values: " << posCount << "\n";
    cout << "Sum of negative values: " << negSum << "\n";
    cout << "Number of negative values: " << negCount << "\n";

    return 0;
}

Key Details to Note

  1. Fixing the undeclared identifier error:

    • In the code above, we declare the array inputArray before using it in the input loop and when passing it to sums(). If you were using data instead, make sure you declare it first (e.g., float data[10];) before trying to access data[i].
  2. How the sums() function works:

    • We use reference parameters (float &positiveSum, etc.) so the function can directly modify the variables in main()—this is a clean way to return multiple values without using pointers or structs.
    • We initialize all result variables to 0 at the start to avoid garbage values.
    • We skip 0 values since the input loop stops when 0 is entered, so 0 won't be stored in the array (but we still handle it just in case).
  3. Main program behavior:

    • Reads up to 10 real numbers, stopping immediately when the user enters 0.
    • Calls sums() with the array, the actual number of elements entered, and the reference variables to store results.
    • Prints a clear summary of the calculations.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:33:58