Megan McCracken
Megan McCracken

Reputation: 53

How to return a variable from one function in C++ to main then use it in another function?

Okay so I have a calorie calculator that is supposed to be separated into the five functions including main seen below. My issue is that I get a compiler error because the variables from the inputNumber function and calculateCalories function cannot be read by any of the other functions once they are obtained. I am not allowed to use Global variables. There must be something I am missing to be able to read the variables within the main function then output them into the other functions to get the proper output. Any help would be appreciated. Here is the code as it stands:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    int Lbs, hourr, hourW, hourWe, hourb;
    double calBad, calRun, calWal, calWei;
    string name;

    cout << "Welcome to Megan McCracken's Workout Calculator!" << endl;
    cout << endl;

    cout << "Please enter your name: ";
    getline(cin, name);

    inputNumber(Lbs, hourr, hourW, hourWe, hourb);

    calculateCalories(Lbs,hourr,hourb,hourW,hourWe,calBad,calRun,calWal,calWei);

    displayHeadings(name);

    displayLine(hourr,hourb,hourW,hourWe,calBad,calRun,calWal,calWei);

    system("pause");
    return 0;
}

int inputNumber(int Lbs, int hourr, int hourb, int hourW, int hourWe)
{
    cout << "Please enter your weight: ";
    cin >> Lbs;
    return Lbs;
    cout << "Please enter the minutes spent playing badminton: ";
    cin >> hourb;
    return hourb;
    cout << "Please enter the minutes spent running: ";
    cin >> hourr;
    return hourr;
    cout << "Please enter the minutes spent walking: ";
    cin >> hourW;
    return hourW;
    cout << "Please enter the minutes spent lifting weights: ";
    cin >> hourWe;
    return hourWe;
    cout << endl;
}

double calculateCalories(int Lbs, int hourW, int hourb, int hourr, int hourWe, double calBad, double calRun, double calWal, double calWei)
{
    const double Badburn = .044, Runburn = .087, Walkb = .036, Weightb = .042;
    double calBad, calRun, calWal, calWei;
    calBad = (Badburn * Lbs) * hourb;
    calRun = (Runburn * Lbs) * hourr;
    calWal = (Walkb * Lbs) * hourW;
    calWei = (Weightb * Lbs) * hourWe;
    return calBad;
    return calRun;
    return calWal;
    return calWei;
}

void displayHeadings(string name)
{
    cout << "Here are the results for " << name << ": " << endl;
    cout << endl;

    cout << "Activity" << right << setw(18) << "Time" << right << setw(10) << "Calories" << endl;
    cout << "--------------------------------------" << endl;
}

void displayLine(int hourb,int hourr, int hourW, int hourWe, double calBad, double calRun, double calWal, double calWei)
{
    int HB, MB, HR, MR, HW, MW, HWE, MWE, Hour, Min;
    double Calorie;
    HB = (hourb / 60);
    MB = (hourb % 60);
    HR = (hourr / 60);
    MR = (hourr % 60);
    HW = (hourW / 60);
    MW = (hourW % 60);
    HWE = (hourWe / 60);
    MWE = (hourWe % 60);

    Calorie = calBad + calRun + calWal + calWei;
    Hour = (hourb + hourr + hourW + hourWe) / 60;
    Min = (hourb + hourr + hourW + hourWe) % 60;

    cout << "Badminton" << right << setw(14) << HB << ":" << setfill('0') << setw(2) << MB << setfill(' ') << right << setw(10) << setprecision(3) << fixed << showpoint << calBad << endl;

    cout << resetiosflags(ios::fixed | ios::showpoint);

    cout << "Running" << right << setw(16) << HR << ":" << setfill('0') << setw(2) << MR << setfill(' ') << right << setw(10) << setprecision(3) << fixed << showpoint << calRun << endl;

    cout << resetiosflags(ios::fixed | ios::showpoint);

    cout << "Walking" << right << setw(16) << HW << ":" << setfill('0') << setw(2) << MW << setfill(' ') << right << setw(10) << setprecision(3) << fixed << showpoint << calWal << endl;

    cout << resetiosflags(ios::fixed | ios::showpoint);

    cout << "Weights" << right << setw(16) << HWE << ":" << setfill('0') << setw(2) << MWE << setfill(' ') << right << setw(10) << setprecision(3) << fixed << showpoint << calWei << endl;

    cout << "--------------------------------------" << endl;

    cout << resetiosflags(ios::fixed | ios::showpoint);

    cout << "Totals" << right << setw(17) << Hour << ":" << setfill('0') << setw(2) << Min << setfill(' ') << right << setw(10) << setprecision(3) << fixed << showpoint << Calorie << endl;
    cout << endl;
}

Upvotes: 2

Views: 20976

Answers (3)

paxdiablo
paxdiablo

Reputation: 881663

If you want to modify passed-in variables within a function in C++, you should be passing them in by reference (default is by value, meaning you get a copy of the variable which is effectively thrown away when the function exits).

So, by way of example:

void xyzzy (int plugh) { plugh = 42; }
int main() {
    int twisty = 7;
    xyzzy (twisty);
    cout << twisty << '\n';
    return 0;
}

will output 7 because twisty was passed by value and changes to it within the function will not be echoed back to the caller.

However, if you pass by reference with:

void xyzzy (int &plugh) { plugh = 42; }
//              ^
//              This does the trick.

then you'll find it outputs 42 as desired.

For your particular case, you want to look at the variables in the argument list of inputNumber:

int inputNumber(int Lbs, int hourr, int hourb, int hourW, int hourWe)

Any of these that you want echoed back to the caller (and that looks like all of them from a cursory glance) should be pass by reference rather than pass by value.

You should also look into calculateCalories as well, since that is doing the same thing. Keep in mind that only the ones you want to change and echo back to the caller need to be pass-by-reference. So that's only the ones starting with cal.

And, since you're using the pass-by-reference to modify the variables, there's absolutely no reason to return anything from that function so it can be specified as void calculateCalories ... and the return statements removed (in any case, only the first return would have actually done anything, the others would have been unreachable code).


If you haven't yet got to the point where you can use references in your classwork (as seems to be indicated by one of your comments), you can do what C coders have been doing for decades, emulating pass-by-reference with pointers. In terms of the simplified example above, that would mean modifying the function to receive a pointer to the item you want changed, changing what it points to, and calling it with the address of the variable to be changed:

void xyzzy (int *pPlugh) { *pPlugh = 42; }
int main() {
    int twisty = 7;
    xyzzy (&twisty);
    cout << twisty << '\n';
    return 0;
}

However, it's a poor substitute for the real thing and, if your educator is trying to teach you that, it's the same as if they're getting you to use printf/scanf rather than cout/cin for user I/O: it's certainly possible in C++ since the language includes legacy C stuff, but it's not really teaching you the C++ way.

People who claim to be C++ developers but really code in C using a C++ compiler, are a rather strange breed that I like to call C+ developers - they've never really embraced the language properly. The sooner people put aside the legacy stuff, the better they'll be as C++ developers.

Upvotes: 2

lostbard
lostbard

Reputation: 5220

In input number, you can not use 'return' to return each value - it will do the first return statement.

In C++ you can use pass by reference so that values assigned to the variables will be passed back up.

In this case, via the input variables would be inputNumber so use '&' to denote the vaiables are references:

void inputNumber(int &Lbs, int &hourr, int &hourb, int &hourW, int &hourWe)
{
.
.
.
}

Similar idea for calculateCalories, get rid of the returns:

void calculateCalories(int Lbs, int hourW, int hourb, int hourr, int hourWe, double &calBad, double &calRun, double &calWal, double &calWei)
{
.
.
}

Note that we are only bothering to pass to reference for the variables that we will be passing back.

Upvotes: 1

Andros Rex
Andros Rex

Reputation: 372

Pass the variables by references. Then the functions will be able to edit them.

Your other solution (not so much of a good idea but still working) is to create a struct/class and make the functions return it.


P.S. Your code won't work if the functions are in this order unless you add their signatures in the beginning:

int main();
int inputNumber(int,int,int,int,int);
//and so on

Upvotes: 1

Related Questions