/* Compute the value of a stock holding, see King P. 40 */ #include <stdio.h> #define TRUE 1 int main () { int price, shares; double numerator, denominator, value; while (TRUE) { printf ("Enter a share price (must include a fraction): "); scanf ("%d%f/%f", &price, &numerator, &denominator); printf ("Enter number of shares: "); scanf ("%d", &shares); value = (price + numerator/denominator) * shares; printf ("Value of holding: $%.2f\n", value); } return 0; } /* ** int scanf (const char* format, ....); ** Reads any number of data items from stdin stream. ** Returns EOF if an error occurs or an end-of-file ** is reached during reading */ /* ** int printf (const char* format, ...); ** Writes output to stdout stream. ** Returns number of characters written ** Returns negative value if an error occurs */ /* Note that the output is wrong, but why? Enter a share price (must include a fraction): 15 3/8 Enter number of shares: 5 Value of holding: $75.00 Enter a share price (must include a fraction): 17 5/16 Enter number of shares: 7 Value of holding: $119.00 Enter a share price (must include a fraction): 19 7/ 16 Enter number of shares: 7 Value of holding: $133.01 Enter a share price (must include a fraction): 21 7 / 16 Disaster, infinite loop on my computer!! See King Section 22.3 for complete details on formatted i/o */