c++ - Why isn't the value to be returned from my function returning to main for the subsequent functions? - Stack Overfl

It's a simple if greater than, then make it the value of BigHighBigLow, however it does not retur

It's a simple if greater than, then make it the value of BigHigh/BigLow, however it does not return successfully to main for the printout of the respective data. Instead, the printout reads 0 for both functions. Any tips on where I go wrong?

#include<iostream>
using namespace std;

double getData(double tempmatrix[][2], double BigHigh, double BigLow) {

    for(int x=0;x<12;++x) {
            cout << "Enter the highest temperature for Month " << x+1 << ": ";
            cin >> tempmatrix[x][0];
            
            if(tempmatrix[x][0] > BigHigh) {
                BigHigh = tempmatrix[x][0];
                }
            
            cout << "Enter the lowest temperature for Month " << x+1 << ": ";
            cin >> tempmatrix[x][1];
            
            if(tempmatrix[x][1] > BigLow) {
                BigLow = tempmatrix[x][1];
                }
            }
return BigHigh, BigLow;
}

double indexHighTemp(double BigHigh) {
    cout << "\nHighest recorded temperature (according to you) was: " << BigHigh << endl;
}

double indexLowTemp(double BigLow) {
    cout << "\nLowest recorded temperature (according to you) was: " << BigLow << endl;
}

main () {
double tempmatrix[12][2];
double BigHigh = 0, BigLow = 0;

getData(tempmatrix, BigHigh, BigLow);
indexHighTemp(BigHigh);
indexLowTemp(BigLow);

return 0;
}

I tried float and double as data types to no avail, I'm completely stumped at this point.

It's a simple if greater than, then make it the value of BigHigh/BigLow, however it does not return successfully to main for the printout of the respective data. Instead, the printout reads 0 for both functions. Any tips on where I go wrong?

#include<iostream>
using namespace std;

double getData(double tempmatrix[][2], double BigHigh, double BigLow) {

    for(int x=0;x<12;++x) {
            cout << "Enter the highest temperature for Month " << x+1 << ": ";
            cin >> tempmatrix[x][0];
            
            if(tempmatrix[x][0] > BigHigh) {
                BigHigh = tempmatrix[x][0];
                }
            
            cout << "Enter the lowest temperature for Month " << x+1 << ": ";
            cin >> tempmatrix[x][1];
            
            if(tempmatrix[x][1] > BigLow) {
                BigLow = tempmatrix[x][1];
                }
            }
return BigHigh, BigLow;
}

double indexHighTemp(double BigHigh) {
    cout << "\nHighest recorded temperature (according to you) was: " << BigHigh << endl;
}

double indexLowTemp(double BigLow) {
    cout << "\nLowest recorded temperature (according to you) was: " << BigLow << endl;
}

main () {
double tempmatrix[12][2];
double BigHigh = 0, BigLow = 0;

getData(tempmatrix, BigHigh, BigLow);
indexHighTemp(BigHigh);
indexLowTemp(BigLow);

return 0;
}

I tried float and double as data types to no avail, I'm completely stumped at this point.

Share Improve this question edited Mar 4 at 11:35 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 4 at 11:17 Mathieu BayanaMathieu Bayana 11 silver badge 5
  • You functions need to return a value. The it should be assigned to a variable (or used in another way) on the caller side. – wohlstad Commented Mar 4 at 11:19
  • 11 return BigHigh, BigLow; doesn't do what you think it does. Or actually, you seem to have a lot more assumptions, given that the return value is not used in main() anywhere. I'd strongly recommend getting a good C++ book to learn from, guessing syntax and how the code should work will only bring you woe and misery. – Yksisarvinen Commented Mar 4 at 11:20
  • 1 double getData - so this method is supposed to return a double. return BigHigh, BigLow; - that's not a double. If you want to return multiple things, you need to wrap them into a pair or tuple or something. Returning multiple values from a C++ function – C3roe Commented Mar 4 at 11:21
  • 11 To be brutally honest, you seem to have skipped quite a lot of your beginners material (books, tutorials, classes). Please don't do that. Please don't guess about things. And please don't use knowledge of another language to guess. – Some programmer dude Commented Mar 4 at 11:27
  • 1 It seems that what you are actually trying to do is use references to 'return' the values to main. For that your code should look like this void getData(double tempmatrix[][2], double& BigHigh, double& BigLow) { and return BigHigh, BigLow; should be deleted. It's clear that you are guessing at syntax, that's rarely going to work. – john Commented Mar 4 at 11:37
Add a comment  | 

1 Answer 1

Reset to default 1

The return statement was irrelevant, an attempt to make things work.

What is needed: the parameters must be passed a variable reference (an expression that contains a double, an assignable double lvalue). double& (double reference).

I give this solution, and also give a second solution with double pointers, as the compiler implements double&. That is how it must be done in C, as C does not have double&.

double maxHigh = 0, maxLow = 0;
getData(tempmatrix, maxHigh, maxLow);
indexHighTemp(maxHigh);
indexLowTemp(maxLow);

void getData(double tempmatrix[][2], double& BigHigh, double& BigLow) {
    BigHigh = -9999.0;
    BigLow = 9999.0
    for (int x = 0; x < 12; ++x) {
        cout << "Enter the highest temperature for Month " << x+1 << ": ";
        cin >> tempmatrix[x][0];
        
        if (tempmatrix[x][0] > BigHigh) {
            BigHigh = tempmatrix[x][0];
        }
        
        cout << "Enter the lowest temperature for Month " << x+1 << ": ";
        cin >> tempmatrix[x][1];
        
        if (tempmatrix[x][1] < BigLow) {
            BigLow = tempmatrix[x][1];
        }
    }
}

This is the same as the explicit code:

double maxHigh = 0, maxLow = 0;
getDataCStyle(tempmatrix, &maxHigh, &maxLow);
indexHighTemp(maxHigh);
indexLowTemp(maxLow);

void getDataCStyle(double tempmatrix[][2], double* BigHigh, double* BigLow) {
    *BigHigh = -9999.0;
    *BigLow = 9999.0
    for (int x = 0; x < 12; ++x) {
        cout << "Enter the highest temperature for Month " << x+1 << ": ";
        cin >> tempmatrix[x][0];
        
        if (tempmatrix[x][0] > *BigHigh) {
            *BigHigh = tempmatrix[x][0];
        }
        
        cout << "Enter the lowest temperature for Month " << x+1 << ": ";
        cin >> tempmatrix[x][1];
        
        if (tempmatrix[x][1] < *BigLow) {
            *BigLow = tempmatrix[x][1];
        }
    }
}

And some remarks:

  • It seems more logical swapping the parameters, low before high: double& BigLow, double& BigHigh
  • Giving BigHigh and BigLow a value inside the function instead of 0 outside, ensures that not coincidentally the outside value is taken should it be an extreme value.
  • I gave the passed variables an other name, it could be any double storing expression.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047472a4608183.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信