Wednesday, 15 May 2013

c - code compiled fine but ran into segmentation fault: 11 when run -



c - code compiled fine but ran into segmentation fault: 11 when run -

can somone please help me code?. compiled fine when seek run it give me segmentation fault: 11. don't know did wrong. suspect adddetailtoaccumulators function problem of it, can't seem find flaw.

main.cpp

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "function.h" #define onscreenreport1 "employee pay reg hrs gross fed ssi net \n" #define onscreenreport2 "name rate ovt hrs pay state defr pay \n" #define onscreenreport3 "======== ===== ======= ======= ======= ====== =====\n" extern void calctax(float gross, float deffered, float *fedtax, float *statetax, float *ssitax); int main(void) { int numemps; float fedtax, statetax, ssitax, hours, payrate, defr, netpay,gross, reghrs, ovthrs, totreg, totovt, totpayr, totgross, totfed, totstate, totssi, totdefr, totnetp; char lastname[10], firstname[10], reply = 'y'; file *reportfile; printreportheadings(reportfile); initializeaccumulators(&totpayr, &totreg, &totovt, &totgross, &totfed,&totstate, &totssi, &totdefr, &totnetp); while (answer == 'y') { employeedata(lastname, firstname, &hours, &payrate, &defr); if (hours <= 40) { reghrs = hours; } else { reghrs = 40; ovthrs = hours - 40; } gross = calcgross(hours, payrate); calctax(gross, defr, &fedtax, &statetax, &ssitax); netpay = gross - fedtax - statetax - ssitax - defr; printf(onscreenreport1); printf(onscreenreport2); printf(onscreenreport3); printf("%s, %s %f %f %f %f %f %f\n", lastname, firstname, payrate, reghrs, gross, fedtax, ssitax, netpay); printf("your reg hours %.2f\n", reghrs); printf("your overtime hours %.2f\n", ovthrs); printf("your gross is: %.2f\n", gross); printf("the federal taxation %.2f\n",fedtax); printf("the state taxation %.2f\n",statetax); printf("the ssi taxation %.2f\n",ssitax); adddetailtoaccumulators(payrate, &totpayr, reghrs, &totreg, ovthrs, &totovt, gross, &totgross, fedtax, &totfed, statetax, &totstate, ssitax, &totssi, defr, &totdefr); numemps++; printf("have more employee? "); scanf("%c", &answer); } printsummaryreport(totpayr, totreg, totovt, totgross, totfed, totstate, totssi, totdefr, totnetp, numemps, reportfile); homecoming 0; }

function.h

void printreportheadings(file *reportfile) { reportfile =fopen("report.txt","w"); if (reportfile != null) { fprintf(reportfile, "employee pay reg hrs gross fed ssi net \n"); fprintf(reportfile, "name rate ovt hrs pay state defr pay \n"); fprintf(reportfile, "======== ===== ======= ======= ======= ====== =====\n"); } else { printf("could not open file.\n"); exit(0); } } void employeedata(char *lastname, char *firstname, float *hours, float *payrate, float *defr) { printf("last name?\n"); scanf("%s", lastname); printf("first name?\n"); scanf("%s", firstname); printf("hours?\n"); scanf("%f",hours); printf("payrate?\n"); scanf("%f",payrate); printf("defr?\n"); scanf("%f",defr); } float calcgross(float hours, float payrate)//module 3.4 { if (hours < 40) homecoming (hours * payrate); else homecoming payrate * 40 + (hours - 40)*1.5*payrate; } void initializeaccumulators(float *totpayr, float * totreg, float *totovt, float *totgross, float *totfed, float *totstate, float*totssi, float *totdefr, float *totnetp) { *totpayr = 0; *totreg = 0; *totovt = 0; *totgross = 0; *totfed = 0; *totstate = 0; *totssi = 0; *totdefr = 0; *totnetp = 0; } void adddetailtoaccumulators(float payrate, float *totpayr, float reghrs, float *totreg, float ovthrs, float*totovt, float gross, float *totgross, float fedtax, float *totfed, float statetax, float *totstate, float ssitax, float *totssi, float defr, float *totdefr) { *totpayr = *totpayr + payrate; *totreg = *totreg + reghrs; *totovt = *totovt + ovthrs; *totgross = *totgross + gross; *totfed = *totfed + fedtax; *totstate = *totstate + statetax; *totssi = *totssi + ssitax; *totdefr = *totdefr + defr; } void printsummaryreport(float totpayr, float totreg, float totovt, float totgross, float totfed, float totstate, float totssi, float totdefr, float totnetp, int numemmps, file *reportfile) { float avgpayr, avgreg, avgovt, avggross, avgfed, avgstate, avgssi, avgdefr, avgnetp; avgpayr = totpayr / numemmps; avgreg = totreg / numemmps; avgovt = totovt / numemmps; avggross = totgross / numemmps; avgfed = totfed / numemmps; avgstate = totstate / numemmps; avgssi = totssi / numemmps; avgdefr = totdefr / numemmps; avgnetp = totnetp / numemmps; fprintf(reportfile, "totals %f %f %f %f %f %f \n",totpayr, totreg,totgross, totfed,totssi,totnetp); fprintf(reportfile, "averages rate ovt hrs pay state defr pay \n"); fprintf(reportfile, "averages rate ovt hrs pay state defr pay \n"); fprintf(reportfile, "======== ===== ======= ======= ======= ====== =====\n"); fclose(reportfile); }

i ran , got result

last name? teo first name? dg hours? 30 payrate? 10 defr? 15 employee pay reg hrs gross fed ssi net name rate ovt hrs pay state defr pay ======== ===== ======= ======= ======= ====== ===== teo, dg 10.000000 30.000000 300.000000 42.750000 22.087500 217.170013 reg hours 30.00 overtime hours 0.00 gross is: 300.00 federal taxation 42.75 state taxation 2.99 ssi taxation 22.09 segmentation fault: 11

update:

although concrete environment segmentation fault appears occur other reason (i missed output listing indicates programme passed beyond info input), writing standards-compliant c code must avoid

scanf("%f",hours);

scanf("%f",payrate);

scanf("%f",defr);

where hours, payrate, defr refer numeric variables (float).

this described programming error @ http://www.drpaulcarter.com/cs/common-c-errors.php#2.3.1 , tested myself code

#include <stdio.h> float f = 0.; main() { scanf("%f", f); printf("f = %f\n", f); }

with "gcc" either leads segfaults or not read info (on different boxes).

scanf() needs pointers variables, not values, although respect strings there no difference. compilers perchance may substitute pointers in rogue code making run particular compiler, code non-portable.

c

No comments:

Post a Comment