Finally enter the distance from Lofty to the tree to the nearest foot

Hi, I'm trying to understand and learn C, reading the book From novice to professional5th edition. In end of 2nd chapter is this one code. What I don't understand is why the last input haven't been done also for inches but just feet??? I don't understand. In my view I would take the same input for feet and inches as previous ones. Maybe I'm dumb. Besides the whole program to find solution based on similar triangles theorem is pretty inaccurate and confusing in real life scenario it wouldn't produce the right answer. If anyone will have the patience with me and try to explain why's that I would really appreciated. Many thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  /* math01.c
 *program calculating height of a tree
 *
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
	long shorty = 0L;
        // Shorty's height in inches
	long lofty = 0L;
	// Lofty's height in inches
	long feet = 0L;
	long inches = 0L;
	long shorty_to_lofty = 0L;
	// Distance from Shorty to Lofty in inches
	long lofty_to_tree = 0L;
	// Distance from Lofty to the tree in inches
	long tree_height = 0L;
	// 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 from Lofty 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;
}
Last edited on
mato9099 wrote:
What I don't understand is why the last input haven't been done also for inches but just feet???

Because the input wants the distance rounded to the nearest foot (read the prompt)
Last edited on
Topic archived. No new replies allowed.