简体   繁体   中英

Losing a variable value after exiting a loop in C

I'm working on an assignment that is supposed to read employee information from a text file and calculate things such as gross pay and amount due in tax. But I cant do the calculation bit w because one of my variables "hours" changes after I exit the loop that reads from the file.

I'm not sure why I can print the correct values inside, but not outside the loop.

I'm reading Lastname, Firstname, Payrate and Hours worked from the file below:

Whittle Ed 11.50 25.50
Davidson Carl 8.75 38.00
Doe John 17.00 46.50
Marion Louise 13.00 40.00
Prentiss Paula 15.75 50.50

Here's my progress so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXEMPLOYEES 5
#define TAXRATE .15


/************************************************************

    Program Decomposition
    1.0
        1.1 InputData(in lastName, firstName as string, in hours, payrate, as real)
        1.2 CalculatePay()
            1.2.1 CalculateOvertime(in hours, out regularHours, out otHours)
            1.2.2 CalculateGross()
            1.2.3 CalculateTax()
            1.2.4 CalculateNet()
*************************************************************/


/************************************************************

    Function Prototypes
************************************************************/

void InputData(FILE * reportFile, char * lastName, char * firstName, float *hours, float *payrate);
void CalculateOvertime(float hours, float * regularHours, float * otHours);




/************************************************************
 Record of employee information
 ***********************************************************/
typedef struct empRecord{

    char fullName[20];
    char lastName[8+1];
    char firstName[8+1];
    float tax, net;
    float payrate, hours, regularHours, otHours, gross;
}empRecord;

int main(void){

    FILE * reportFile;
    empRecord emp[MAXEMPLOYEES];

    int employeeCount = 0;

    reportFile = fopen("report.txt","r");
        if (reportFile == NULL)
            {
            printf("File failed to open!...\n");
            printf("Press a key to exit...\n");
            while (getchar() != '\n');
            exit (86);
            }
/*********************************************************
    Input Loop
*********************************************************/  
    for(int i = 0; i < MAXEMPLOYEES; i++){

        InputData(reportFile, emp[i].lastName, emp[i].firstName, &emp[i].hours, &emp[i].payrate);
        // Checking for correct inputs
        printf("%s, %s, %.2f, %.2f\n",emp[i].lastName, emp[i].firstName, emp[i].hours, emp[i].payrate);

        employeeCount++;
    }

    // Checking for input 
    for(int i = 0; i < MAXEMPLOYEES; i++){
    printf("%f\n",emp[i].hours);
    }
/**********************************************************
    Processing Loop
**********************************************************/
    for(int i = 0; i < MAXEMPLOYEES; i++){


        CalculateOvertime(emp[i].hours, &emp[i].regularHours, &emp[i].otHours);

    }

    return 0;
}

/********************************************

    1.1 InputData(in lastName, firstName as string, in hours, payrate, as real)
        This function reads inputs from the file and inputs that data into the Array of Records.

**********************************************/

void InputData(FILE * reportFile, char * lastName, char * firstName, float *hours, float *payrate){

    printf(" Reading  new employee data...\n");

    fscanf(reportFile, "%s", lastName);

    fscanf(reportFile, "%s", firstName);

    fscanf(reportFile, "%f", hours);

    fscanf(reportFile, "%f", payrate);


}

void CalculateOvertime(float hours, float * regularHours, float * otHours){


printf(" Hours are : ", "%f", hours);


    if (hours > 40)
    {

        *regularHours = 40;
        *otHours = (hours - 40);
    }
    else
    {

        *regularHours = hours;
        *otHours = 0;
    }

}

I'm pretty new to programming and feel like its a simple fix, but I haven't been able to figure out whats wrong. Any help would be greatly appreciated.

Thanks.

From what I can tell it looks like your program (both here and on repl.it) are doing exactly what you have programmed. When I read the description of your data it says "Lastname, Firstname, Payrate and Hours " However, when you read the data in your program you are reading: "emp[i].lastName, emp[i].firstName, &emp[i]. hours , &emp[i]. payrate ". You have reversed the payrate and hours fields in your read. It's a simply mistake and easy to make. My experience is the easiest problems are often to hardest to spot. I've certainly spend my share of hours puzzled over similar bugs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM