【问题标题】:How do I pass data through multiple functions and call them correctly in main?如何通过多个函数传递数据并在 main 中正确调用它们?
【发布时间】:2014-07-29 07:25:12
【问题描述】:

我正在尝试将userWeight 调用到double convert() 函数中。我该怎么做呢?我遇到了在 main 中没有合作的问题。

#include <iostream>
#include <string>

using namespace std;

// health calc

string name()
{
    cout << "Welcome ________ ... uhmmmm, what was your name again?   ";
    string name1;
    cin >> name1;
    cout << " " << endl;
    cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
    return name1;

}

int height(string name1) //(string name1) is what we are passing into this function
{
    //feet and inches to inches
    cout << " How tall are you, " << name1 <<"?"<< endl;
    cout << " " << endl;
    cout << " " << endl;
    cout << " Enter feet:    ";
    int feet;
    cin >> feet;
    cout << " " << endl;
    cout << " Enter inches:    ";
    int inches;
    cin >> inches;
    int inchesheight;

    inchesheight = (feet * 12) + inches;

    cout << " " << endl;
    cout << " Your height is equal to " << inchesheight << " inches total." << endl;


    if (inchesheight < 65 )
    {
        cout << " You are shorter than the average male." << endl;
    }
    else if (inchesheight > 66 && inchesheight < 72)
    {
        cout << " You are of average height." << endl;
    }
    else
    {
        cout << " You are taller than average." << endl;
    }


}

double wieght()
{
    cout << " How much do you weigh? (In pounds) " << endl;
    double userWeight;
    cin >> userWeight;

    cout << " Ok so your weight in the Imperial System (lbs.), is " << userWeight << endl;
    cout << " Would you like to know what your weight is in the Metric System? (kilograms) " << endl;
cout << " please answer as 'yes' or 'no;" << endl;
string response;
cin >> response;

    if (response == "yes")
    {
        cout << " Alright! Let us start converting your weight! " << endl;
    }
    else if (response == "no")
    {
        cout << " Too bad! We are going to do it anyway! " << endl;
    }
    else
    {
        cout << " That was not a proper response! Way to follow directions!, as consequence, we will do it!" << endl;
    }

    return userWeight;


}

double convert(double userWeight)
{
    cout << " Well since 1 kilogram is equal to 2.2046226218 pounds, we need to divide your weight by that repeating number." << endl;
    cout << " Since that number is very long and ugly, we will use 2.2046 for the sake of clarity." << endl;
    double kiloWeight = (userWeight / 2.2046);
    cout << "Your weight in pounds is " << userWeight << "lbs, divided by 2.2046 gives us" << kiloWeight << "kgs! " << endl;


}


int main()
{

    string name1 = name();
    height(name1);
    weight(userWeight);
    convert();
    return 0;
}

【问题讨论】:

  • 呃,“函数参数”?看这里:Reference Parameters in CFunction returns 是另一种选择。 Global variables 是另一个(但通常很差)的选择。
  • 你还有一个错字:double wieght().
  • 你应该 a) 切换你正在做的教程,b) 进一步阅读你的教程或 c) 买一本关于 c++ 的书。
  • 堆栈中的艰难人群
  • 对不起,这不是消极的意思,但是这个问题应该在每本书或教程的第一页都有涉及。阅读综合指南会比问很多基本问题要快得多。

标签: c++ function scope main type-conversion


【解决方案1】:

你做错了。您应该阅读有关函数签名和传递参数的信息。

您已将weight 定义为不带任何参数的函数

double weight() { //...}

但是您在主函数中使用一些参数userWeight 调用它

weight(userWeight);

另外这个参数没有定义。 (不:你不能调用一个函数,其参数是从同一范围调用的函数堆栈上的本地参数 - 这在技术上是可能的,但这不是你想要的)。

这应该是这样的:

int main() {

    double userWeight = weight();
    double result = convert( userWeight);
    // we can see here that local variable named userWeight was assigned value 
    // from a call to weight() and this result is now being passed to convert
    // now you can use a result from calling convert
    //...
    return 0;
}

【讨论】:

  • ..我到底应该怎么做?我现在很困惑。
  • userWeight 来自已经在权重函数中的用户。我正在尝试将此变量与他们的输入一起使用,并将完全相同的东西带入转换函数。
  • 所有工作都在函数中完成,我只希望 main 随后运行每个函数
  • 权重()中放入堆栈的变量,具有双用户权重;无法在其他函数中访问
  • 对函数的每次调用都会创建一个新堆栈,并且所有放置在此堆栈上的变量都会在函数返回时销毁
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多