我试图将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;
}
你做错了。你应该阅读函数签名和传递参数。
您已经将weight
定义为不接受任何参数的函数
double weight() { //...}
但是你在main函数
中用一些参数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;
}