[英]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.