Thread: Program fails to show totals/averages

  1. #1
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8

    Program fails to show totals/averages

    Hello and good morning!


    The code is supposed to display the total and averages of the data as well, but cuts off due to an error in the code. The code should also:


    1)Print checks for all employees, one per page, sorted by lastname. The first check number, 100, is to be read from a company data file (see requirement 4). The border of each check is important and should not be omitted.


    2)Convert the net pay to a text string for each check printed.


    3)Print a reference code on each check. The reference code is obtained by combining the first letter of the lastname with all the consonants remaining after removing all vowels (a,e,i,o,u).


    4)Use the same employee data found in assignment 2. Use the following company data, obtained from a text file, for each check printed:


    Sabre Corporation
    15790 West Henness Lane
    New Corio, New Mexico 65790


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    typedef short BOOLEAN;
    typedef char STR15[15 + 1];
    typedef char STR10[10 + 1];
    typedef struct PayRecord
    {
        STR15 LastName;
        STR10 FirstName;
        float hours, payrate, deferred, gross, fedtax, statetax, ssitax, net;
        float reghrs, ovthrs;
        float totreg, totovt, totrate, totgross, totfed, totstate, totssi, totdefr, totnet;
    } PayRecord;
    
    
    #define FEDTAXRATE 0.15
    #define STATETAXRATE 0.07
    #define SSITAXRATE 0.0775
    #define MAXEMPLOYEES 6
    #define TRUE 1
    #define FALSE 0
    
    
    #define HEADLINE1       "Employee         Pay    Reg Hrs  Gross    Fed    SSI    Net\n"
    #define HEADLINE2       "Name             Rate   Ovt Hrs  Pay     State   Defr   Pay\n"
    #define HEADLINE3       "=========        ====== ======= ======= ======= ====== =======\n"
    #define EMPLOYEEREPORT  "%-16s %5.2f    %5.2f %7.2f  %6.2f %6.2f %7.2f\n"
    #define EMPLOYEEREPORT2 "                          %5.2f          %6.2f %6.2f\n\n"
    #define BREAK           "==============================================================\n"
    #define TOTALS1         "Totals           %5.2f   %6.2f %6.2f  %6.2f %6.2f %7.2f\n"
    #define TOTALS2         "                         %6.2f           %5.2f %6.2f\n\n"
    #define AVERAGES1       "Averages         %5.2f   %6.2f  %5.2f  %6.2f %6.2f %7.2f\n"
    #define AVERAGES2       "                         %6.2f           %5.2f %6.2f\n\n"
    
    
    #define BLANK    "*                               *\n"
    #define BORDER  "************************************************************\n"
    #define HEADER  "*                Check No %3d           *\n"
    #define HEADER2 "*   Sabre Corporation                       *\n"
    #define HEADER3 "*   15790 West Henness lane                   *\n"
    #define HEADER4 "*   New Corio, New Mexico 65790                   *\n"
    #define HEADER5 "*   Pay to the Order of                       *\n"
    #define HEADER6 "*   %-16s                       *\n"
    #define HEADER7 "*   The Sum Nine Hundred Ninety-Nine and XX/100 Dollars    *\n"
    #define HEADER8 "*   Reference: %-16s                   *\n"
    
    
    #define STUB     "*   Check No %3d                       *\n"
    #define STUB2    "*   %-16s       Reg Pay        Fed Tax       *\n"
    #define STUB3    "*   Address           Ovt Pay        State Tax  *\n"
    #define STUB4    "*   City, State           Gross Pay        SSI Tax    *\n"
    #define STUB5    "*   Zip               Net Pay        Tax Total  *\n"
    
    
    void PrintHeadings(FILE *reportFile)
    {
        fprintf(reportFile, HEADLINE1);
        fprintf(reportFile, HEADLINE2);
        fprintf(reportFile, HEADLINE3);
    }
    void InitAccumulators(PayRecord EmployeeData[], int maxemployees)
    {
        EmployeeData[MAXEMPLOYEES].totreg = 0;
        EmployeeData[MAXEMPLOYEES].totovt = 0;
        EmployeeData[MAXEMPLOYEES].totrate = 0;
        EmployeeData[MAXEMPLOYEES].totgross = 0;
        EmployeeData[MAXEMPLOYEES].totfed = 0;
        EmployeeData[MAXEMPLOYEES].totstate = 0;
        EmployeeData[MAXEMPLOYEES].totssi = 0;
        EmployeeData[MAXEMPLOYEES].totdefr = 0;
        EmployeeData[MAXEMPLOYEES].totnet = 0;
    }
    void InputEmployeeData(PayRecord EmployeeData[], int count)// 3.3
    {
        printf("Please enter your first and last name: ");
        scanf("%s%s", EmployeeData[count].FirstName, EmployeeData[count].LastName);
    
    
        printf("Please enter hours, payrate, and deferred amount: ");
        scanf("%f%f%f", &EmployeeData[count].payrate, &EmployeeData[count].hours, &EmployeeData[count].deferred);
        printf("\n");
    }
    void QSortEmployee(PayRecord EmployeeData[], int start, int finish)
    {
        int left = start,
            right = finish;
        char * pivot = EmployeeData[(start + finish) / 2].LastName;
    
    
        while (left < right)
        {
            while ((strcmp(EmployeeData[left].LastName, pivot) < 0) && (left < right))
            {
                left++;
            }
            while ((strcmp(EmployeeData[right].LastName, pivot) > 0) && (right > left))
            {
                right--;
            }
            if (left <= right)
            {
                PayRecord tempEmployee = EmployeeData[left];
                EmployeeData[left] = EmployeeData[right];
                EmployeeData[right] = tempEmployee;
                left++;
                right--;
            }
        }
        if (start < finish)
        {
            QSortEmployee(EmployeeData, start, right);
        }
        if (left < finish)
        {
            QSortEmployee(EmployeeData, left, finish);
        }
    }
    float CalculateGross(PayRecord EmployeeData[], int count) // 3.4
    {
        return (EmployeeData[count].hours <= 40) ? EmployeeData[count].hours * EmployeeData[count].payrate :
            EmployeeData[count].payrate * 40 + (EmployeeData[count].hours - 40) * 1.5 * EmployeeData[count].payrate;
    }
    
    
    void CalculateHours(PayRecord EmployeeData[], int count)
    {
        if (EmployeeData[count].hours <= 40)
        {
            EmployeeData[count].reghrs = EmployeeData[count].hours;
            EmployeeData[count].ovthrs = 0;
        }
        else
        {
            EmployeeData[count].reghrs = 40;
            EmployeeData[count].ovthrs = (EmployeeData[count].hours - 40);
        }
    }
    float CalculateNet(PayRecord EmployeeData[], int count)
    {
        return EmployeeData[count].gross - (EmployeeData[count].fedtax + EmployeeData[count].statetax + EmployeeData[count].ssitax + EmployeeData[count].deferred);
    }
    void AddAccumulators(PayRecord EmployeeData[], int count, int maxemployees)
    {
        EmployeeData[MAXEMPLOYEES].totreg = EmployeeData[MAXEMPLOYEES].totreg + EmployeeData[count].reghrs;
        EmployeeData[MAXEMPLOYEES].totovt = EmployeeData[MAXEMPLOYEES].totovt + EmployeeData[count].ovthrs;
        EmployeeData[MAXEMPLOYEES].totrate = EmployeeData[MAXEMPLOYEES].totrate + EmployeeData[count].payrate;
        EmployeeData[MAXEMPLOYEES].totgross = EmployeeData[MAXEMPLOYEES].totgross + EmployeeData[count].gross;
        EmployeeData[MAXEMPLOYEES].totfed = EmployeeData[MAXEMPLOYEES].totfed + EmployeeData[count].fedtax;
        EmployeeData[MAXEMPLOYEES].totstate = EmployeeData[MAXEMPLOYEES].totstate + EmployeeData[count].statetax;
        EmployeeData[MAXEMPLOYEES].totssi = EmployeeData[MAXEMPLOYEES].totssi + EmployeeData[count].ssitax;
        EmployeeData[MAXEMPLOYEES].totdefr = EmployeeData[MAXEMPLOYEES].totdefr + EmployeeData[count].deferred;
        EmployeeData[MAXEMPLOYEES].totnet = EmployeeData[MAXEMPLOYEES].totnet + EmployeeData[count].net;
    }
    
    
    void showReport(FILE *reportFile, char *FullName, PayRecord EmployeeData[], int count) //3.8
    {
        printf(HEADLINE1);
        printf(HEADLINE2);
        printf(HEADLINE3);
        printf(EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
        printf(EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
        fprintf(reportFile, EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
        fprintf(reportFile, EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
    }
    void printTotals(FILE *reportFile, PayRecord EmployeeData[], int maxemployees)
    {
        printf(BREAK);
        printf(TOTALS1, EmployeeData[MAXEMPLOYEES].totrate, EmployeeData[MAXEMPLOYEES].totreg, EmployeeData[MAXEMPLOYEES].totgross, EmployeeData[MAXEMPLOYEES].totfed, EmployeeData[MAXEMPLOYEES].totssi, EmployeeData[MAXEMPLOYEES].totnet);
        printf(TOTALS2, EmployeeData[MAXEMPLOYEES].totovt, EmployeeData[MAXEMPLOYEES].totstate, EmployeeData[MAXEMPLOYEES].totdefr);
        fprintf(reportFile, BREAK);
        fprintf(reportFile, TOTALS1, EmployeeData[MAXEMPLOYEES].totrate, EmployeeData[MAXEMPLOYEES].totreg, EmployeeData[MAXEMPLOYEES].totgross, EmployeeData[MAXEMPLOYEES].totfed, EmployeeData[MAXEMPLOYEES].totssi, EmployeeData[MAXEMPLOYEES].totnet);
        fprintf(reportFile, TOTALS2, EmployeeData[MAXEMPLOYEES].totovt, EmployeeData[MAXEMPLOYEES].totstate, EmployeeData[MAXEMPLOYEES].totdefr);
    }
    void printAverages(FILE *reportFile, int maxemployees, PayRecord EmployeeData[])
    {
        printf(AVERAGES1, (EmployeeData[MAXEMPLOYEES].totrate / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totreg / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totgross / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totfed / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totssi / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totnet / (MAXEMPLOYEES - 1)));
        printf(AVERAGES2, (EmployeeData[MAXEMPLOYEES].totovt / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totstate / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totdefr / (MAXEMPLOYEES - 1)));
        fprintf(reportFile, AVERAGES1, (EmployeeData[MAXEMPLOYEES].totrate / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totreg / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totgross / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totfed / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totssi / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totnet / (MAXEMPLOYEES - 1)));
        fprintf(reportFile, AVERAGES2, (EmployeeData[MAXEMPLOYEES].totovt / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totstate / (MAXEMPLOYEES - 1)), (EmployeeData[MAXEMPLOYEES].totdefr / (MAXEMPLOYEES - 1)));
    }
    float CalcFedtax(PayRecord EmployeeData[], int count) // 3.5.1
    {
        return (EmployeeData[count].gross - EmployeeData[count].deferred)*FEDTAXRATE;
    }
    float CalcStatetax(PayRecord EmployeeData[], int count) // 3.5.2
    {
        return EmployeeData[count].fedtax * STATETAXRATE;
    }
    float CalcSSItax(PayRecord EmployeeData[], int count) // 3.5.3
    {
        return (EmployeeData[count].gross - EmployeeData[count].deferred)........ITAXRATE;
    }
    void CalculateTaxes(PayRecord EmployeeData[], int count) //3.5
    {
        EmployeeData[count].fedtax = CalcFedtax(EmployeeData, count); // call 3.5.1
        EmployeeData[count].statetax = CalcStatetax(EmployeeData, count); // call 3.5.2
        EmployeeData[count].ssitax = CalcSSItax(EmployeeData, count); // call 3.5.3
    }
    //====================
    
    
    void printCheckHeader(PayRecord EmployeeData[], char *CheckName, int chkNo, char *refCode, int count)
    {
        printf(BORDER);
        printf(HEADER, chkNo);
        printf(BLANK);
        printf(HEADER2);
        printf(HEADER3);
        printf(HEADER4);
        printf(BLANK);
        printf(BLANK);
        printf(HEADER5);
        printf(BLANK);
        printf(HEADER6, CheckName);
        printf(BLANK);
        printf(HEADER7);
        printf(BLANK);
        printf(BLANK);
        printf(HEADER8, refCode);
        printf(BLANK);
        printf(BLANK);
        printf(BORDER);
    }
    void printCheckStub(PayRecord EmployeeData[], char *CheckName, int chkNo, int count)
    {
        printf(STUB, chkNo);
        printf(BLANK);
        printf(STUB2, CheckName);
        printf(STUB3);
        printf(STUB4);
        printf(STUB5);
        printf(BLANK);
        printf(BLANK);
        printf(BORDER);
        printf("\n");
    }
    BOOLEAN isVowel(char aChar)
    {
        switch (toupper(aChar)) //<ctype.h>
        {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U': return TRUE; break;
        default: return FALSE;
        }
    }
    char *generateRefCode(PayRecord EmployeeData[], int chkNo, int count)
    {
    
    
        static char tempStr[15 + 10 + 1];
        char buffer[10 + 1];
        int i, j = 1;
    
    
        //This loop is used to clear the string everytime generateRefCode is called
        for (i = 0; i < 31; i++)
        {
            tempStr[i] = 0;
        }
        //tempStr[0] = lastname[0]; // Needs to put the first letter into the temporary string.
        tempStr[0] = toupper(EmployeeData[count].LastName[0]);
        // This should be the first address that holds the first letter of the last name.
        // LOOP
        // Need to get through all the characters in the lastname.
        // Check if the letter is a vowl. If it is, add to the temporary string, else omit it.
        // ENDLOOP
        for (i = 1; i < strlen(EmployeeData[count].LastName); i++)
        {
            if (!isVowel(EmployeeData[count].LastName[i]))
            {
                tempStr[j++] = toupper(EmployeeData[count].LastName[i]);
            }
        }
    
    
        sprintf(buffer, "%d", chkNo);
        //sprintf is used to make int chkNo into a string using a char buffer
        strcat(tempStr, buffer);
        tempStr[j + 3] = 0;
        // add string terminator, adds the the current subscript+3 because we add
        // 3 numbers to the end of the string so that it adds the string terminator
        // to the end of the string.
        return tempStr;
    }
    
    
    int main(void)
    {
        PayRecord EmployeeData[MAXEMPLOYEES];
        char FullName[15 + 10 + 1];
        char CheckName[15 + 10 + 1];
        char *refCode;
        int count;
        int chkNo = 100;
        FILE *reportFile; // Read as "reportFile is a pointer to a file"
    
    
        reportFile = fopen("./report.txt", "wt");// open the file for "write-mode" access
    
    
        if (reportFile == NULL)// Test if file DID NOT open, exit.
        {
            printf(" report file open failed ...\n");
            fflush(stdin);
            exit(-30); //reqs <stdlib.h>
        }
    
    
        PrintHeadings(reportFile);//Call 3.1
        InitAccumulators(EmployeeData, MAXEMPLOYEES);//call 3.2
        for (count = 0; count < (MAXEMPLOYEES - 1); count++)
        {
            InputEmployeeData(EmployeeData, count);
        }
    
    
        QSortEmployee(EmployeeData, 0, (MAXEMPLOYEES - 2));
    
    
        for (count = 0; count < (MAXEMPLOYEES - 1); count++)
        {
            strcpy(FullName, EmployeeData[count].LastName);
            strcat(FullName, ", ");
            strcat(FullName, EmployeeData[count].FirstName);
            EmployeeData[count].gross = CalculateGross(EmployeeData, count);// Call 3.4
            //CalculateHours() does not actually calculate any hours
            //It seperates the hours into regular hours and overtime hours
            CalculateHours(EmployeeData, count);// Call 3.4.1
            CalculateTaxes(EmployeeData, count); // call 3.5
            EmployeeData[count].net = CalculateNet(EmployeeData, count);//call 3.6
            AddAccumulators(EmployeeData, count, MAXEMPLOYEES);//call 3.7
            showReport(reportFile, FullName, EmployeeData, count);//call 3.8
        }
    
    
        for (count = 0; count < (MAXEMPLOYEES - 1); count++)
        {
            refCode = generateRefCode(EmployeeData, chkNo, count);
            strcpy(CheckName, EmployeeData[count].FirstName);
            strcat(CheckName, " ");
            strcat(CheckName, EmployeeData[count].LastName);
            printCheckHeader(EmployeeData, CheckName, chkNo, refCode, count);
            printCheckStub(EmployeeData, CheckName, chkNo, count);
            chkNo++;
        }
    
    
        printTotals(reportFile, EmployeeData, MAXEMPLOYEES);//call 3.8.1
        printAverages(reportFile, MAXEMPLOYEES, EmployeeData);//call 3.8.2
        fclose(reportFile);// Close file
        fflush(stdin);
        getchar();
        return 0;
    }

    And here's where the program stops.
    http://i.imgur.com/pw0AXXy.png

    Now as far as where I went wrong in here, I am having a hard time figuring out. Can anyone please help me guide me in the right direction?

    Output I'm expecting: Program to display totals and averages (worked before I turned everything into an array), now exits with error before totals/averages are displayed. Program also writes up a report.txt file that also writes this information into it, as well as using a quicksort to organize the names alphabetically, and print paychecks (all of the #BORDER, #HEADER1-8, #STUB, etc). In that check a reference code is also generated (teacher gave us code, we just had to modify for our final program as seen here)

    Flow of the program:
    Print headings to label all of the input data we will enter soon under the categories as listed in the heading. Initialize all of our totals (in our array) to value of 0. These will be added in a loop "AddAccumulators" which takes our value for the hours, payrate, and taxes from each employee array structure and adds it to the total array. Program will write this information into the report.txt file with the HEADER line for totals, and should be displaying it in that picture. (Same for averages). After all of the data is calculated, the Reference code, Totals, Averages, and individual employee data is taken and put into printing out a Check Header and Stub.

    Thanks and apologies for the long code.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int myarray[10];
    If you declare an array as size 10 the last legal location is myarray[9] NOT myarray[10].


    Tim S.
    Last edited by stahta01; 05-17-2014 at 02:03 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    typedef short BOOLEAN;
    typedef char STR15[15 + 1];
    typedef char STR10[10 + 1];
    typedef struct PayRecord
    {
    	STR15 LastName;
    	STR10 FirstName;
    	float hours, payrate, deferred, gross, fedtax, statetax, ssitax, net;
    	float reghrs, ovthrs;
    	float totreg, totovt, totrate, totgross, totfed, totstate, totssi, totdefr, totnet;
    } PayRecord;
    
    
    #define FEDTAXRATE 0.15
    #define STATETAXRATE 0.07
    #define SSITAXRATE 0.0775
    #define MAXEMPLOYEES 5
    #define TRUE 1
    #define FALSE 0
    
    
    #define HEADLINE1       "Employee         Pay    Reg Hrs  Gross    Fed    SSI    Net\n"
    #define HEADLINE2       "Name             Rate   Ovt Hrs  Pay     State   Defr   Pay\n"
    #define HEADLINE3       "=========        ====== ======= ======= ======= ====== =======\n"
    #define EMPLOYEEREPORT  "%-16s %5.2f    %5.2f %7.2f  %6.2f %6.2f %7.2f\n"
    #define EMPLOYEEREPORT2 "                          %5.2f          %6.2f %6.2f\n\n"
    #define BREAK           "==============================================================\n"
    #define TOTALS1         "Totals           %5.2f   %6.2f %6.2f  %6.2f %6.2f %7.2f\n"
    #define TOTALS2         "                         %6.2f           %5.2f %6.2f\n\n"
    #define AVERAGES1       "Averages         %5.2f   %6.2f  %5.2f  %6.2f %6.2f %7.2f\n"
    #define AVERAGES2       "                         %6.2f           %5.2f %6.2f\n\n"
    
    
    #define BLANK	"*							   *\n"
    #define BORDER  "************************************************************\n"
    #define HEADER  "*				Check No %3d		   *\n"
    #define HEADER2 "*   Sabre Corporation					   *\n"
    #define HEADER3 "*   15790 West Henness lane				   *\n"
    #define HEADER4 "*   New Corio, New Mexico 65790				   *\n"
    #define HEADER5 "*   Pay to the Order of					   *\n"
    #define HEADER6 "*   %-16s					   *\n"
    #define HEADER7 "*   The Sum Nine Hundred Ninety-Nine and XX/100 Dollars    *\n"
    #define HEADER8 "*   Reference: %-16s				   *\n"
    
    
    #define STUB 	"*   Check No %3d					   *\n"
    #define STUB2	"*   %-16s	   Reg Pay		Fed Tax	   *\n"
    #define STUB3	"*   Address		   Ovt Pay		State Tax  *\n"
    #define STUB4	"*   City, State		   Gross Pay		SSI Tax    *\n"
    #define STUB5	"*   Zip			   Net Pay		Tax Total  *\n"
    
    
    void PrintHeadings(FILE *reportFile)
    {
    	fprintf(reportFile, HEADLINE1);
    	fprintf(reportFile, HEADLINE2);
    	fprintf(reportFile, HEADLINE3);
    }
    void InitAccumulators(PayRecord EmployeeData[], int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES].totreg = 0;
    	EmployeeData[MAXEMPLOYEES].totovt = 0;
    	EmployeeData[MAXEMPLOYEES].totrate = 0;
    	EmployeeData[MAXEMPLOYEES].totgross = 0;
    	EmployeeData[MAXEMPLOYEES].totfed = 0;
    	EmployeeData[MAXEMPLOYEES].totstate = 0;
    	EmployeeData[MAXEMPLOYEES].totssi = 0;
    	EmployeeData[MAXEMPLOYEES].totdefr = 0;
    	EmployeeData[MAXEMPLOYEES].totnet = 0;
    }
    void InputEmployeeData(PayRecord EmployeeData[], int count)// 3.3
    {
    	printf("Please enter your first and last name: ");
    	scanf("%s%s", EmployeeData[count].FirstName, EmployeeData[count].LastName);
    
    
    	printf("Please enter hours, payrate, and deferred amount: ");
    	scanf("%f%f%f", &EmployeeData[count].payrate, &EmployeeData[count].hours, &EmployeeData[count].deferred);
    	printf("\n");
    }
    void QSortEmployee(PayRecord EmployeeData[], int start, int finish)
    {
    	int left = start,
    		right = finish;
    	char * pivot = EmployeeData[(start + finish) / 2].LastName;
    
    
    	while (left < right)
    	{
    		while ((strcmp(EmployeeData[left].LastName, pivot) < 0) && (left < right))
    		{
    			left++;
    		}
    		while ((strcmp(EmployeeData[right].LastName, pivot) > 0) && (right > left))
    		{
    			right--;
    		}
    		if (left <= right)
    		{
    			PayRecord tempEmployee = EmployeeData[left];
    			EmployeeData[left] = EmployeeData[right];
    			EmployeeData[right] = tempEmployee;
    			left++;
    			right--;
    		}
    	}
    	if (start < finish)
    	{
    		QSortEmployee(EmployeeData, start, right);
    	}
    	if (left < finish)
    	{
    		QSortEmployee(EmployeeData, left, finish);
    	}
    }
    float CalculateGross(PayRecord EmployeeData[], int count) // 3.4
    {
    	return (EmployeeData[count].hours <= 40) ? EmployeeData[count].hours * EmployeeData[count].payrate :
    		EmployeeData[count].payrate * 40 + (EmployeeData[count].hours - 40) * 1.5 * EmployeeData[count].payrate;
    }
    
    
    void CalculateHours(PayRecord EmployeeData[], int count)
    {
    	if (EmployeeData[count].hours <= 40)
    	{
    		EmployeeData[count].reghrs = EmployeeData[count].hours;
    		EmployeeData[count].ovthrs = 0;
    	}
    	else
    	{
    		EmployeeData[count].reghrs = 40;
    		EmployeeData[count].ovthrs = (EmployeeData[count].hours - 40);
    	}
    }
    float CalculateNet(PayRecord EmployeeData[], int count)
    {
    	return EmployeeData[count].gross - (EmployeeData[count].fedtax + EmployeeData[count].statetax + EmployeeData[count].ssitax + EmployeeData[count].deferred);
    }
    void AddAccumulators(PayRecord EmployeeData[], int count, int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES].totreg = EmployeeData[MAXEMPLOYEES].totreg + EmployeeData[count].reghrs;
    	EmployeeData[MAXEMPLOYEES].totovt = EmployeeData[MAXEMPLOYEES].totovt + EmployeeData[count].ovthrs;
    	EmployeeData[MAXEMPLOYEES].totrate = EmployeeData[MAXEMPLOYEES].totrate + EmployeeData[count].payrate;
    	EmployeeData[MAXEMPLOYEES].totgross = EmployeeData[MAXEMPLOYEES].totgross + EmployeeData[count].gross;
    	EmployeeData[MAXEMPLOYEES].totfed = EmployeeData[MAXEMPLOYEES].totfed + EmployeeData[count].fedtax;
    	EmployeeData[MAXEMPLOYEES].totstate = EmployeeData[MAXEMPLOYEES].totstate + EmployeeData[count].statetax;
    	EmployeeData[MAXEMPLOYEES].totssi = EmployeeData[MAXEMPLOYEES].totssi + EmployeeData[count].ssitax;
    	EmployeeData[MAXEMPLOYEES].totdefr = EmployeeData[MAXEMPLOYEES].totdefr + EmployeeData[count].deferred;
    	EmployeeData[MAXEMPLOYEES].totnet = EmployeeData[MAXEMPLOYEES].totnet + EmployeeData[count].net;
    }
    
    
    void showReport(FILE *reportFile, char *FullName, PayRecord EmployeeData[], int count) //3.8
    {
    	printf(HEADLINE1);
    	printf(HEADLINE2);
    	printf(HEADLINE3);
    	printf(EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
    	printf(EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
    	fprintf(reportFile, EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
    	fprintf(reportFile, EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
    }
    void printTotals(FILE *reportFile, PayRecord EmployeeData[], int maxemployees)
    {
    	printf(BREAK);
    	printf(TOTALS1, EmployeeData[MAXEMPLOYEES].totrate, EmployeeData[MAXEMPLOYEES].totreg, EmployeeData[MAXEMPLOYEES].totgross, EmployeeData[MAXEMPLOYEES].totfed, EmployeeData[MAXEMPLOYEES].totssi, EmployeeData[MAXEMPLOYEES].totnet);
    	printf(TOTALS2, EmployeeData[MAXEMPLOYEES].totovt, EmployeeData[MAXEMPLOYEES].totstate, EmployeeData[MAXEMPLOYEES].totdefr);
    	fprintf(reportFile, BREAK);
    	fprintf(reportFile, TOTALS1, EmployeeData[MAXEMPLOYEES].totrate, EmployeeData[MAXEMPLOYEES].totreg, EmployeeData[MAXEMPLOYEES].totgross, EmployeeData[MAXEMPLOYEES].totfed, EmployeeData[MAXEMPLOYEES].totssi, EmployeeData[MAXEMPLOYEES].totnet);
    	fprintf(reportFile, TOTALS2, EmployeeData[MAXEMPLOYEES].totovt, EmployeeData[MAXEMPLOYEES].totstate, EmployeeData[MAXEMPLOYEES].totdefr);
    }
    void printAverages(FILE *reportFile, int maxemployees, PayRecord EmployeeData[])
    {
    	printf(AVERAGES1, (EmployeeData[MAXEMPLOYEES].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totnet / MAXEMPLOYEES));
    	printf(AVERAGES2, (EmployeeData[MAXEMPLOYEES].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totdefr / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES1, (EmployeeData[MAXEMPLOYEES].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totnet / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES2, (EmployeeData[MAXEMPLOYEES].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES].totdefr / MAXEMPLOYEES));
    }
    float CalcFedtax(PayRecord EmployeeData[], int count) // 3.5.1
    {
    	return (EmployeeData[count].gross - EmployeeData[count].deferred)*FEDTAXRATE;
    }
    float CalcStatetax(PayRecord EmployeeData[], int count) // 3.5.2
    {
    	return EmployeeData[count].fedtax * STATETAXRATE;
    }
    float CalcSSItax(PayRecord EmployeeData[], int count) // 3.5.3
    {
    	return (EmployeeData[count].gross - EmployeeData[count].deferred)........ITAXRATE;
    }
    void CalculateTaxes(PayRecord EmployeeData[], int count) //3.5
    {
    	EmployeeData[count].fedtax = CalcFedtax(EmployeeData, count); // call 3.5.1
    	EmployeeData[count].statetax = CalcStatetax(EmployeeData, count); // call 3.5.2
    	EmployeeData[count].ssitax = CalcSSItax(EmployeeData, count); // call 3.5.3
    }
    //====================
    
    
    void printCheckHeader(PayRecord EmployeeData[], char *CheckName, int chkNo, char *refCode, int count)
    {
    	printf(BORDER);
    	printf(HEADER, chkNo);
    	printf(BLANK);
    	printf(HEADER2);
    	printf(HEADER3);
    	printf(HEADER4);
    	printf(BLANK);
    	printf(BLANK);
    	printf(HEADER5);
    	printf(BLANK);
    	printf(HEADER6, CheckName);
    	printf(BLANK);
    	printf(HEADER7);
    	printf(BLANK);
    	printf(BLANK);
    	printf(HEADER8, refCode);
    	printf(BLANK);
    	printf(BLANK);
    	printf(BORDER);
    }
    void printCheckStub(PayRecord EmployeeData[], char *CheckName, int chkNo, int count)
    {
    	printf(STUB, chkNo);
    	printf(BLANK);
    	printf(STUB2, CheckName);
    	printf(STUB3);
    	printf(STUB4);
    	printf(STUB5);
    	printf(BLANK);
    	printf(BLANK);
    	printf(BORDER);
    	printf("\n");
    }
    BOOLEAN isVowel(char aChar)
    {
    	switch (toupper(aChar)) //<ctype.h>
    	{
    	case 'A':
    	case 'E':
    	case 'I':
    	case 'O':
    	case 'U': return TRUE; break;
    	default: return FALSE;
    	}
    }
    char *generateRefCode(PayRecord EmployeeData[], int chkNo, int count)
    {
    
    
    	static char tempStr[15 + 10 + 1];
    	char buffer[10 + 1];
    	int i, j = 1;
    
    
    	//This loop is used to clear the string everytime generateRefCode is called
    	for (i = 0; i < 31; i++)
    	{
    		tempStr[i] = 0;
    	}
    	//tempStr[0] = lastname[0]; // Needs to put the first letter into the temporary string.
    	tempStr[0] = toupper(EmployeeData[count].LastName[0]);
    	// This should be the first address that holds the first letter of the last name.
    	// LOOP
    	// Need to get through all the characters in the lastname.
    	// Check if the letter is a vowl. If it is, add to the temporary string, else omit it.
    	// ENDLOOP
    	for (i = 1; i < strlen(EmployeeData[count].LastName); i++)
    	{
    		if (!isVowel(EmployeeData[count].LastName[i]))
    		{
    			tempStr[j++] = toupper(EmployeeData[count].LastName[i]);
    		}
    	}
    
    
    	sprintf(buffer, "%d", chkNo);
    	//sprintf is used to make int chkNo into a string using a char buffer
    	strcat(tempStr, buffer);
    	tempStr[j + 3] = 0;
    	// add string terminator, adds the the current subscript+3 because we add
    	// 3 numbers to the end of the string so that it adds the string terminator
    	// to the end of the string.
    	return tempStr;
    }
    
    
    int main(void)
    {
    	PayRecord EmployeeData[MAXEMPLOYEES];
    	char FullName[15 + 10 + 1];
    	char CheckName[15 + 10 + 1];
    	char *refCode;
    	int count;
    	int chkNo = 100;
    	FILE *reportFile; // Read as "reportFile is a pointer to a file"
    
    
    	reportFile = fopen("./report.txt", "wt");// open the file for "write-mode" access
    
    
    	if (reportFile == NULL)// Test if file DID NOT open, exit.
    	{
    		printf(" report file open failed ...\n");
    		fflush(stdin);
    		exit(-30); //reqs <stdlib.h>
    	}
    
    
    	PrintHeadings(reportFile);//Call 3.1
    	InitAccumulators(EmployeeData, MAXEMPLOYEES);//call 3.2
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		InputEmployeeData(EmployeeData, count);
    	}
    
    
    	QSortEmployee(EmployeeData, 0, (MAXEMPLOYEES - 1));
    
    
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		strcpy(FullName, EmployeeData[count].LastName);
    		strcat(FullName, ", ");
    		strcat(FullName, EmployeeData[count].FirstName);
    		EmployeeData[count].gross = CalculateGross(EmployeeData, count);// Call 3.4
    		//CalculateHours() does not actually calculate any hours
    		//It seperates the hours into regular hours and overtime hours
    		CalculateHours(EmployeeData, count);// Call 3.4.1
    		CalculateTaxes(EmployeeData, count); // call 3.5
    		EmployeeData[count].net = CalculateNet(EmployeeData, count);//call 3.6
    		AddAccumulators(EmployeeData, count, MAXEMPLOYEES);//call 3.7
    		showReport(reportFile, FullName, EmployeeData, count);//call 3.8
    	}
    
    
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		refCode = generateRefCode(EmployeeData, chkNo, count);
    		strcpy(CheckName, EmployeeData[count].FirstName);
    		strcat(CheckName, " ");
    		strcat(CheckName, EmployeeData[count].LastName);
    		printCheckHeader(EmployeeData, CheckName, chkNo, refCode, count);
    		printCheckStub(EmployeeData, CheckName, chkNo, count);
    		chkNo++;
    	}
    
    
    	printTotals(reportFile, EmployeeData, MAXEMPLOYEES);//call 3.8.1
    	printAverages(reportFile, MAXEMPLOYEES, EmployeeData);//call 3.8.2
    	fclose(reportFile);// Close file
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    Modified my #define MAXEMPLOYEES from 6 to new value of 5. Changed how my loops worked to still enter values for, calculate, and sort through 5 employees. But I still haven't figured out where the out-of-bounds behavior is after sweeping through my code.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Code:
    int myarray[10];
    If you declare an array as size 10 the last legal location is myarray[9] NOT myarray[10].


    Tim S.
    Please re-read my post; You fixed nothing!

    Code:
    #define SIZE 20
    int myarray[SIZE];
    The value of myarray[SIZE] will always be illegal no matter how large you make SIZE!

    Tim S.
    Last edited by stahta01; 05-17-2014 at 05:07 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    Please re-read my post; You fixed nothing!

    Code:
    #define SIZE 20
    int myarray[SIZE];
    The value of myarray[SIZE] will always be illegal no matter how large you make SIZE!

    Tim S.
    So would this mean I need to use MAXEMPLOYEES-1 everywhere? I already modified it in the following parts of my code but that hasn't fixed my issue yet.

    Code:
    void InitAccumulators(PayRecord EmployeeData[], int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES-1].totreg = 0;
    	EmployeeData[MAXEMPLOYEES-1].totovt = 0;
    	EmployeeData[MAXEMPLOYEES-1].totrate = 0;
    	EmployeeData[MAXEMPLOYEES-1].totgross = 0;
    	EmployeeData[MAXEMPLOYEES-1].totfed = 0;
    	EmployeeData[MAXEMPLOYEES-1].totstate = 0;
    	EmployeeData[MAXEMPLOYEES-1].totssi = 0;
    	EmployeeData[MAXEMPLOYEES-1].totdefr = 0;
    	EmployeeData[MAXEMPLOYEES-1].totnet = 0;
    }
    //-------------
    void AddAccumulators(PayRecord EmployeeData[], int count, int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES-1].totreg = EmployeeData[MAXEMPLOYEES-1].totreg + EmployeeData[count].reghrs;
    	EmployeeData[MAXEMPLOYEES-1].totovt = EmployeeData[MAXEMPLOYEES-1].totovt + EmployeeData[count].ovthrs;
    	EmployeeData[MAXEMPLOYEES-1].totrate = EmployeeData[MAXEMPLOYEES-1].totrate + EmployeeData[count].payrate;
    	EmployeeData[MAXEMPLOYEES-1].totgross = EmployeeData[MAXEMPLOYEES-1].totgross + EmployeeData[count].gross;
    	EmployeeData[MAXEMPLOYEES-1].totfed = EmployeeData[MAXEMPLOYEES-1].totfed + EmployeeData[count].fedtax;
    	EmployeeData[MAXEMPLOYEES-1].totstate = EmployeeData[MAXEMPLOYEES-1].totstate + EmployeeData[count].statetax;
    	EmployeeData[MAXEMPLOYEES-1].totssi = EmployeeData[MAXEMPLOYEES-1].totssi + EmployeeData[count].ssitax;
    	EmployeeData[MAXEMPLOYEES-1].totdefr = EmployeeData[MAXEMPLOYEES-1].totdefr + EmployeeData[count].deferred;
    	EmployeeData[MAXEMPLOYEES-1].totnet = EmployeeData[MAXEMPLOYEES-1].totnet + EmployeeData[count].net;
    }
    //--------------
    void printTotals(FILE *reportFile, PayRecord EmployeeData[], int maxemployees)
    {
    	printf(BREAK);
    	printf(TOTALS1, EmployeeData[MAXEMPLOYEES-1].totrate, EmployeeData[MAXEMPLOYEES-1].totreg, EmployeeData[MAXEMPLOYEES-1].totgross, EmployeeData[MAXEMPLOYEES-1].totfed, EmployeeData[MAXEMPLOYEES-1].totssi, EmployeeData[MAXEMPLOYEES-1].totnet);
    	printf(TOTALS2, EmployeeData[MAXEMPLOYEES-1].totovt, EmployeeData[MAXEMPLOYEES-1].totstate, EmployeeData[MAXEMPLOYEES-1].totdefr);
    	fprintf(reportFile, BREAK);
    	fprintf(reportFile, TOTALS1, EmployeeData[MAXEMPLOYEES-1].totrate, EmployeeData[MAXEMPLOYEES-1].totreg, EmployeeData[MAXEMPLOYEES-1].totgross, EmployeeData[MAXEMPLOYEES-1].totfed, EmployeeData[MAXEMPLOYEES-1].totssi, EmployeeData[MAXEMPLOYEES-1].totnet);
    	fprintf(reportFile, TOTALS2, EmployeeData[MAXEMPLOYEES-1].totovt, EmployeeData[MAXEMPLOYEES-1].totstate, EmployeeData[MAXEMPLOYEES-1].totdefr);
    }
    void printAverages(FILE *reportFile, int maxemployees, PayRecord EmployeeData[])
    {
    	printf(AVERAGES1, (EmployeeData[MAXEMPLOYEES-1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totnet / MAXEMPLOYEES));
    	printf(AVERAGES2, (EmployeeData[MAXEMPLOYEES-1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totdefr / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES1, (EmployeeData[MAXEMPLOYEES-1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totnet / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES2, (EmployeeData[MAXEMPLOYEES-1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totdefr / MAXEMPLOYEES));
    }

  6. #6
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Also changed these parts of the code now; still occurs the error before it deals with Totals and Averages.

    Code:
    int main(void)
    {
    	PayRecord EmployeeData[MAXEMPLOYEES-1];
    //......
    PrintHeadings(reportFile);//Call 3.1
    	InitAccumulators(EmployeeData, (MAXEMPLOYEES-1));//call 3.2
    //......
    for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		strcpy(FullName, EmployeeData[count].LastName);
    		strcat(FullName, ", ");
    		strcat(FullName, EmployeeData[count].FirstName);
    		EmployeeData[count].gross = CalculateGross(EmployeeData, count);// Call 3.4
    		//CalculateHours() does not actually calculate any hours
    		//It seperates the hours into regular hours and overtime hours
    		CalculateHours(EmployeeData, count);// Call 3.4.1
    		CalculateTaxes(EmployeeData, count); // call 3.5
    		EmployeeData[count].net = CalculateNet(EmployeeData, count);//call 3.6
    		AddAccumulators(EmployeeData, count, (MAXEMPLOYEES-1));//call 3.7
    //.......
    printTotals(reportFile, EmployeeData, (MAXEMPLOYEES-1));//call 3.8.1
    	printAverages(reportFile, (MAXEMPLOYEES-1), EmployeeData);//call 3.8.2
    	fclose(reportFile);// Close file

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you understand C array start at zero!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

    I can NOT help you; you are beyond my ability to help.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    Do you understand C array start at zero!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !

    I can NOT help you; you are beyond my ability to help.

    Tim S.
    Well I tried, sorry.

  9. #9
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Found the run-time error with my code:
    Code:
    char *generateRefCode(PayRecord EmployeeData[], int chkNo, int count){
    	static char tempStr[15 + 10 + 1];
    	char buffer[10 + 1];
    	int i, j = 1;
    
    
    	//This loop is used to clear the string everytime generateRefCode is called
    	for (i = 0; i < 31; i++)
    	{
    		tempStr[i] = 0;
    	}
    This little guy was the whole issue with why my program was terminating, now that this is fixed...
    I got to see my whole thing in action, and everything looks good...

    Aside the totals/averages. (Not the format, the numbers)
    Check it: Program fails to show totals/averages-h7zvck3-pngProgram fails to show totals/averages-kqnknk1-jpgProgram fails to show totals/averages-cozq02p-jpgProgram fails to show totals/averages-u4ywsrh-png

    Alright and now my current code is this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    typedef short BOOLEAN;
    typedef char STR15[15 + 1];
    typedef char STR10[10 + 1];
    typedef struct PayRecord
    {
    	STR15 LastName;
    	STR10 FirstName;
    	float hours, payrate, deferred, gross, fedtax, statetax, ssitax, net;
    	float reghrs, ovthrs;
    	float totreg, totovt, totrate, totgross, totfed, totstate, totssi, totdefr, totnet;
    } PayRecord;
    
    
    #define FEDTAXRATE 0.15
    #define STATETAXRATE 0.07
    #define SSITAXRATE 0.0775
    #define MAXEMPLOYEES 5
    #define TRUE 1
    #define FALSE 0
    
    
    #define HEADLINE1       "Employee         Pay    Reg Hrs  Gross    Fed    SSI    Net\n"
    #define HEADLINE2       "Name             Rate   Ovt Hrs  Pay     State   Defr   Pay\n"
    #define HEADLINE3       "=========        ====== ======= ======= ======= ====== =======\n"
    #define EMPLOYEEREPORT  "%-16s %5.2f    %5.2f %7.2f  %6.2f %6.2f %7.2f\n"
    #define EMPLOYEEREPORT2 "                          %5.2f          %6.2f %6.2f\n\n"
    #define BREAK           "==============================================================\n"
    #define TOTALS1         "Totals           %5.2f   %6.2f %6.2f  %6.2f %6.2f %7.2f\n"
    #define TOTALS2         "                         %6.2f           %5.2f %6.2f\n\n"
    #define AVERAGES1       "Averages         %5.2f   %6.2f  %5.2f  %6.2f %6.2f %7.2f\n"
    #define AVERAGES2       "                         %6.2f           %5.2f %6.2f\n\n"
    
    
    #define BLANK	"*							   *\n"
    #define BORDER  "************************************************************\n"
    #define HEADER  "*				Check No %3d		   *\n"
    #define HEADER2 "*   Sabre Corporation					   *\n"
    #define HEADER3 "*   15790 West Henness lane				   *\n"
    #define HEADER4 "*   New Corio, New Mexico 65790				   *\n"
    #define HEADER5 "*   Pay to the Order of					   *\n"
    #define HEADER6 "*   %-16s					   *\n"
    #define HEADER7 "*   The Sum Nine Hundred Ninety-Nine and XX/100 Dollars    *\n"
    #define HEADER8 "*   Reference: %-16s				   *\n"
    
    
    #define STUB 	"*   Check No %3d					   *\n"
    #define STUB2	"*   %-16s	   Reg Pay		Fed Tax	   *\n"
    #define STUB3	"*   Address		   Ovt Pay		State Tax  *\n"
    #define STUB4	"*   City, State		   Gross Pay		SSI Tax    *\n"
    #define STUB5	"*   Zip			   Net Pay		Tax Total  *\n"
    
    
    void PrintHeadings(FILE *reportFile)
    {
    	fprintf(reportFile, HEADLINE1);
    	fprintf(reportFile, HEADLINE2);
    	fprintf(reportFile, HEADLINE3);
    }
    void InitAccumulators(PayRecord EmployeeData[], int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES-1].totreg = 0;
    	EmployeeData[MAXEMPLOYEES-1].totovt = 0;
    	EmployeeData[MAXEMPLOYEES-1].totrate = 0;
    	EmployeeData[MAXEMPLOYEES-1].totgross = 0;
    	EmployeeData[MAXEMPLOYEES-1].totfed = 0;
    	EmployeeData[MAXEMPLOYEES-1].totstate = 0;
    	EmployeeData[MAXEMPLOYEES-1].totssi = 0;
    	EmployeeData[MAXEMPLOYEES-1].totdefr = 0;
    	EmployeeData[MAXEMPLOYEES-1].totnet = 0;
    }
    void InputEmployeeData(PayRecord EmployeeData[], int count)// 3.3
    {
    	printf("Please enter your first and last name: ");
    	scanf("%s%s", EmployeeData[count].FirstName, EmployeeData[count].LastName);
    
    
    	printf("Please enter hours, payrate, and deferred amount: ");
    	scanf("%f%f%f", &EmployeeData[count].payrate, &EmployeeData[count].hours, &EmployeeData[count].deferred);
    	printf("\n");
    }
    void QSortEmployee(PayRecord EmployeeData[], int start, int finish)
    {
    	int left = start,
    		right = finish;
    	char * pivot = EmployeeData[(start + finish) / 2].LastName;
    
    
    	while (left < right)
    	{
    		while ((strcmp(EmployeeData[left].LastName, pivot) < 0) && (left < right))
    		{
    			left++;
    		}
    		while ((strcmp(EmployeeData[right].LastName, pivot) > 0) && (right > left))
    		{
    			right--;
    		}
    		if (left <= right)
    		{
    			PayRecord tempEmployee = EmployeeData[left];
    			EmployeeData[left] = EmployeeData[right];
    			EmployeeData[right] = tempEmployee;
    			left++;
    			right--;
    		}
    	}
    	if (start < finish)
    	{
    		QSortEmployee(EmployeeData, start, right);
    	}
    	if (left < finish)
    	{
    		QSortEmployee(EmployeeData, left, finish);
    	}
    }
    float CalculateGross(PayRecord EmployeeData[], int count) // 3.4
    {
    	return (EmployeeData[count].hours <= 40) ? EmployeeData[count].hours * EmployeeData[count].payrate :
    		EmployeeData[count].payrate * 40 + (EmployeeData[count].hours - 40) * 1.5 * EmployeeData[count].payrate;
    }
    
    
    void CalculateHours(PayRecord EmployeeData[], int count)
    {
    	if (EmployeeData[count].hours <= 40)
    	{
    		EmployeeData[count].reghrs = EmployeeData[count].hours;
    		EmployeeData[count].ovthrs = 0;
    	}
    	else
    	{
    		EmployeeData[count].reghrs = 40;
    		EmployeeData[count].ovthrs = (EmployeeData[count].hours - 40);
    	}
    }
    float CalculateNet(PayRecord EmployeeData[], int count)
    {
    	return EmployeeData[count].gross - (EmployeeData[count].fedtax + EmployeeData[count].statetax + EmployeeData[count].ssitax + EmployeeData[count].deferred);
    }
    void AddAccumulators(PayRecord EmployeeData[], int count, int maxemployees)
    {
    	EmployeeData[MAXEMPLOYEES-1].totreg = EmployeeData[MAXEMPLOYEES-1].totreg + EmployeeData[count].reghrs;
    	EmployeeData[MAXEMPLOYEES-1].totovt = EmployeeData[MAXEMPLOYEES-1].totovt + EmployeeData[count].ovthrs;
    	EmployeeData[MAXEMPLOYEES-1].totrate = EmployeeData[MAXEMPLOYEES-1].totrate + EmployeeData[count].payrate;
    	EmployeeData[MAXEMPLOYEES-1].totgross = EmployeeData[MAXEMPLOYEES-1].totgross + EmployeeData[count].gross;
    	EmployeeData[MAXEMPLOYEES-1].totfed = EmployeeData[MAXEMPLOYEES-1].totfed + EmployeeData[count].fedtax;
    	EmployeeData[MAXEMPLOYEES-1].totstate = EmployeeData[MAXEMPLOYEES-1].totstate + EmployeeData[count].statetax;
    	EmployeeData[MAXEMPLOYEES-1].totssi = EmployeeData[MAXEMPLOYEES-1].totssi + EmployeeData[count].ssitax;
    	EmployeeData[MAXEMPLOYEES-1].totdefr = EmployeeData[MAXEMPLOYEES-1].totdefr + EmployeeData[count].deferred;
    	EmployeeData[MAXEMPLOYEES-1].totnet = EmployeeData[MAXEMPLOYEES-1].totnet + EmployeeData[count].net;
    }
    
    
    void showReport(FILE *reportFile, char *FullName, PayRecord EmployeeData[], int count) //3.8
    {
    	printf(HEADLINE1);
    	printf(HEADLINE2);
    	printf(HEADLINE3);
    	printf(EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
    	printf(EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
    	fprintf(reportFile, EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
    	fprintf(reportFile, EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
    }
    void printTotals(FILE *reportFile, PayRecord EmployeeData[], int maxemployees)
    {
    	printf(BREAK);
    	printf(TOTALS1, EmployeeData[MAXEMPLOYEES-1].totrate, EmployeeData[MAXEMPLOYEES-1].totreg, EmployeeData[MAXEMPLOYEES-1].totgross, EmployeeData[MAXEMPLOYEES-1].totfed, EmployeeData[MAXEMPLOYEES-1].totssi, EmployeeData[MAXEMPLOYEES-1].totnet);
    	printf(TOTALS2, EmployeeData[MAXEMPLOYEES-1].totovt, EmployeeData[MAXEMPLOYEES-1].totstate, EmployeeData[MAXEMPLOYEES-1].totdefr);
    	fprintf(reportFile, BREAK);
    	fprintf(reportFile, TOTALS1, EmployeeData[MAXEMPLOYEES-1].totrate, EmployeeData[MAXEMPLOYEES-1].totreg, EmployeeData[MAXEMPLOYEES-1].totgross, EmployeeData[MAXEMPLOYEES-1].totfed, EmployeeData[MAXEMPLOYEES-1].totssi, EmployeeData[MAXEMPLOYEES-1].totnet);
    	fprintf(reportFile, TOTALS2, EmployeeData[MAXEMPLOYEES-1].totovt, EmployeeData[MAXEMPLOYEES-1].totstate, EmployeeData[MAXEMPLOYEES-1].totdefr);
    }
    void printAverages(FILE *reportFile, int maxemployees, PayRecord EmployeeData[])
    {
    	printf(AVERAGES1, (EmployeeData[MAXEMPLOYEES-1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totnet / MAXEMPLOYEES));
    	printf(AVERAGES2, (EmployeeData[MAXEMPLOYEES-1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totdefr / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES1, (EmployeeData[MAXEMPLOYEES-1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totnet / MAXEMPLOYEES));
    	fprintf(reportFile, AVERAGES2, (EmployeeData[MAXEMPLOYEES-1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES-1].totdefr / MAXEMPLOYEES));
    }
    float CalcFedtax(PayRecord EmployeeData[], int count) // 3.5.1
    {
    	return (EmployeeData[count].gross - EmployeeData[count].deferred)*FEDTAXRATE;
    }
    float CalcStatetax(PayRecord EmployeeData[], int count) // 3.5.2
    {
    	return EmployeeData[count].fedtax * STATETAXRATE;
    }
    float CalcSSItax(PayRecord EmployeeData[], int count) // 3.5.3
    {
    	return (EmployeeData[count].gross - EmployeeData[count].deferred)........ITAXRATE;
    }
    void CalculateTaxes(PayRecord EmployeeData[], int count) //3.5
    {
    	EmployeeData[count].fedtax = CalcFedtax(EmployeeData, count); // call 3.5.1
    	EmployeeData[count].statetax = CalcStatetax(EmployeeData, count); // call 3.5.2
    	EmployeeData[count].ssitax = CalcSSItax(EmployeeData, count); // call 3.5.3
    }
    //====================
    
    
    void printCheckHeader(PayRecord EmployeeData[], char *CheckName, int chkNo, char *refCode, int count)
    {
    	printf(BORDER);
    	printf(HEADER, chkNo);
    	printf(BLANK);
    	printf(HEADER2);
    	printf(HEADER3);
    	printf(HEADER4);
    	printf(BLANK);
    	printf(BLANK);
    	printf(HEADER5);
    	printf(BLANK);
    	printf(HEADER6, CheckName);
    	printf(BLANK);
    	printf(HEADER7);
    	printf(BLANK);
    	printf(BLANK);
    	printf(HEADER8, refCode);
    	printf(BLANK);
    	printf(BLANK);
    	printf(BORDER);
    }
    void printCheckStub(PayRecord EmployeeData[], char *CheckName, int chkNo, int count)
    {
    	printf(STUB, chkNo);
    	printf(BLANK);
    	printf(STUB2, CheckName);
    	printf(STUB3);
    	printf(STUB4);
    	printf(STUB5);
    	printf(BLANK);
    	printf(BLANK);
    	printf(BORDER);
    	printf("\n");
    }
    BOOLEAN isVowel(char aChar)
    {
    	switch (toupper(aChar)) //<ctype.h>
    	{
    	case 'A':
    	case 'E':
    	case 'I':
    	case 'O':
    	case 'U': return TRUE; break;
    	default: return FALSE;
    	}
    }
    char *generateRefCode(PayRecord EmployeeData[], int chkNo, int count)
    {
    
    
    	static char tempStr[15 + 10 + 1];
    	char buffer[10 + 1];
    	int i, j = 1;
    
    
    	//This loop is used to clear the string everytime generateRefCode is called
    	for (i = 0; i < 26; i++)
    	{
    		tempStr[i] = 0;
    	}
    	//tempStr[0] = lastname[0]; // Needs to put the first letter into the temporary string.
    	tempStr[0] = toupper(EmployeeData[count].LastName[0]);
    	// This should be the first address that holds the first letter of the last name.
    	// LOOP
    	// Need to get through all the characters in the lastname.
    	// Check if the letter is a vowl. If it is, add to the temporary string, else omit it.
    	// ENDLOOP
    	for (i = 1; i < strlen(EmployeeData[count].LastName); i++)
    	{
    		if (!isVowel(EmployeeData[count].LastName[i]))
    		{
    			tempStr[j++] = toupper(EmployeeData[count].LastName[i]);
    		}
    	}
    
    
    	sprintf(buffer, "%d", chkNo);
    	//sprintf is used to make int chkNo into a string using a char buffer
    	strcat(tempStr, buffer);
    	tempStr[j + 3] = 0;
    	// add string terminator, adds the the current subscript+3 because we add
    	// 3 numbers to the end of the string so that it adds the string terminator
    	// to the end of the string.
    	return tempStr;
    }
    
    
    int main(void)
    {
    	PayRecord EmployeeData[MAXEMPLOYEES];
    	char FullName[15 + 10 + 1];
    	char CheckName[15 + 10 + 1];
    	char *refCode;
    	int count;
    	int chkNo = 100;
    	FILE *reportFile; // Read as "reportFile is a pointer to a file"
    
    
    	reportFile = fopen("./report.txt", "wt");// open the file for "write-mode" access
    
    
    	if (reportFile == NULL)// Test if file DID NOT open, exit.
    	{
    		printf(" report file open failed ...\n");
    		fflush(stdin);
    		exit(-30); //reqs <stdlib.h>
    	}
    
    
    	PrintHeadings(reportFile);//Call 3.1
    	InitAccumulators(EmployeeData, (MAXEMPLOYEES-1));//call 3.2
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		InputEmployeeData(EmployeeData, count);
    	}
    
    
    	QSortEmployee(EmployeeData, 0, (MAXEMPLOYEES - 1));
    
    
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		strcpy(FullName, EmployeeData[count].LastName);
    		strcat(FullName, ", ");
    		strcat(FullName, EmployeeData[count].FirstName);
    		EmployeeData[count].gross = CalculateGross(EmployeeData, count);// Call 3.4
    		//CalculateHours() does not actually calculate any hours
    		//It seperates the hours into regular hours and overtime hours
    		CalculateHours(EmployeeData, count);// Call 3.4.1
    		CalculateTaxes(EmployeeData, count); // call 3.5
    		EmployeeData[count].net = CalculateNet(EmployeeData, count);//call 3.6
    		AddAccumulators(EmployeeData, count, (MAXEMPLOYEES-1));//call 3.7
    		showReport(reportFile, FullName, EmployeeData, count);//call 3.8
    	}
    
    
    	for (count = 0; count < MAXEMPLOYEES; count++)
    	{
    		refCode = generateRefCode(EmployeeData, chkNo, count);
    		strcpy(CheckName, EmployeeData[count].FirstName);
    		strcat(CheckName, " ");
    		strcat(CheckName, EmployeeData[count].LastName);
    		printCheckHeader(EmployeeData, CheckName, chkNo, refCode, count);
    		printCheckStub(EmployeeData, CheckName, chkNo, count);
    		chkNo++;
    	}
    
    
    	printTotals(reportFile, EmployeeData, (MAXEMPLOYEES-1));//call 3.8.1
    	printAverages(reportFile, (MAXEMPLOYEES-1), EmployeeData);//call 3.8.2
    	fclose(reportFile);// Close file
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    Can someone help me fix the issue with my totals and averages being displayed as crazy numbers?

  10. #10
    Registered User Ferii's Avatar
    Join Date
    May 2014
    Posts
    8
    Alright issue is fixed, got the correct totals and averages now. Close this thread please!

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Alright issue is fixed, got the correct totals and averages now. Close this thread please!
    O_o

    The thread will not be closed; you can just stop responding.

    Also, it is considered a good thing to post solutions to your own problems.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program wont add up totals
    By Boolean0605 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2014, 02:51 PM
  2. My program is not computing averages from an array. Simple C
    By Mike Garber in forum C Programming
    Replies: 10
    Last Post: 09-23-2013, 10:43 PM
  3. simple MPI program fails
    By dayalsoap in forum C++ Programming
    Replies: 10
    Last Post: 08-05-2011, 09:03 AM
  4. Grand Totals??
    By leopardforest@g in forum C Programming
    Replies: 1
    Last Post: 04-28-2007, 07:12 PM
  5. Program that averages specified number of tests
    By Fatal in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 03:18 PM

Tags for this Thread