Thread: newb help

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    18

    Question newb help

    I am writing a program to a class. It is pretty self explanatory on what is supposed to happen but the math is coming up completely wrong. Any help?

    Code:
    #include<stdio.h>
    #define POUNDS 16 // 1lbs = 16 oz
    #define KILOGRAMS 1000 // 1kilogram = 1000 grams
    #define OUNCE .028 // ounce/kilo
    #define GRAM 28.35 // ounce/gram
    int main(void)
    {
     int lbs, oz;
     float kg, g;
    
     printf("Enter pounds\n");
     scanf("%d", &lbs);
     printf("Enter ounces\n");
     scanf("%d",&oz);
     printf("The weight converted to kilograms is %d\n ",((lbs * POUNDS)+ oz)* OUNCE);
     printf("The weight converted to grams is %d\n ",((lbs * POUNDS)+ oz)* GRAM);
    
    return 0;

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    It'd be nice to see some input and output from when you ran it. I may be thinking of LISP, but you might need to change .028 to 0.028.

    Anyway, the problem is that you're multiplying an integer by a floating point. You need to tell C to change the int into a float before multiplying it by another float, otherwise it thinks you're multiplying by 0 and 28 (.028 and 28.35 rounded).

    Ex:
    (float)((lbs * POUNDS)+ oz) * OUNCE

    And...

    The printf statement will be printing a float, not a decimal, so replace %d with %f.


    you can get rid of the "float kg, g;" too, since you're not using them.
    Last edited by Epy; 10-02-2009 at 07:40 AM. Reason: mistake

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    18

    i/o

    Enter pounds
    1
    Enter ounces
    1
    The weight converted to kilograms is -1271310319
    The weight converted to grams is 858993460


    This is the i/o. I tried fixing what you had said the but numbers hadn't changed....

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Try working out your formulas on paper --- you will see they are bogus. It might help if you defined your constants better. Instead of "POUNDS" doesn't something like "OZ_PER_LB" sound more accurate?

    Your ((lbs * POUNDS)+ oz)* GRAM) ---- is this really what you want? You are adding "oz" to (lbs * POUNDS).

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    18

    Thanks!

    I didn't see the second half of the reply. Thanks! It ended up working. I'm still learning so I am sure i'll have more questions but thanks again!

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Just a minor niggle

    Code:
    ...
    #define OUNCE .028 // ounce/kilo
    #define GRAM 28.35 // ounce/gram
    ...

    I'm glad your problem has been solved, but I couldn't help but point this out. You mean kilo/ounce and gram/ounce rather than what you have. The constants are correct for what you are doing with them, but the notes are upside-down

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C network newb
    By htpw16 in forum Networking/Device Communication
    Replies: 5
    Last Post: 04-14-2009, 03:01 PM
  2. Hey, the newb is back again.
    By nifear4 in forum C Programming
    Replies: 13
    Last Post: 10-20-2008, 02:14 PM
  3. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. if your not newb you can help me
    By Klinerr1 in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2002, 12:09 AM