Bug in basic C program

The program is supposed to calculate the height of a tree based on two individuals heights and distances from each other and from the tree. It uses a formula for similar triangles and does some unit conversion.

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
#include <stdio.h>

void main()
{
	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;			/*a whole number of inches*/
	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 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 eyes*/
	printf("Enter shorty's height up to his eyes, in whole feet: ");
	scanf("%ld", &feet);
	printf("		...and then inches: ");
	printf("%ld", &inches);
	shorty = 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*/
	tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty - shorty)/shorty_to_lofty;

	/*The code to display the result will go here*/
	printf("The height of the tree is %ld feet and %ld inches.\n", tree_height/inches_per_foot, tree_height% inches_per_foot);
}


The program runs like this in the console:
Enter lofty's height to the top of his head, in whole feet: 6
...and then inches: 4
Enter shorty's height up to his eyes, in whole feet: 4
...and then inches: 1505088Finally enter the distance to the tree to the nearest foot:


The program doesnt allow me to input a number for shorty's height in inches. after I input feet execution skips over inches and the number 1505088 appears along with the next printf() statement to enter the distance to the tree.

Last edited on
Line 25:
printf("%ld", &inches);

Shouldn't it be scanf?
yes thank you. im still bad at catching little mistakes like that. i just go right over them.
The best way to catch things like that is walk through your code step by step. When you compile and notice something unexpected, look back through your code at where the unexpected outcome came from. It might take a careful look, but some things will stick out like a sore thumb. I'm not a big fan of C I/O functions because they simply are almost identical to each other. C++ I/O is a little easier to catch errors and understand what's going on (it might just be because I'm used to C++).
Topic archived. No new replies allowed.