我正在尝试从函数返回一个值,该函数有一个参数,我只是不知道如何获取这些值。我知道我可能做了一些错误的代码,这就是我在这里的原因。
以下是代码:
// 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();
}
答案 0 :(得分:0)
看看你如何使用这些函数,我猜你想提示用户在函数userAge
中输入用户的年龄并将该值返回给调用函数。
如果那是你想做的......
这是你的功能应该是什么样的。
int usersAge()
{
int age;
std::cout << "Please enter age: ";
std::cin >> age;
// If the user entered incorrect data, try again.
if ( !std::cin )
{
std::cout << "Unable to read age. Try again.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return userAge();
}
return age;
}
确保添加
#include <limits>
能够使用std::numeric_limits
。
对其他功能进行类似的更改。
答案 1 :(得分:0)
请修改您的代码如下; 希望这会有所帮助。
double usersHeight()
{
double height;
cout << "Ënter Height..." << endl;
cin >> height;
return height;
}
//function for users age
int usersAge()
{
int age;
cout << "Ënter age..." << endl;
cin >> age;
return age;
}
// function for users weight in lbs
double usersWeight()
{
double weight;
cout << "Ënter weight..." << endl;
cin >> weight;
return weight;
}
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())
+ (12.7 * usersHeight())
- (6.8 * usersAge());
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;
double BMI;
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')
{
BMI = calculateBmrForMen();
}
else if (a == 'f')
{
BMI = calculateBmrForWomen();
}
cout << "The BMI is..."<< BMI << endl;
std::cin.get();
std::cin.clear();
}