[C] Let me understand the errors

This is a discussion on [C] Let me understand the errors within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to learn C, using a book called " C From Novice To Professionals 4th Edition" by ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    [C] Let me understand the errors

    Hi everyone,

    I'm trying to learn C, using a book called " C From Novice To Professionals 4th Edition" by Ivor Norton.

    In the end of the second chapter there is this example:

    Code:
    /* Program 2.18 Calculating the height of a tree */
    #include <stdio.h>
    int main(void)
    {
    long shorty = 0L; /* Shorty's height in inches*/
    long lofty = 0L; /* Lofty's height in inches*/
    long feet = 0L; /* A whole number of feet*/
    long inches = 0L;
    long shorty_to_lofty = 0; /* Distance from Shorty to Lofty in inches*/
    long lofty_to_tree = 0; /* Distance from Lofty to the tree in inches*/
    long tree_height = 0; /* Height of the tree in inches
    const long inches_per_foot = 12L;
    
    /* Get Lofty's height */
    printf("Enter Lofty's height to the top of his/her head, in whole feet: ");
    scanf("%ld", &feet);
    printf("          ...and then inches: ");
    scanf("%ld", &inches);
    lofty = feet*inches_per_foot + inches;
    
    /* Get Shorty's height up to his/her eyes */
    printf("Enter Shorty's height up to his/her eyes, in whole feet: ");
    scanf("%ld", &feet);
    printf("                 ... and then inches: ");
    scanf("%ld", &inches);
    shorty = feet*inches_per_foot + inches;
    
    /* Get the distance from Shorty to Lofty */
    printf("Enter the distance between Shorty and Lofty, in whole feet: ");
    scanf("%ld", &feet);
    printf("                 ... and then inches: ");
    scanf("%ld", &inches);
    shorty_to_lofty = feet*inches_per_foot + inches;
    
    /* Get the distance from Lofty to the tree */
    printf("Finally enter the distance to the tree to the nearest foot: ");
    scanf("%ld", &feet);
    lofty_to_tree = feet*inches_per_foot;
    
    
    /* Calculate the height of the tree in inches */
    tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty) shorty_to_lofty;
    
    /* Display the result in feet and inches*/
    
    printf("The height of the tree is %ld feet and %ld inches.\n",
    tree_height/inches_per_foot, tree_height% inches_per_foot);
    
    return 0;
    
    }
    Then the book say:

    Code:
    And there you have it. The output from the program looks something like this:
    -------------------------------------------------------------------------------
    Enter Lofty's height to the top of his/her head, in whole feet first: 6
    ... and then inches: 2
    Enter Shorty's height up to his/her eyes, in whole feet: 4
    ... and then inches: 6
    Enter the distance between Shorty and Lofty, in whole feet : 5
    ... and then inches: 0
    Finally enter the distance to the tree to the nearest foot: 20
    The height of the tree is 12 feet and 10 inches.
    ------------------------------------------------------------------------------
    But, my execution is a little bit different

    Code:
    -------------------------------------------------------------------------------
    Enter Lofty's height to the top of his/her head, in whole feet first: 6
    ... and then inches: 2
    Enter Shorty's height up to his/her eyes, in whole feet: 4
    ... and then inches: 6
    Enter the distance between Shorty and Lofty, in whole feet : 5
    ... and then inches: 0
    Finally enter the distance to the tree to the nearest foot: 20
    Floating Point Exception
    ------------------------------------------------------------------------------
    After this i've tried (using my limited skill ) to fix the problem on basis of lessons learned assuming that the problem was all about variables declarations, but i was not capable to make this work.

    I've searched on google and i've found that this example is here on Google Books.

    Now i ask to all of you where are the errors in this program and how to fix them.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    A floating-point exception is usually caused by trying to divide by 0 (either by dividing, or using the modulus operator "%"). This looks like the culprit:

    Code:
    tree_height% inches_per_foot
    You've set tree_height to 0 and not changed it - this will be what's caused your error.
    Last edited by JohnGraham; 02-04-2011 at 06:10 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,571
    My best guess at the right book; please try better on title and Author name in future.
    APRESS.COM : Beginning C: From Novice to Professional, Fourth Edition : 9781590597354


    From the website

    Code:
      /* Calculate the height of the tree in inches */
      tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty)/
                                                                     shorty_to_lofty;
    Your code is missing "/"

    Code:
    /* Calculate the height of the tree in inches */
    tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty) shorty_to_lofty;
    Last edited by stahta01; 02-04-2011 at 06:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 06:11 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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