Jump to content
  • Advertisement
Sign in to follow this  
Emotions06

Code Help.

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay so I am doing tutorials trying to learn C++ and am messing around with input and output fuctions and I thought I understood it quite well, but when I tried to write a simple code it doesn't seem to work properly. This is what I wrote.
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int weight;
    int height;
    
    cout << "Please enter your height in inches: ";
    cin >> height;
    cout << "Please enter your weight in pounds: ";
    cin >> weight;
    cout << "Your Body-mass index is: ";
    cout << (weight / (height * height)) * 703;
    
    getch ();
    return 0;
}
It compiles and runs. It first asks for the height and then I input it. Then it asks for the weight, so I input it. The it says "Your Body-mass index is: 0." Why does it always come to zero no matter what I input? At firstI had another variable as int formula and had that fomula assigned that integer, but it also didn't work. I figured it was becuase it was above the input, so the formula was running before it had any input for what the integers were and it assigned 0 to them. But even when I moved it like this it still always gives me a zero. Can someone explaint his please. Thank you. Also. The actual formula has weight divided by height squared. I know height to the second power is the same as height x height, but is there a way of writing to the second, or third, or higher power in C++ instead of having to write it as height * height?? Thanks on this also.

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the quick replys and yes float does work so thanks. Now why? Does int not work in this case because when it divides weight by (height * height) it goes into a very small decimal number and int doesn't support a decimal? or am I way off here? I just want/need to understand why it didn't work in the first place. Thanks again.

Anything on how to right squared, cubed, etc. without having to write height * height?

Emotions

Share this post


Link to post
Share on other sites
Any time you do math with an int that is stored in an int, any decimal parts are just discared. So 1.34534 is 1, 0.987643 is 0. So in this case you have a number that was between 1 and 0. This would always be dropped down to zero.

theTroll

Share this post


Link to post
Share on other sites
Alright that makes great sense. Since I was getting something like .3 or so when I divided weight by height ^ 2, it was being dropped to 0 so when I multiplied that by 703 I was get zero. Thanks alot.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!