【问题标题】:Simple Segmentation Fault, I can't seem to find the issue [duplicate]简单的分段错误,我似乎找不到问题[重复]
【发布时间】:2015-07-20 07:25:35
【问题描述】:

这可能有一个简单的解决方案,但作为菜鸟,我似乎无法找到问题所在。当我第一次运行程序时,一切都很好,但在scan("%f",hrs) 之后,我得到“分段错误(核心转储)”。任何帮助将不胜感激,谢谢。如果你也想看完整代码you can find it here.

这是我收到的警告:格式“%f”需要“float *”类型的参数,但参数 2 的类型为“double

float grossModule(float *hours, float payrate, float *overtimeHours);
void CalculateTaxes(float *gross, float *defr, float *fedtax, float *statetax, float *ssitax);
float calcFedtax(float gross, float defr);
float calcStatetax(float fedtax);
float calcSSItax(float gross, float defr);
float netModule(float *gross, float *ft, float *st, float *ssit);

int main(void)
{
    float dfr,ft,st,ssit,gross,hrs,pay,net,ovrtHrs;
    int counter = 0;
    float netTotal, payTotal, hoursTotal, overtimeTotal, grossTotal, fedTotal, stateTotal, ssiTotal, dfrTotal;
    float grossAvg, netAvg, payAvg, hoursAvg, overtimeAvg, fedAvg, stateAvg, ssiAvg, dfrAvg;
    char fName[11], lName[21], ask[4];
    FILE * myreport;
    myreport = fopen("reports.txt","wt");

fprintf(myreport, header1);
fprintf(myreport, header2);
fprintf(myreport, header3);

netTotal = payTotal = hoursTotal = overtimeTotal = grossTotal = fedTotal = stateTotal = ssiTotal = dfrTotal = 0;
dfr = ft = st = ssit = gross = hrs = pay = net = ovrtHrs = 0;

do
{
    counter++;
    printf("Please enter your FIRST name: ");
    scanf("%s",fName);
    printf("Please enter your LAST name: ");
    scanf("%s",lName);
    printf("Enter your payrate: \n");
    scanf("%f",pay);
    printf("Please enter your total hours: ");
    scanf("%f",hrs);
    printf("Please enter the amount of deferred earnings: ");
    scanf("%f",dfr);

【问题讨论】:

    标签: c segmentation-fault


    【解决方案1】:

    改变

     scanf("%f",pay);
     printf("Please enter your total hours: ");
     scanf("%f",hrs);
     printf("Please enter the amount of deferred earnings: ");
     scanf("%f",dfr);Change 
    

     scanf("%f", &pay);
     printf("Please enter your total hours: ");
     scanf("%f", &hrs);
     printf("Please enter the amount of deferred earnings: ");
     scanf("%f", &dfr);
    

    scanf 需要变量的地址而不是它的内容。

    顺便说一句,您的编译器应该为此发出警告。

    【讨论】:

    • 我认为确实如此,但作为我的菜鸟,我不太明白这个警告。谢谢!
    • @Dan 你为什么没有在你的代码中发布任何警告?
    • @MartinJames 抱歉,下次再做。我仍然习惯了这里的工作方式。我将编辑我的帖子并添加警告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多