Hello All, well this is my first post after a while of inactivity.

I have run into a problem. I have an assignment for school, but when I debug my program using Dev C++(BloodShed) it give me a segmentation fault.

I am reading from a file that looks like this:

0001:Satriani:Joe:6:38.0
0002:Vai:Steve:1:44.5
0003:Morse:Steve:10:50.0
0004:Van Halen:Eddie:3:25.75
0005:Petrucci:John:8:42.25
0006:Beck:Jeff:3:62.0

i read from the file using this:

fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);

* Not Sure weather this is causing the problem or not.

I have an array that contains pay rates.

I want to do this:

rate = payRates[pGrp-1];

While debugging it gives me a segmentation fault error and high lites this line. Is it possible to do what I want to do? If it is not is there a way that I can achieve the same result?

Thank You,

Edouard

OK it seems to me I have a problem with reading the file.
I hard coded some values to be passed into the calc_gross_to_pay() function and when I output to file it looks like this:

Emp# Last Name    First Name        Gross Pay         LTB          EI         
0001   ÿ"           X                  455.00        9.10        6.37              
Satr   ÿ"           X                  455.00        9.10        6.37              
iani   ÿ"           X                  455.00        9.10        6.37             
Joe    ÿ"           X                  455.00        9.10        6.37              
6      ÿ"           X                  455.00        9.10        6.37

* I have removed the last 4 columns due to formatting of the page*

As you can see the data in the Emp# column is from the first line of the data file that I was trying to read. Is there anything I can do to fix the problem in reading the data?

Here is my code for reading and out puting the data to a file.

main()
{    
    FILE *in;
    FILE *out;

    double payRates[NUMRATES];      
    int i, iMax = 0, iTotEmp;      
    char empNum[5];                 
    char lName[13];                 
    char fName[13];                 
    char lineC[50];                 
    int pGrp;                       
    double hrsWorked;               
    double grossPay, LTB, EI, CPP, FT, ded, netPay, junk, tNetPay, tGrossPay;

    get_PR(payRates);   
    in = fopen("employees.dat", "r");
    if(in == NULL)
    {
        printf("Error! Cant find the file employees.dat!\n");
        exit(1);
    }
   
    fgets(lineC, sizeof(lineC) - 1 ,in);
                                   
    while (! feof(in))
    {
        iMax++;
        fgets(lineC, sizeof(lineC) - 1 ,in);
    }
    rewind(in);                                 
    out = fopen("payroll.dat", "w");
    if(in == NULL)
    {
        printf("Error! Cant find the file payroll.dat!\n");
        exit(1);
    }
    else                          
    {
        fprintf(out, "Emp# Last Name    First Name        Gross Pay         LTB          EI         CPP          FT  Deductions     Net Pay\n");
    }    

    for(i = 0; i<iMax;i++)       
    {
        fscanf(in, "%4[^:]:%12[ ^:]:%12[ ^:]:%d:%lf\n", empNum, lName, fName, &pGrp, &hrsWorked);
 
        grossPay = calc_gross_ot_pay(PRATE, HRSWRKED);
        //PRATE and HRSWRKED are defined using #define
        LTB = calc_LTB(grossPay);
        EI = calc_EI(grossPay);
        CPP = calc_CPP(grossPay);
        FT = calc_FT(grossPay);
        ded = LTB + EI + CPP + FT;
        netPay = grossPay - ded;
        tNetPay += netPay;
        tGrossPay +=grossPay;
        fprintf(out, "%-5s %-13s %-13s %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf\n", empNum, lName, fName, grossPay, LTB, EI, CPP, FT, ded, netPay);
    }
    printf("# of employees: %d Total Net Pay %lf Total Gross Pay %lf", iMax, tNetPay, tGrossPay);

    fclose(out);
    fclose(in);
    return 1;
}

i read from the file using this:

fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);

I tested this and it works: fscanf(in, "%[^:]:%[a-zA-Z0-9]:%[a-zA-Z0-9]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked)

The SPACEs in the scanset seem to be the problem. Remove them and see if that works for you.

Also, check out this information on
main()
while (! feof(in))

And what is the purpose of reading the file first? Just read it once.
I would also suggest rather than using fscanf() you switch to fgets()/sscanf() combo. If an error occurs with fscanf() it's harder to recover.

Thanks a lot for the help you have both given me!

It was a space problem and the way I was getting the Last and First Name.
I changed my scanf code to this and it works flawlessly:

fscanf(in, "%[^:]:%[a-zA-Z a-zA-Z]:%[a-zA-Z]:%d:%lf\n", &empNum, &lName, &fName, &pGrp, &hrsWorked);

I also used the

while(!=feof(in))

you had suggested, the reason I was reading it twice was to find the length but using your suggestion I just added a counter to get over that and just read it once.

Once Again thank you both for your input.:)

Now its just a matter of tweaking some functions to get the correct output values.

Wow now in Unix it gives me a Syntax Error when I try to compile it...

Is there anything wrong with this?

void get_PR(double rates[]);        //Used to get pay rates and populates array.

Its right at the begining of the C source file:

#include <stdio.h>

void get_PR(double rates[]);        //Used to get pay rates and populates array.
double calc_gross_ot_pay(double pRate, double hrsWrk); 
				    //Used to calculate gross pay of employee.
double calc_LTB(double gross_pay);  //Used to calculate Long Term Benefits.
double calc_EI(double gross_pay);   //Used to calculate Employee Insurance.
double calc_CPP(double gross_pay);  //Used to calculate Canadian Pension Plan.
double calc_FT(double gross_pay);   //Used to calculate Fed. & Prov. Taxes.

int main()
{

Looks ok to me. What compiler and what is the error message?

I'm not sure which compiler, because I am remotely connecting to my school and using Unix from there. But it gives me a syntax error when I attempt to compile it.

*EDIT* I think its this:

VisualAge C++ Professional / C for AIX Compiler, Version 5

The AIX native C compiler does not like the // comment style for .c files, since its considers it a C++ comment and not a C style comment. I think that is probably the reason you're getting the errors and you will either need to change it to the /* */ comment style or using the compile flag -qcpluscmt.

See if that helps.

Wow that was it... the comments...god...

Thanks A lot for that response!

I also used the

while(!=feof(in))

you had suggested, the reason I was reading it twice was to find the length but using your suggestion I just added a counter to get over that and just read it once.

I would never suggest while(!=feof(in)) !!! See this and you'll see why.

I guess I mis read what you posted I will make the adjustment to my code as to not encounter that error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.