代码编译得很好但遇到了分段错误:11运行时

时间:2014-10-28 11:46:21

标签: c

可以somone请帮我这个代码?它编译得很好,但当我尝试运行它时,它给我分段错误:11。我不知道我做错了什么。我怀疑AddDetailToAccumulators功能是它的问题,但我似乎无法找到这个缺陷。

的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], answer = '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 is %.2f\n", regHrs);
         printf("Your overtime Hours is %.2f\n", ovtHrs);
         printf("Your Gross is: %.2f\n", Gross);
         printf("the Federal Tax is %.2f\n",FedTax);
         printf("the State Tax is %.2f\n",StateTax);
         printf("the SSI Tax is %.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);
    return 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 the 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)
    return (hours * payrate);
  else
    return 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);
}

我跑了它并得到了结果

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
Your reg Hours is 30.00
Your overtime Hours is 0.00
Your Gross is: 300.00
the Federal Tax is 42.75
the State Tax is 2.99
the SSI Tax is 22.09
Segmentation fault: 11

1 个答案:

答案 0 :(得分:-1)

更新

虽然在这个具体的环境中,分段错误似乎由于其他原因而发生(我错过了表明程序超出数据输入的输出列表),任何编写符合标准的C代码的人都必须避免

  

的scanf( “%F”,小时);

     

的scanf( “%F”,payrate);

     

的scanf( “%F”,DEFR);

其中hourspayratedefr是指数字变量(浮点数)。

这被描述为http://www.drpaulcarter.com/cs/common-c-errors.php#2.3.1处的编程错误 我测试了自己的代码

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

导致段错误或不读取数据(在不同的盒子上)。

scanf()需要指向变量的指针,而不是它们的值,尽管关于字符串没有区别。有些编译器可能会在流氓代码中替换指针,使其与特定编译器一起运行,但此代码是不可移植的。