Beginner C++ Programmer Not Getting Decimal Answers

This is a discussion on Beginner C++ Programmer Not Getting Decimal Answers within the C++ Programming forums, part of the General Programming Boards category; Hey I am starting to teach myself C++ for a school project and I have just started going through the ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Beginner C++ Programmer Not Getting Decimal Answers

    Hey I am starting to teach myself C++ for a school project and I have just started going through the book C++ Primer Plus and I am on Chapter 3 which is mostly talking about different ways to store variables like as a int or float. I am doing the problems they have at the end of the chapter. The one I am currently on asks to find the percent of the US population out of the Worlds population using long long to store the information. Here is my code.
    Code:
    #include <iostream>
    
    
    int main()
    {
        using namespace std;
        long long w_pop, us_pop;
        long long percent;
    
    
        cout<<"Enter current world's population: ";
        cin>> w_pop;
        cout<<endl<<"Enter current US population: ";
        cin>> us_pop;
    
    
        percent = (us_pop / w_pop) * 100;
    
    
        cout<<endl<<"The currecnt US population is " << percent;
        cout<<" percent of the world's population.";
        return 0;
    }
    When I run the program it works for when the answer equal 1, like when I type in 5 for the worlds population and 5 for the US population, but I am not able to get it to display any numbers that actually would show a percent, like I do 5 for the world's and a 1 for the US population it will display a 0. I have looked in the book but I did not see anything that might fix it that I have learned so far. Is this caused by how the computer treats long long? Could someone explain what is causing me not to get a decimal answer? Thank you

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    You are using integer math, there are no fractions. You should probably be using floating point numbers, I suggest double.

    Jim

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The problem is that you are using integer math. When all operands are integers, the resulting number will have its decimals chopped off, so anything x = [0, 1) will become 0.
    To fix it, at least one operand must be a float or double. You can change the type of your variables, or cast one of them to float or double, like static_cast<float>(us_pop).

    Also, consider moving variable declaration closer to where you need them. For example, removing "long long percent" and changing to "float percent = (us_pop / w_pop) * 100;".
    You probably don't need a long long for percent either, since it's likely going to be very small.
    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
    Dec 2011
    Posts
    34
    Oh that makes sense i didnt know long long counted as an int. I knew it would be something simple like that and i just kept on missing it when i looked in the book. Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Beginner Programmer....
    By Shodai100 in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2010, 02:54 AM
  2. Help. Beginner Programmer
    By n4rush0 in forum C Programming
    Replies: 16
    Last Post: 08-25-2010, 03:04 AM
  3. Why aren't my answers having decimal places
    By curls8 in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2010, 01:45 AM
  4. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21