# Thread: Problem storing values and dividing

1. ## Problem storing values and dividing

Hi!

I'm a newbie so bare with me...

I'm trying to complete a simple excersize given to me in the book i'm currently reading.

I'm supposed to store the value of a countrys population. Then gather out the percentage that countrys population holds when compared with the global population.

Anyway here's the code:

Code:
```#include <iostream>

long  swe_pop = 9644864;

int main ()

{
long  world_pop;
double perc_pop = swe_pop / world_pop;

using namespace std;

cout << "Enter the number of people living in the world today: ";
cin >> world_pop;
cout << "Swedens percentage of the global population is: " << perc_pop << "%";

return 0;
}```

The result I'm getting is 0%.

I was under the impression that long (or long long) integers could hold high values. And that I could then divide these and answer with a float type value. Giving space for the decimals.

Would be great if anybody could correct me where I'm wrong.

I've been studying programming on my own on and off so my knowledge is to say the least mildly weak.

2. Have you tried converting everything to doubles? Or casting your division's arguments as doubles?

Edit : It's because your division is an integer one so it automatically rounds. Because it's less than 0, it's 0. You are then casting this 0 as a double.

Edit edit : You are also doing things entirely out of order. You don't calculate the value before you gather the number itself...

3. >>double perc_pop = swe_pop / world_pop;
This says that at this very line, divide swe_pop by world_pop and store it in perc_pop.
C++ does not have a system where you specify relations between variables in hope that when one variable gets updated, then the result gets updated too. All lines are executed once and only when they're first encountered.

Your problem is because you are using integer math. If both operands are integers, then the decimals are thrown away before you store it in the double variable, hence your problem.
The easiest solution would be just to make swe_pop a double. It should also be const (or constexpr in C++11), btw. This makes it such that both sides are not integers. Hence, floating point division will be used and you will get your decimals.

4. Originally Posted by Elysia
>>double perc_pop = swe_pop / world_pop;
This says that at this very line, divide swe_pop by world_pop and store it in perc_pop.
C++ does not have a system where you specify relations between variables in hope that when one variable gets updated, then the result gets updated too. All lines are executed once and only when they're first encountered.

Your problem is because you are using integer math. If both operands are integers, then the decimals are thrown away before you store it in the double variable, hence your problem.
The easiest solution would be just to make swe_pop a double. It should also be const (or constexpr in C++11), btw. This makes it such that both sides are not integers. Hence, floating point division will be used and you will get your decimals.

Thank you both so much! This sort of help really makes you eager to keep on going when you know you can get great help here!

My latest reply was somehow not posted but it was about what you explained about variables holding relations between other variables.

Anyway, again thanks!

Popular pages Recent additions