i need help

This is a discussion on i need help within the C++ Programming forums, part of the General Programming Boards category; is dis correct? sum= (score1/num2) + (score2/num1);...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    i need help

    is dis correct?

    sum= (score1/num2) + (score2/num1);

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well that's a pretty incomplete question. Assuming all those variables are correctly declared, that line will compile with no errors. But it's hard to tell you if that's correct because I have no idea what you're trying to do or what kind of data is stored in those variables.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    trying to write a formula

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Just be careful with your variable types when you're doing division. It you use type int or type char, you won't get the correct result with fractions.

    For example: MyFloat = Int1/Int2; will confuse you, because the Int1/Int2 is done first, and the result is stored (in a temporary register) as an int. Everything to the right of the decimal point is "lost" before the value is assigned to MyFloat.

    The same thing can happen with integer constants.

    (1/2) + (1/2) + (1/2) will give you zero!

    (1.0/2.0) + (1.0/2.0) + (1.0/2.0) will give you 1.5
    Last edited by DougDbug; 03-03-2006 at 06:08 PM.

Popular pages Recent additions subscribe to a feed

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