Thread: cout statement not working....i think...

  1. #1
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88

    cout statement not working....i think...

    ahhh...yeah this is really stupid but why is my program outputting zero for the variable pop?
    Code:
    #include <iostream>
    
    int main(void){
    
       using namespace std;
    
        //capture data
       cout << "Enter the world's population: ";
       long worlds_pop; cin >> worlds_pop;
       cout << "Enter the u.s. population: ";
       long us_pop; cin >> us_pop;
    
       //operate on data
       long pop = us_pop/worlds_pop;
     
       //ouput results
       cout << "The United States population is ";
       cout << pop << " % of the worlds population.";
       cout << endl;
       return 0;
    }
    edit: yeah, it's obviously not std::cout, but I'm thinking its the way I'm operating on long?
    Last edited by hex_dump; 05-20-2013 at 04:10 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Integer division. 1 / 2 -> 0
    Kurt

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    riiight...int division results in an integer response with the remainder truncated. If the divisor was smaller than the numerator it would have given me an int, but that's not the case here....i see. this is an example in a book that wants both populations to be of type long...even if i have the answer as a float and its <1% the data would still be truncated. At least that's what I'm getting. Is there a way to get around that....while keeping both populations as long? Sorry if it's obvioius but I've been in thinking mode all day. I could just be tired

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Then you should use
    Code:
    long pop = 100 * us_pop/worlds_pop;
    Kurt

  5. #5
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by ZuK View Post
    Then you should use
    Code:
    long pop = 100 * us_pop/worlds_pop;
    Kurt
    yeah thanks figured that out. long day.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to factor some float or double into the expression. You can do it by, for example

    (us_pop * 1.0)/worlds_pop

    or by casting the type of one variable

    us_pop/static_cast<double>(worlds_pop)

    The result will be a double, of course, so you have to cast it back to a long if you want to truncate it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expressions in cout statement vs variable
    By misterpogos in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2013, 02:36 AM
  2. Replies: 1
    Last Post: 07-15-2011, 10:46 AM
  3. std::cout not working
    By FoodDude in forum C++ Programming
    Replies: 9
    Last Post: 08-12-2005, 03:14 PM
  4. I'm REALLY confused. Why isn't cout or cin working here?
    By niudago in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2003, 05:53 PM
  5. cout not working properly
    By Yoshi in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2002, 10:04 AM