[previous_group] * [up] [next] [next_group]

Adding Debug Lines to the Code

To see for sure what values are being computed in our program, we can add extra printf() statements to the code to show the values of variables during execution.

For example, in our copmutation of the portfolio value, we multiply the number of shares by the stock price. We can break that computation into two parts to make sure the stock price is being computed correctly:

        price = (sell_whole + sell_numer / sell_denomin);

        portfolio_value = shares * price;

Don't forget to declare the new variable price.

Then we can print the value of price to verify it has the correct value. This is an extra printf statement in our code, which we will not keep in the final version of the program. We are only adding it here to debug. I generally do not indent these debug lines like the rest of the code, so that they stand out for removal later. Also, to make the output they generate stand out at run time, I indicate in the format string that this is debugging output. I would use a statement like:

printf("degug: price = %f\n",price);

When we compile and run the modified code, we get the following output:
You have 57 shares of XYZ Corp.
selling at $ 11 3/8
degug: price = 11.000000

The value of the stock is $627.000000

Hmmm.... the computer is calculating the stock price as 11.0000, not 11.375, as our calculator would show.

What's wrong?


[up] to Overview.

[next]