Help a C++ coder see the light

Alan Morgan amorgan at Xenon.Stanford.EDU
Fri Jan 31 19:06:42 CET 2003


In article <m3of5yf6hs.fsf at ferret.object-craft.com.au>,
Dave Cole  <djc at object-craft.com.au> wrote:

>Ahhh yes, the excellent safety net of the compiler/linker.  It
>compiles, my work is done...
>
>- - calculate-wage.c - - - - - - - - - - - - - - - - - - - - -
>#include <stdio.h>
>
>float calculate_weekly_wage(float hours, float rate)
>{
>    if (hours > 37.5)
>	return 37.5 * rate + (37.5 - hours) * rate * 1.5;
>    return hours * rate;
>}
>
>int main(int argc, char *argv[])
>{
>    float hours = 45.0;
>    float rate = 10.0;
>
>    printf("Worked %.2f hours at $%.2f hourly rate.\n", hours, rate);
>    printf(" -> wage (including overtime) is $%.2f.\n",
>	   calculate_weekly_wage(hours, rate));
>    return 0;
>}
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>% cc calculate-wage.c -o calculate-wage   
>% ./calculate-wage 
>Worked 45.00 hours at $10.00 hourly rate.
> -> wage (including overtime) is $262.50.
>
>What!!!  How can that be!!!  It passed through the compiler/linker
>safety net.

It's obvious.  You had the arguments to calculate_weekly_wage backwards.

You should have written

    printf(" -> wage (including overtime) is $%.2f.\n",
	   calculate_weekly_wage(rate, hours));

Alan
-- 
Defendit numerus




More information about the Python-list mailing list