What's wrong with the following arithmetic operation?

I'm just trying to do a simple arithmetic operation but it returns me the 0 value. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
  main(){

 long Population_A, Population_B; 

 cin>>Population_A;
 cin>>Population_B;

 float Increase_A, Increase_B;

 Increase_A = (6*Population_A)/100; //Here is the problem because I receive 0.
 Increase_B = (3*Population_B)/100;

 }//main 
You don't say anything about what values you're giving Population_A and Population_B. But, let's assume they're <= 16.

What you're doing there is an integer division. All the numbers involved are integers, after all. This basically means that anything after the decimal point in a number gets thrown out (no rounding, there's actually a good reason for this).

Try adding .0F onto the end of those 100s. The 100s are integers, when the .0F is added, it becomes a floating-point number, which supports decimals. When one of the numbers in a division is floating-point number, the operation becomes a floating-point division, which should works as you'd like it too.

And... one more thing. Please change main() to int main(). Pretty please?

-Albatross
If your result should be smaller than 0, the problem may be that you are doing integer division. Both Population_A and Population_B are integers. And so are the constants in your expression.


use floating point variables for both populations, and rewrite the expressions to:

1
2
Increase_A = (6.0*Population_A)/100.0; 
Increase_B = (3.0*Population_B)/100.0;


Adding the .0 will tell the compiler its a float constant.

Hope it helps!
Thanks to all, it helped.
Topic archived. No new replies allowed.