2

Whenever I change the function "double getsales (double &num)" to double getsales (double num) and the function prototype appropriately, the program doesn't correctly. I don't understand why my lucky guess fixed it, no matter how much I try to read about reference variables.

Can someone explain?

#include <iostream>
#include <iomanip>

using namespace std;

double getsales (double &);
void findhighest (double, double, double, double);

int main()
{
    double northeast = 0;
    double southeast = 0;
    double northwest = 0;
    double southwest = 0;

    cout << "Enter NorthEast sales: $" ;
    cout << getsales(northeast) << endl;
    cout << "Enter SouthEast sales: $"; 
    cout << getsales(southeast) << endl;
    cout << "Enter NorthWest sales: $";
    cout << getsales(northwest) << endl;
    cout << "Enter SouthWest sales: $";
    cout << getsales(southwest) << endl;

    findhighest(northeast, southeast, northwest, southwest);

    return 0;
}

double getsales (double &num)
{
    do
    {
        if(!cin)
        {
            cin.clear();
            cin.ignore(100, '\n');
        }

        cin >> num;
        cout << "Number entered: ";


    }while(!cin || num <= 0);
    return num;
}

void findhighest (double ne, double se, double nw, double sw) 
{
    const char *who = "NorthEast";
    double high = ne;
    if(se > high)
    {
        who = "SouthEast";
        high = se;
    }
    if(nw > high)
    {
        who = "NorthWest";
        high = nw;
    }
    if(sw > high)
    {
        who = "SouthWest";
        high = sw;
    }

    cout << fixed << showpoint << setprecision(2) << endl;
    cout << who << "has the highest sale ($" << high << ")" << endl;
}
bigZigZag
  • 33
  • 5
  • 3
    Isn't `do { if(!cin) ...` equivalent to [`while(!cin.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)? – πάντα ῥεῖ Nov 28 '14 at 21:07

2 Answers2

2

When passing by value, getsales won't modify the original number passed as parameter.

Therefore, the call

findhighest(northeast, southeast, northwest, southwest);

will operate with the original numbers (which are zero).

You should use the modified values.

Laura Maftei
  • 1,773
  • 1
  • 13
  • 23
1

with

double getsales(double&num);
double southwest = 0;
cout << getscales(southwest);

your function getsales() actually works on the variable handed in by the caller (using num as local name for it), i.e. it reads directly into the variable southwest of the calling program. It also returns that value, but that will not change the variable southwest.

When instead you have

double getsales(double num);

the function works with its own internal variable num, which is initialised to the value passed by the caller, i.e. with 0 since that's the value of southwest at the moment of call. It thus has no effect on the variable southwest. The function does return the same value, though.

Thus in the first case (using references), the variables southwest etc. are modified but not in the other case of your program.


However, returning a value via a reference is not necessarily best practice, because it is not evident from the function call (i.e. without having seen the function declaration) that this will potentially modify the variable passed. Instead, a 'getter' typically returns the value, i.e. has prototype

double getsales();

then the usage could be

cout << (southwest = getsales()) << endl;

etc. In this case, it is selfevident what's going on, even without having seen the prototype for getsales().

Walter
  • 40,885
  • 16
  • 97
  • 176