【问题标题】:Getting “Assign Return Value to New Variables” warning and output is memory address获得“将返回值分配给新变量”警告和输出是内存地址
【发布时间】:2018-07-17 03:18:23
【问题描述】:

我刚刚开始第二学期的 CS,我们使用的编程语言是 c++,当然我是新手。我正在编写一个程序,将每月的温度(高温和低温)记录到二维数组中。我之前有一些编程经验,主要是 javascript 和 python,但没有什么特别的。程序编译并成功运行,但我没有得到想要的结果。首先,这是我的完整代码(至此)

#include<iostream>
#include<fstream>
using namespace std;

const int MONTHS = 12;
const int TEMPS = 2;

void getData(); 
void averageHigh(double arr1[][TEMPS]); 

int main()
{

    cout << "This program outputs average high and low temperature for a year in Puerto Rico:\n\n";

    getData();

    return 0;
}

void getData()
{
    ifstream tempsHigh_Low;
    int i = 0, j = 0;
    double temps[MONTHS][TEMPS];
    tempsHigh_Low.open("tempsHigh_Low.txt");
    if (tempsHigh_Low.is_open())
    {
        for (i; i < MONTHS; i++)
        {
            for (j; j < TEMPS; j++)
            {
                tempsHigh_Low >> temps[i][j];
            }
        }
    }
    averageHigh(temps);
}

void averageHigh(double arr1[][TEMPS])
{
    double tempsAvgHigh[MONTHS][TEMPS];
    double sumElements = 0;
    double numElements = MONTHS;
    double averageHighTemp;
    int v = 0, k = 0;
    for (v; v < MONTHS; v++)
    {
        for (k; k < TEMPS; k++)
        {
            sumElements = sumElements + tempsAvgHigh[v][k];
        }
    }
    cout << sumElements << endl; //right here, it prints out memory loc and

//and issues out warning...this is a test to find out whether i can
//proceed to take the arithmetic mean of the first column of the array
//averageHigh

}

我想补充一点,这是我第一次使用这个网站,对于任何错误,我提前道歉!提前致谢!

【问题讨论】:

  • 该行不打印内存地址。
  • 将代码放入编辑器以更好地缩进它,编译器会尖叫着让你听。不要忽略警告。警告意味着您的代码在语法上是正确的并且可以编译,但逻辑看起来很不稳定。编译器是抵御错误的第一道防线,因此请充分利用它告诉您的信息。

标签: c++ function multidimensional-array


【解决方案1】:

谢谢大家,正如所指出的,我修复了需要修复的问题并提出了这个问题。这是一个作业,理想情况下我想要的是制作一个随机数生成器来将数字输出到文本文件中,但我的时间很短。

 //Sebastian R. Papanikolaou


#include<iostream>
#include<fstream>
using namespace std;

const int MONTHS = 12;
const int TEMPS = 2;


void getData(); //getData() function prototype
void averageHigh(double arr1[][TEMPS]);//averageHigh() function prototype
void averageLow(double arr2[][TEMPS]); //averageLow() function prototype
void indexHighTemp(double arr3[][TEMPS]); //indexHighTemp() function prototype
void indexLowTemp(double arr4[][TEMPS]);//indexLowTemp() function prototype

int main() {

    cout << "This program takes in data from a text file (.txt ) and\n"
            "computes the average high and low temperature for the island\n"
            "of Puerto Rico, also it calculates the index highest and lowest\n"
            "temperatures on the island as well\n\n";

    getData();

    return 0;
}
void getData() //gathers input from the text file 'tempsHigh_Low.txt'
               //and stores it into the 2D array 'temps'

{
    ifstream tempsHigh_Low;
    double temps[MONTHS][TEMPS];

    tempsHigh_Low.open("tempsHigh_Low.txt");

    if(tempsHigh_Low.is_open()) {

        for(int i = 0; i < MONTHS; i++) {
            for(int j = 0; j < TEMPS; j++){
                tempsHigh_Low >> temps[i][j];
            }
        }
    }
    averageHigh(temps);//function call to averageHigh() with 'temps' as the parameter    
}

void averageHigh(double arr1[][TEMPS])//calculates the average of the first column in the arr1
{ 

    double sumElements = 0;
    double numElements = MONTHS;
    double averageHighTemp;

    for(int v = 0; v < MONTHS; v++)
    {
        sumElements = sumElements + arr1[v][0];
    }
    averageHighTemp = sumElements/MONTHS;

    cout << "The average high temperature in Puerto Rico is " 
            << averageHighTemp<< "F" << endl << endl;
    averageLow(arr1);//arr1 is sent to the function averageLow()
}

void averageLow(double arr2[][TEMPS])//averageLow() takes in the data from averageLow() and calculates the average the second column of arr2
//arr2 in reality is still 'temps' from getData()
{
    double sumElements = 0;   
    double numElementes = MONTHS;
    double averageLowTemp;

    for(int n = 0; n < MONTHS; n++)
    {
        sumElements = sumElements + arr2[n][1];
    }
    averageLowTemp = sumElements/MONTHS;

    cout << "The average low temperature in Puerto Rico is "
         << averageLowTemp<< "F" << endl << endl;
    indexHighTemp(arr2);//arr2 is sento to indexHighTemp()
}

void indexHighTemp(double arr3[][TEMPS])//calculates the highest value in the first column of arr3
{
    int largestHighTemp;

    for(int l = 0; l < TEMPS; l++)
    {
        largestHighTemp = arr3[0][l];

        for(int e = 0; e < MONTHS; e++)
        {
            if(arr3[e][0] > largestHighTemp)
                largestHighTemp = arr3[e][0];
        }
    }
    cout << "The highest temperature that Puerto Rico reaches is "
            << largestHighTemp << "F" << endl << endl;
    indexLowTemp(arr3);//arr3 is sent to indexLowTemp()
}

void indexLowTemp(double arr4[][TEMPS])//calculates the lowest value in the second column of arr4
{
    int lowestLowTemp;

    for(int a = 0; a < TEMPS;a++)
    {
        lowestLowTemp = arr4[0][a];
        for(int b = 0; b < TEMPS; b++)
        {
            if(arr4[b][1] < lowestLowTemp)
            {
                lowestLowTemp = arr4[b][1];
            }
        }
    }
    cout << "The lowest temperature that Puerto Rico reaches is "
            << lowestLowTemp << "F" << endl << endl;
}

输入列表如下:

83 70
84 70
85 71
86 73
87 74
89 76
88 76
89 76
88 76
86 75
86 74
84 72

【讨论】:

    【解决方案2】:

    以下是我在编译时看到的警告:

    在函数 'void getData()' 中: 29:15:警告:声明无效 [-Wunused-value] 31:19:警告:声明无效 [-Wunused-value] 在函数 'void averageHigh(double (*)[2])' 中: 47:11:警告:声明无效 [-Wunused-value] 49:15:警告:声明无效 [-Wunused-value] 44:12:警告:未使用的变量 'numElements' [-Wunused-variable] 45:12:警告:未使用的变量 'averageHighTemp' [-Wunused-variable] 51:58:警告:“tempsAvgHigh[0][0]”在此函数中未初始化 [-Wuninitialized] 51:58:警告:“tempsAvgHigh[0][1]”在此函数中未初始化 [-Wuninitialized] 在全球范围内: 40:37:警告:未使用的参数 'arr1' [-Wunused-parameter]

    似乎你的 for 循环没有做你期望他们做的事情(或者他们是,但不是你认为的数据......)

    也许正在尝试:

        for (int i=0; i < MONTHS; i++)
        {
            for (int j=0; j < TEMPS; j++)
            {
                tempsHigh_Low >> temps[i][j];
            }
        }
    

    for (int v=0; v < MONTHS; v++)
    {
        for (int k=0; k < TEMPS; k++)
        {
            sumElements = sumElements + tempsAvgHigh[v][k];
        }
    }
    

    可能对您的一些警告有所帮助。它确实有助于将计数器变量保持在一个不会在事故中对其他地方造成伤害的范围内。请注意,您需要删除代码中较高的 i,j,v,k 声明,因为我们现在在 for 循环中将它们声明为内联。

    还请注意,您将arr1 传递给您的普通函数,但根本不使用它,而是从tempAvgHig 的未初始化堆栈内存中读取。也许您打算在循环中使用arr1

    这对你来说是一个开始。欢迎编码。

    【讨论】:

    • 感谢您的反馈,我会尽快处理这个问题,我会返回更新。我唯一的爱好是三年或更长时间的数学,我可以告诉你,编码完全是另一个层次。如此之多,令人担忧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多