代码编译正常,但遇到段错误 : 11 when run

标签 c

有人可以帮我解决这个代码吗?它编译得很好,但是当我尝试运行它时,它给了我段错误: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;
}

函数.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

最佳答案

更新:

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

scanf("%f",hours);

scanf("%f",payrate);

scanf("%f",defr);

其中hourspayratedefr指的是数值变量( float )。

这在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() 需要指向变量的指针,而不是它们的值,尽管对于字符串来说没有区别。某些编译器可能会替换恶意代码中的指针,使其能够与特定编译器一起运行,但此代码是不可移植的。

关于代码编译正常,但遇到段错误 : 11 when run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26607549/

相关文章:

无符号字符数组的 C memcpy 问题

c - 与素数相关的输出

c - 通用堆栈停止弹出值

c - C中函数调用的默认参数

c - 基本 C SDL2 程序不能与 TCC 一起工作,但它可以与 GCC (Linux) 一起工作

c - 大小 4 的无效读取

c - 有效指针的可能值是什么?

c - 放弃 C 中的 THE 规则有多糟糕(又名 : return 0 on success)?

c - 使用文件时出错

c - C 语言 hello world 程序的汇编代码