好的,我有一个卡路里计算器,应该将其分解为包括以下主要功能的五个功能。我的问题是我得到一个编译器错误,因为一旦获得了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;
}

最佳答案

如果要在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,因为这样做是一样的。请记住,只有您要更改并回显给 call 者的对象才需要通过引用传递。所以这只是从cal开头的。

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

如果您还没有达到可以在类作业中使用引用的地步(您的注释之一似乎表明了这一点),则可以执行C程序员数十年来所做的事情,通过指针。在上面的简化示例中,这意味着修改函数以接收要更改的项目的指针,更改其指向的内容,并使用要更改的变量的地址进行调用:
void xyzzy (int *pPlugh) { *pPlugh = 42; }
int main() {
    int twisty = 7;
    xyzzy (&twisty);
    cout << twisty << '\n';
    return 0;
}

但是,它不能替代真实的东西,如果您的教育家想教给您,那就像是他们让您为用户I / O使用printf/scanf而不是cout/cin一样:在C++中肯定是可能的,因为该语言包括传统的C语言内容,但实际上并不是在教您C++的方式。

那些自称是C++开发人员但真正使用C++编译器编写C语言的人,我很喜欢将其称为C +开发人员-他们从未真正接受过这种语言。人们越早放弃旧的东西,他们作为C++开发人员的状况就会越好。

10-06 05:33
查看更多