[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