如何通过多个函数传递数据并在main中正确调用它们?

时间:2014-06-08 17:14:54

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

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

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

1 个答案:

答案 0 :(得分: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;
}