Working Through book, need help with a program

This is a discussion on Working Through book, need help with a program within the C Programming forums, part of the General Programming Boards category; Hi, I've just started teaching myself C. I'm very familiar with networking and hardware, and I decided to branch out ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Working Through book, need help with a program

    Hi,

    I've just started teaching myself C. I'm very familiar with networking and hardware, and I decided to branch out to programming. That said, I bought the book C Programming, A Visual Quickstart Guide by Larry Ullman and Marc Liyanage. I'm only on chapter 2, so my problem/code is all very simple, but I've already come up on an issue that I'd appreciate some help with, even though it is probably far below the normal scope of this board.

    Each chapter contains little programs that the reader basically copies word-for-word for the sake of practicing/explaining different concepts. The piece I'm working on is introducing constants, and is on page 32 and 33, if anyone happens to have the book/cares to look it up on Amazon.

    here's the code:

    Code:
    /* temperature.c - script 2.6
     * a script  to demonstrate constants */
    
    #include <stdio.h>
    
            int main (void) {
                    int fahrenheit = 212;
                    const int SUBTRACT_NUM = 32;
                    const int RATIO = 5.0/9.0;
    
                    printf ("%d degrees Fahrenheit is %0.1f degrees Celcius.\n", fahrenheit, (fahrenheit - SUBTRACT_NUM) * RATIO);
    
                    getchar();
                    return 0;
    }
    As I said, it's very simple, but I'm trying to go through and be thorough, so I don't miss anything. The issue that I'm having is that when I compile I get no errors, but when I actually execute the program, the output comes out as the following:

    Code:
     212 degrees Fahrenheit is 0.0 degrees Celcius.
    The example in the book (and actually doing the math) says that it should print this:

    Code:
    212 degrees Fahrenheit is 100 degrees Celcius.
    I have gone over the entire program, and am not seeing what I did wrong. Perhaps someone could provide me a fresh pair of eyes and see what I've done wrong?

    Also I'm compiling it using gcc on a Debian Lenny box. The output of gcc -v:

    Code:
     
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
    Thread model: posix
    gcc version 4.3.2 (Debian 4.3.2-1.1)
    I'm not sure if that's relevant, but I've always been one to include more info, than none at all. Thanks in advance for any help anyone can give me!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Code:
    #include <stdio.h>
    
            int main (void) {
                    int fahrenheit = 212;
                    const int SUBTRACT_NUM = 32;
                    const float RATIO = 5.0/9.0;
    
                    printf ("%d degrees Fahrenheit is %0.0f degrees Celcius.\n", fahrenheit, (fahrenheit - SUBTRACT_NUM) * RATIO);
    
                    getchar();
                    return 0;
    }
    It should be float instead of int and %.0.1f will display 1 decimal which you do not want.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Oh, wow.... thank you so much! A fresh pair of eyes always helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2006, 07:42 AM
  2. Replies: 6
    Last Post: 11-29-2004, 10:28 PM
  3. Book inventory program
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 08:47 PM
  4. calculator program from Bjarne Stroustrup's book
    By deduct in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2003, 01:43 AM
  5. Program logic not working..
    By ronkane in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2002, 08:31 PM

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