Variable help

This is a discussion on Variable help within the C Programming forums, part of the General Programming Boards category; hello everyone, im new to the whole programing thing, and I seem to be having problems with multiplying/dividing multiple integers. ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Variable help

    hello everyone, im new to the whole programing thing, and I seem to be having problems with multiplying/dividing multiple integers.
    The line that is giving me all the problem is
    Code:
    printf( "Message: %d", int1 * ( int2 / int3 );
    I have defined all three variables, and have it set up so the user enters all 3 ints, and then the program spits out the number. most of the time, int2 / int3 gets me a irrational number, (such as 1.66666666), then multiplies it by int1, usualy resulting in a integer.
    the only problem is that im trying to make the program work even when the output number is 199.99999, so how would i go about doing this? mabye rounding the out put to the nearest integer?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    What do you mean? If the result is 199.99999 then that is what you want to see or do you want to see the rounded integer value? Integer division will truncate anything after the decimal point, i.e 8/5 = 1, which will screw up your expected results. If you need to work with floating point value then use float/double variables instead of int variables.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    What do you mean? If the result is 199.99999 then that is what you want to see or do you want to see the rounded integer value? Integer division will truncate anything after the decimal point, i.e 8/5 = 1, which will screw up your expected results. If you need to work with floating point value then use float/double variables instead of int variables.
    Or, you can use a typecast (and change the format to float, or cast it back to integer later):
    Code:
    printf( "Message: %f", int1 * (float)( int2 / int3 ));
    or
    Code:
    printf( "Message: %d", (int)(int1 * (float)( int2 / int3 )));
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    10
    Thx much everyone, its working now.
    i changed int2 and int 3 to floating point, and used matsp's 2nd code line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

Tags for this Thread


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