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

Getting “Assign Return Value to New Variables” warning and output is memory address

本文关键字:警告 输出 地址 内存 变量 返回值 分配 新变量 获取      更新时间:2023-10-16

我刚刚开始我的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:nn";
    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
}

我想补充一点,这是我第一次使用此网站,我对任何错误表示歉意!预先感谢!

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

在函数" void getData(("中:29:15:警告:语句没有效果[-wunused-value]31:19:警告:声明没有效果[-wunused-value] 在函数中" void peraseashigh(double(*([2](':47:11:警告:声明没有效果[-wunused-value]49:15:警告:声明没有效果[-wunused-value]44:12:警告:未使用的变量'数字'[ -  wunused-variable]45:12:警告:未使用的变量'平均hightemp'[ -  wunused-dariable]51:58:警告:'tempsavghigh [0] [0]'在此功能[ -  wuninitialization]中使用51:58:警告:'tempsavghigh [0] [1]'在此功能[ -  wuninitialization]中使用 在全球范围:40:37:警告:未使用的参数'arr1'[ -  wunused-parameter]

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

(

也许尝试:

    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

这是您的开始。欢迎进行编码。

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

 //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 ) andn"
            "computes the average high and low temperature for the islandn"
            "of Puerto Rico, also it calculates the index highest and lowestn"
            "temperatures on the island as wellnn";
    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