[英]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.
但是我无法进行计算位 w 因为我的变量“小时”之一在我退出从文件读取的循环后发生了变化。
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.据我所知,您的程序(此处和 repl.it 上的程序)似乎完全按照您的程序进行。 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 ".
当我阅读您的数据描述时,它显示“姓氏、名字、工资率和小时数” 但是,当您在程序中阅读数据时,您正在阅读:“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.
我确实花了很多时间对类似的错误感到困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.