[英]Losing a variable value after exiting a loop in C
我正在從事一項任務,該任務應該從文本文件中讀取員工信息並計算諸如總工資和應付稅款之類的東西。 但是我無法進行計算位 w 因為我的變量“小時”之一在我退出從文件讀取的循環后發生了變化。
我不確定為什么我可以在內部打印正確的值,但不能在循環之外打印。
我正在閱讀以下文件中的姓氏、名字、工資率和工作時間:
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
這是我到目前為止的進展:
#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;
}
}
我對編程很陌生,感覺它是一個簡單的修復,但我無法弄清楚什么是錯的。 任何幫助將不勝感激。
謝謝。
據我所知,您的程序(此處和 repl.it 上的程序)似乎完全按照您的程序進行。 當我閱讀您的數據描述時,它顯示“姓氏、名字、工資率和小時數” 但是,當您在程序中閱讀數據時,您正在閱讀:“emp[i].lastName, emp[i].firstName, &emp[ i]. hours , &emp[i]. payrate ". 您已反轉閱讀中的工資率和小時數字段。 這是一個簡單的錯誤,很容易犯。 我的經驗是最簡單的問題往往最難發現。 我確實花了很多時間對類似的錯誤感到困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.