获取“将返回值分配给新变量”警告和输出是内存地址

时间:2018-02-06 23:20:30

标签: c++ function multidimensional-array

我刚开始第二学期的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

}

我想补充说这是我第一次使用这个网站,我为任何错误事先道歉!提前谢谢!

2 个答案:

答案 0 :(得分:1)

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

 In function 'void getData()':
29:15: warning: statement has no effect [-Wunused-value]
31:19: warning: statement has no effect [-Wunused-value]
 In function 'void averageHigh(double (*)[2])':
47:11: warning: statement has no effect [-Wunused-value]
49:15: warning: statement has no effect [-Wunused-value]
44:12: warning: unused variable 'numElements' [-Wunused-variable]
45:12: warning: unused variable 'averageHighTemp' [-Wunused-variable]
51:58: warning: 'tempsAvgHigh[0][0]' is used uninitialized in this function [-Wuninitialized]
51:58: warning: 'tempsAvgHigh[0][1]' is used uninitialized in this function [-Wuninitialized]
 At global scope:
40:37: warning: unused parameter '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

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

答案 1 :(得分:0)

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

 //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