C++未声明标识符'data'问题及sums()函数编程实现问询
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
Fixing the undeclared identifier error:
- In the code above, we declare the array
inputArraybefore using it in the input loop and when passing it tosums(). If you were usingdatainstead, make sure you declare it first (e.g.,float data[10];) before trying to accessdata[i].
- In the code above, we declare the array
How the
sums()function works:- We use reference parameters (
float &positiveSum, etc.) so the function can directly modify the variables inmain()—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).
- We use reference parameters (
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

