如何将变量从 C 中的一个函数返回到 main 然后在另一个函数中使用它?

新手上路,请多包涵

好的,所以我有一个卡路里计算器,它应该分为五个功能,包括下面看到的主要功能。我的问题是我得到一个编译器错误,因为 inputNumber 函数和 calculateCalories 函数中的变量一旦获得就不能被任何其他函数读取。我不允许使用全局变量。我必须缺少一些东西才能读取主函数中的变量,然后将它们输出到其他函数中以获得正确的输出。任何帮助,将不胜感激。这是代码:

 #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;
}

原文由 Megan McCracken 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 393
1 个回答

如果你想在 C++ 的函数中修改传入的变量,你应该通过引用传递它们(默认是通过值,这意味着你会得到一个在函数退出时被有效丢弃的变量的 _副本_)。

因此,例如:

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

将输出 7 因为 twisty 是按值传递的,并且在函数内对其进行的更改不会回显给调用者。

但是,如果您通过 引用 传递:

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

然后你会发现它根据需要输出 42

对于您的特定情况,您想查看 inputNumber 的参数列表中的变量:

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

您想要回显给调用者的任何这些(从粗略的一瞥看起来就像所有这些)都应该通过引用传递而不是通过值传递。

您还应该查看 calculateCalories 以及,因为这是做同样的事情。请记住, 只有 您想要更改并回显给调用者的那些需要通过引用传递。所以这只是以 cal 开头的那些。

而且,由于您使用传递引用来修改变量,因此绝对没有理由从该函数返回任何内容,因此可以将其指定为 void calculateCalories ...return 删除的语句(无论如何,只有第一个 return 实际上会做任何事情,其他的将是无法访问的代码)。


如果您还没有达到可以在课堂作业中使用引用的程度(正如您的评论之一所表明的那样),您可以做 C 编码员几十年来一直在做的 _事情_,用指针。就上面的简化示例而言,这意味着修改函数以接收 指向 您要更改的项目的指针,更改它指向的内容,并使用要更改的变量的地址调用它:

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

但是,它不能替代真实的东西,如果您的教育者试图教您,这就像他们让您使用 printf/scanf 而不是 cout/cin 对于用户 I/O:这在 C++ 中当然是 可能 的,因为该语言包含遗留 C 的东西,但它并没有 真正 教你 C++ 的方式。

自称是 C++ 开发人员但实际上使用 C++ 编译器编写 C 代码的人是一个相当奇怪的品种,我喜欢称其为 C+ 开发人员——他们从未真正正确地接受该语言。人们越早放下遗留的东西,他们作为 C++ 开发人员的能力就越好。

原文由 paxdiablo 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题