Thread: Problem storing values and dividing

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    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. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Quote Originally Posted by Elysia View Post
    >>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 subscribe to a feed

Similar Threads

  1. Storing values in an array
    By kpop in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 01:46 PM
  2. Storing values in memory
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 09-29-2006, 03:10 AM
  3. storing values.....NEED HELP.
    By new2tehgame in forum C++ Programming
    Replies: 9
    Last Post: 09-14-2005, 08:17 PM
  4. help storing and calculating hex values
    By anzas in forum C++ Programming
    Replies: 4
    Last Post: 07-14-2003, 01:41 PM
  5. storing values
    By hen in forum C Programming
    Replies: 4
    Last Post: 07-08-2002, 03:20 PM