Click here to Skip to main content
15,889,865 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I'm new to programming in C++ and am wondering why bmi in this program is returning as 0. Thank you!

C++
#include <iostream>
using namespace std;

int main()
{

    string name;
    int weight;
    int height;
    float bmi;

    cout << "What is your name?" << endl;
    cin >> name;

    cout << "What is your total weight (lbs.)?" << endl;
    cin >> weight;

    cout << "What is your height (inches) [1 foot = 12 inches]?" << endl;
    cin >> height;


    bmi = weight / (height * height) * 703;


    cout << name << "'s BMI is " << bmi << endl;


    return 0;
}


Any help would be much appreciated, thank you!

What I have tried:

I've tried rearranging the equation to no avail.
Posted
Updated 30-Aug-21 20:14pm
v3

Maybe take a look at the following thread, I suspect your problem is similar. This C++ program give percentage 0% , can someone help?[^]
 
Share this answer
 
Comments
CPallini 31-Aug-21 2:11am    
5.
jeron1 31-Aug-21 10:07am    
Thanks. :)
Try to replace
C++
bmi = weight / (height * height) * 703;

with
C++
bmi = weight * 703 / (height * height);

With C/C++, a division of integers return an integer. To get the result as a float, you need to cast the integers.
 
Share this answer
 
v2
Comments
CPallini 31-Aug-21 2:11am    
5.(Note, there is missing '3' in your formula).
Patrice T 31-Aug-21 2:29am    
Thank you, typo
The problem is that integer division are making implicit type casts.

The easiest solution is to use float or double for all numbers in the equation ie weight and height.
 
Share this answer
 
Comments
CPallini 31-Aug-21 2:17am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900