Closed Thread Icon

Preserved Topic: If only I got paid this much.. Pages that link to <a href="https://ozoneasylum.com/backlink?for=17356" title="Pages that link to Preserved Topic: If only I got paid this much.." rel="nofollow" >Preserved Topic: If only I got paid this much..\

 
Author Thread
Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 01-03-2003 20:36

I was writing a program (in C) that calculates the income given the salary and the hours worked (time and a half after 40 hours)..

... interesting way the math works here- work 40 hours a week at $5.15 and the total comes to $17,718,760.00!!

I'm sure I'll localize the bug eventually.. but it just kinda suprised me when I first ran it.. got me rechecking my paycheck (hey.. I'm getting underpaid here!)

Here's the code------------------------------------------------------------------------------------------------

#include<stdio.h>

int main(){
int hours;
int extrahours;
char getInfoLine[5];
float salary;
float income;

printf("Enter the hours: ");
fgets(getInfoLine,sizeof(getInfoLine),stdin);
sscanf(getInfoLine, "%d",&hours);
printf("Enter the hourly salary: ");
fgets(getInfoLine,sizeof(getInfoLine),stdin);
sscanf(getInfoLine, "%f",&salary);
printf("\nHours worked: %d\n",hours);
printf("\nSalary paid: %f\n",salary);

if (hours > 40){
extrahours = hours - 40;
hours = 40;
}

income = 0;
income = (hours * salary) + (extrahours * (salary * 1.5));

printf("\nYour check comes to: $%f\n",income);

return(0);
}


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 01-03-2003 20:45

Ah.. I seem to be working 2,293,664 extra hours a week...

what can I say... I'm a dedicated worker..

-apparently, when 'hours' is less than 40- the 'hours' variable doesn't get initialized...

Damn... and I was just about to call work, too...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 01-03-2003 21:29

Yeah, be very careful when using if without an else.

Even if I can't think of any single case where the else will be reached, I'll put a graceful exit in an else just to be sure.

Also, be very aware of all possible extreme cases when running through your program. For this one, for example, I'd test it on numbers way above 40, on numbers between 40 and 0, on 0, on numbers less than 0, and on 40 itself. How it reacts to each should help you track down the bug.

This becomes very important when dealing with more complicated programs and especially once you hit recursion. Keeping track of variable values and memory allocation starts becoming a pain later on.

« BackwardsOnwards »

Show Forum Drop Down Menu