C++二维数组温度记录程序异常:警告及输出内存地址问题求助
Hey there! As someone who’s navigated the C++ learning curve after starting with Python/JS, I totally get where you’re stuck. Let’s break down your issues one by one and get that temperature program working right.
First: Why Are You Seeing Memory Addresses Instead of Temperatures?
Unlike Python or JavaScript, where printing an array directly shows its elements, C++ treats array names as pointers to the first element of the array. So if you tried something like cout << monthlyTemps;, you’re just printing the memory location where your 2D array starts—not the actual temperature values.
The Fix: Loop Through the 2D Array
You need nested loops to access each temperature value in your 12-month, high/low array. Here’s how to do it properly:
// Assume your array is defined like this: double temps[12][2]; for (int month = 0; month < 12; ++month) { cout << "Month " << month + 1 << ": " << "High = " << temps[month][0] << ", " << "Low = " << temps[month][1] << endl; }
This loops through each month, then accesses the first index (high temp) and second index (low temp) for that entry.
Second: Fixing the "Assign Return Value to New Variables" Warning
This warning pops up when you call a function that returns a value but don’t use that return value. For your temperature program, this is almost certainly related to cin (since you’re inputting temperatures).
cin returns an istream& object that tells you whether the input operation succeeded. If you just write cin >> temps[month][0];, the compiler notices you’re ignoring that return value—and flags it with a warning.
Two Ways to Fix This Warning
- Use the return value for input validation (recommended)
This isn’t just about silencing the warning—it makes your program more robust. Check if the input was valid:cout << "Enter high temp for month " << month + 1 << ": "; if (!(cin >> temps[month][0])) { cerr << "Whoops, that's not a valid number!" << endl; return 1; // Exit with error code } - Explicitly ignore the return value (quick fix)
If you just want to get rid of the warning without adding validation, you can cast the return value tovoid:static_cast<void>(cin >> temps[month][0]);
A Full Working Example
Putting it all together, here’s a complete snippet that should behave as expected:
#include <iostream> using namespace std; int main() { // 2D array: 12 months, each with [high temp, low temp] double monthlyTemps[12][2]; // Input phase for (int month = 0; month < 12; ++month) { cout << "=== Month " << month + 1 << " ===" << endl; cout << "Enter high temperature: "; if (!(cin >> monthlyTemps[month][0])) { cerr << "Invalid input! Exiting program." << endl; return 1; } cout << "Enter low temperature: "; if (!(cin >> monthlyTemps[month][1])) { cerr << "Invalid input! Exiting program." << endl; return 1; } } // Output phase cout << "\n--- Monthly Temperature Summary ---" << endl; for (int month = 0; month < 12; ++month) { cout << "Month " << month + 1 << ": " << "High = " << monthlyTemps[month][0] << "°C, " << "Low = " << monthlyTemps[month][1] << "°C" << endl; } return 0; }
Quick Note for Python/JS Devs
Remember: C++ arrays are way more low-level than what you’re used to. There’s no built-in "print all elements" method—you have to handle iteration yourself. Also, array indices start at 0 (just like JS/Python), so months 1-12 map to indices 0-11.
内容的提问来源于stack exchange,提问作者SSBASE

