Ok extremly simple piece of code just to test... help me out

This is a discussion on Ok extremly simple piece of code just to test... help me out within the C Programming forums, part of the General Programming Boards category; Ok, so I just got a new mac and I am using xcode to do this just to see how ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    Ok extremly simple piece of code just to test... help me out

    Ok, so I just got a new mac and I am using xcode to do this just to see how it works and this won't work.. it is very very simple, am I just overlooking something? Heres the code.

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
        float f, c;
    	
    	printf("Please enter a temperature in Farenhiet to be converted to Celsius\n");
    	printf("Temp: ");
    	scanf(" %f", &f);
    	c = (5/9) * (f -32);
    	printf("The temperature in Celsius is: %f \n", c);
    	
    	
        return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess: You are always getting zero?

    the expression (5/9) is zero, because both sides are integer values, and 5 is smaller than 9, so it goes "zero whole times".

    If you change it to say (5.0f/9.0f) it will be a "float" value instead, and thus calcualte to 0.5555555 (or thereabouts).

    You could also, if you wanted to do it in integer math, do ((f - 32) *5) / 9. Now only 32 and 33 f turn into 0 c - and this is correct, since that's 0 and 0.55 'C.

    I use this formula for "mental" f to c:
    c = (f - 30); c /= 2; c += c / 10;

    It's not precisely right, but it's pretty close - if it's easier to deduct 32 then I'll do that - but often one degree C won't make any differences either - what's important is if it's above or below zero, and if it's double digits, if it's a 1, 2, 3 or 4 at the beginning.

    --
    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.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Wow, good guess I was getting 0 every time.. and thanks for the help :]

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by alexnb185 View Post
    Wow, good guess I was getting 0 every time.. and thanks for the help :]
    Lucky guess, eh? It would of course have been much better if you actually said WHAT was going wrong. [Most new members forget to actually supply that information, and often post 50-100 line pieces of code saying "can you tell me how to fix this" - without actually saying what they want fixed - is it a syntax error, the code crashes (if so when?) or just prints the last line a little bit different from what they expected]. At least your code was understandable within 10 seconds of looking at it, and I could figure out what was the likely problem.

    --
    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.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Ha, well ok ya I was just wanting to see if like the debugger didn't pick something up I kinda didn't think about the zero thing. Ill make sure to be through next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 06:02 AM
  2. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 07:59 AM
  3. need help to understand a piece of code
    By rraajjiibb in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 11:18 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM

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