我試圖從一個函數返回一個值,該函數有一個參數,我只是不知道如何去獲取這些值。我知道我可能做了一些錯誤的代碼,這就是爲什麼我在這裏。返回參數並在其他函數中使用它們
下面是代碼:
// ask user if male or female
// complete calculations depending on result
// ask user for: weight in lbs, height in inches and age.
#include <iostream>
// function for users height in inches
double usersHeight(double height)
{
return height;
}
//function for users age
int usersAge(int age)
{
return age;
}
// function for users weight in lbs
double usersWeight(double weight)
{
return weight;
}
// mens bmr (basal metabolic rate)
double calculateBmrForMen()
{
// return calculations
// Men: BMR = 66 + (6.23 x weight in pounds) + (12.7 x height in inches) - (6.8 x age in years)
double BMR = 66 + (6.23 * usersWeight(weight/*how do I get user input and here without causing any errors?*/))
+ (12.7 * usersHeight(height/* and here*/))
- (6.8 * usersAge(age/*and here*/));
return BMR;
}
// womens bmr
double calculateBmrForWomen()
{
// return calculations
// Women: BMR = 655 + (4.35 x weight in pounds) + (4.7 x height in inches) - (4.7 x age in years)
double BMR = 655 + (4.35 * usersWeight()) + (4.7 * usersHeight()) - (4.7 * usersAge());
return BMR;
}
int main()
{
/*char a;
std::cout << "Are you Male or Female: " << std::endl;
std::cout << "Press M if Male\nPress F if female" << std::endl;
std::cin >> a;
if (a == 'm')
{
calculateBmrForMen();
}
else if (a == 'f')
{
calculateBmrForWomen();
}*/
std::cin.get();
std::cin.clear();
}
'weight/*如何獲得用戶輸入,這裏沒有引起任何錯誤?* /)'使用一個名爲'weight'和'std :: cin'的局部變量可能是什麼? –
我不知道你爲什麼在這裏得到-4,雖然這對人們宣佈他們的DV很好,但我們允許匿名投票,所以我們必須猜測。這可能是沒有跡象表明您已經完成了什麼調試,或者問題是什麼。如果將來你可以更具體,那通常會有幫助。此外,不要在帖子中添加投票評論是一個好主意,因爲這可以自相矛盾地鼓勵更多的DV! HTH。 – halfer