How to calculate BMI in C++ with ft and inches in calculation - c++

How do you calculate BMI = mass (lb) x 703/ (height(in))squared in C++.
BMI answer was 33.4695
I entered:
cout << "weight(lbs)";
cin >> lbs;
cout << "height";
cin >> height >> in;
BMI = ((lbs) * 703)/pow(height(in)), 2);
I'm having and error: no instance of overloaded function "pow" matched the argument list argue types are: (<error-types>).
I have included the <cmath> file and height is defined as double.

Let's start saying you don't need to call pow() at all, but in your case you misplaced the braces. It should be
pow(height, 2)
Assuming that height is a numeric variable.
It's not clear if you want to include somehow the units of measures of phisical quantities (inches or pound) into your variables. That's a more challenging task, but meanwhile you can use this:
float weight = 0, height = 0;
cout << "Enter weight(lbs): ";
cin >> weight;
cout << "Enter height(inches):";
cin >> height;
float BMI = weight * 703.0 / (height * height);
cout << "BMI = " << BMI << '\n';

Related

Not understanding setprecision function in c++

I'm learning C++ and am supposed to make a program which takes height in inches, weight in pounds, and age from the user, and gives them the size of their clothing. You get the size of their hat by dividing their weight by their height and multiplying that result with 2.9. I have been testing my code and the output is correct but always has an extra 1 (like 9.941 instead of 9.94) at the end. My answer should only have two digits after the decimal point, even if it's a zero. Does anyone have any tips? Thanks guys. Here is my code:
#include<iostream>
#include<iomanip>
using namespace std;
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9);
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
cout << HatSize;
return 0;
}
You're using setprecision correctly, the issue is that you have an additional statement that is generating the 1.
Remove the cout << HatSize; line. HatSize is a function that returns void, so you're sending the actual function itself as input to cout, which is being interpreted as 1.
I would also recommend adding a << endl to the cout in your HatSize function, so that your output finishes with a newline.
void HatSize(double userHeight, double userWeight) {
cout << setprecision(2) << fixed << ((userWeight / userHeight) * 2.9)
<< endl; // Newline to make output nicer
}
int main(){
double height;
double weight;
double age;
cout << "Give me your height in inches, weight in pounds, and age in years. I will give you your hat size, jacket size (inches at chest)\nand waist size in inches." << endl;
cin >> height;
cin >> weight;
cin >> age;
HatSize(height, weight);
// cout << HatSize !!!!!!!!! Get rid of this
return 0;
}

How to debug my C++ BMI calculator program?

I've only recently learnt C++. I had a school project for making a BMI calculator. Unfortunately it is showing up errors beyond my scope of understanding. I'm not sure if I should use a different data type for my height and weight variables - should these by double?
#include <iostream>
using namespace std;
float bmi_calc(int height, int weight){
float bmi_user = weight / height * height;
return bmi_user;
}
int main()
{
int weight_user;
int height_user();
cout << "Enter your weight in kilograms";
cin >> weight_user;
cout << "Enter your height in meters";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
}
fixed code:
#include <iostream>
using namespace std;
float bmi_calc(float height, float weight) {
return weight / (height * height);
}
int main()
{
float weight_user;
float height_user;
cout << "Enter your weight in kilograms ";
cin >> weight_user;
cout << "Enter your height in meters ";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
return 0;
}
since your height is given in meters you need a float, since a int can only be used for whole numbers. also use proper parenthesis to get the formula right :)

C++ Airfare Charge Calculation project

I have to create a program to calculate charges for airfare. It's a simple program so far and I am not done adding to it, but every time I run it the result turns out to be 0. Is there something missing in my code? I am a beginner and I would appreciate any advice on improving my code. Thank you.
#include <iostream>
using namespace std;
void main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price = distance * 0.15;
double bag_price = num_bags * 25.00;
double meal_price = num_meals * 10.00;
double total_airfare = 0.00;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
total_airfare = (distance_price + bag_price + meal_price);
cout << total_airfare;
}
Your confusion is completely understandable - the piece you're missing is that when you assign a variable, you're assigning the left side to the result of the right side at that moment in time. It's not like algebra, where you say f(x) = x + 5 and f(x) is always whatever x + 5 is.
So, you assign double distance_price = distance * 0.15 when distance is 0 (which you just initialized). distance_price remains 0 even after you ask for input and change distance.
Do your price calculations after you ask for input, and everything will work just fine.
You are calculating the distance_price bag_price meal_price with default values i.e. 0 not with the value which you took from user.
Below code works fine and you won't see the issue.
#include <iostream>
using namespace std;
// My compiler did not allow void main so used int main
int main () {
int distance = 0;
int num_bags= 0;
int num_meals= 0;
double distance_price ;
double bag_price ;
double meal_price;
double total_airfare;
cout << "CorsairAir Fare Calculator" << endl;
cout << "Enter the distance being travelled: " << endl;
cin >> distance;
cout << "Enter number of bags checked: " <<endl;
cin >> num_bags;
cout << "Enter the number of meals ordered: " << endl;
cin >> num_meals;
distance_price = distance * 0.15;
bag_price = num_bags * 25.00;
meal_price = num_meals * 10.00;
total_airfare = 0.00;
total_airfare = distance_price + bag_price + meal_price;
cout << total_airfare;
return 0;
}
Result
CorsairAir Fare Calculator
Enter the distance being travelled:
200
Enter number of bags checked:
2
Enter the number of meals ordered:
2
100

BMI calculator bug

I'm trying to do a basic BMI calculator, and it works fine when I do the metric side of it, however, the imperial side returns a wrong answer.
cout << "What is your weight? ";
cin >> weight;
cout << "What is your height in either inches or meters? ";
cin >> height;
cout << "Is that metric or imperial? Type 1 for metric, or 0 for imperial ";
cin >> unit;
if (unit = 1)
answer = weight / (height * height);
if (unit = 0)
answer = (weight * 703) / (height * height);
cout << "Your BMI is " << answer << endl;
system("PAUSE");
return 0;
I thought the equation for BMI for imperial, according to Wikipedia was
Weight(in pounds) * 703 / (height in inches squared)
Use == for testing equality not =
You are using the assignment operator "=" in your if statements, both of which will ALWAYS return true, because you ARE able to assign the value of "0" or "1" to unit.
To test for equality you must use "==".
This should fix this problem! (:

A question about cin in C++

When I declare int weight and then input a double value 165.1 a 2nd cin >> height; doesn't work and there is no any error message. Can you tell me why?
VS2010 Console Application used.
#include <iostream>
using namespace std;
const double lbs_to_kg = 2.2046, inches_to_meter = 39.370;
int main()
{
int weight, height;
double kilograms, meters;
cout << "\nEnter weight in pounds: ";
cin >> weight;
kilograms = weight / lbs_to_kg;
cout << "\nEnter height in inches: ";
cin >> height;
meters = height / inches_to_meter;
cout << "\nYour BMI is approximately "
<< "\nbody fat ratio is "
<< kilograms / (meters * meters)
<< ". Under 25 is good."
<< endl;
}
output:
Enter weight in pounds: 165.1
Enter height in inches:
Your BMI is approximately
body fat ratio is 1.57219e-013. Under 25 is good.
If you try to have cin extract data into a variable that can't hold it, the data is left in the input stream and cin is flagged as having failed. You need to check if it's failed with !cin, and use cin.clear() to clear the fail flag so you can read again (future extract operations will automatically fail until the flag is cleared). You can either extract the data into a different variable that's capable of holding it, or use cin.ignore() to discard it