我编写了一个非常好的代码,但是,一旦我将代码外部化并将代码传输到像Puttty或MAC系统控制台这样的UNIX环境,我就会出现异常现象。我有一个for循环执行一个额外的时间,它很奇怪。 Here is a picture of the report
抱歉,我不太确定如何在此输入我的代码,因为当我查看预览时,我的#define变得过大。
This is a link to all the source files if it is necessary to view all of them
maxEmployee.h头文件仅包含#define MAXEMP 40
这是主要功能:
#include <stdio.h>
#include "maxEmployee.h"
extern void InitializeAccumulators(float* totDefr, float *totPayRate, float *totRegHrs, float *totOvtHrs, float *totGross, float *totFed, float *totSt, float *totSSI, float *totNet); //3.1 Prototype
extern void InputEmployeeData(char ln[][10+1], char fn[][10+1], float hr[], float pRate[], float defr[], int i); //3.2 Prototype
extern void CalculateGross(float hr[], float pRate[], float reghr[], float ovthr[], float gross[], int i); //3.3 Prototype
extern void calculateTaxes(float gross[], float defr[], float ft[], float st[], float ssit[], int i); //3.4 Prototype
extern void CalculateNetPay(float gross[], float defr[], float ft[], float st[], float ssit[], float netPay[], int i); //3.5 Prototype
extern void CalculateTotAvg(float pRate[], float reghr[], float ovthr[], float gross[], float ft[], float st[], float ssit[], float netPay[], float defr[], int i, float *totDefr, float *totPayRate, float *totRegHrs, float *totOvtHrs, float *totGross, float *totFed, float *totSt, float *totSSIT, float *totNet,
float *avgDefr, float *avgPayRate, float *avgRegHrs, float *avgOvtHrs, float *avgGross, float *avgFed, float *avgSt, float *avgSSIT, float *avgNet); //3.6 Prototype
extern void PrintReportSummary(char fn[][10+1], char ln[][10+1], float pRate[], float reghr[], float ovthr[], float gross[], float ft[], float st[], float ssit[], float netPay[], float defr[], int i, float totDefr, float totPayRate, float totRegHrs, float totOvtHrs, float totGross, float totFed, float totSt, float totSSIT, float totNet,
float avgDefr, float avgPayRate, float avgRegHrs, float avgOvtHrs, float avgGross, float avgFed, float avgSt, float avgSSIT, float avgNet); //3.7 Prototype
int main(void)
{ // 3.0
char ln[MAXEMP][10 + 1], fn[MAXEMP][10 + 1]; //last name and first name
float hr[MAXEMP], pRate[MAXEMP], defr[MAXEMP], gross[MAXEMP], reghr[MAXEMP], ovthr[MAXEMP]; // hour, payrate, deferred, gross, regular hour, over time hours
float ft[MAXEMP], st[MAXEMP], ssit[MAXEMP]; //calculate taxes
float netPay[MAXEMP];
float totDefr, totPayRate, totRegHrs, totOvtHrs, totGross, totFed, totSt, totSSIT, totNet;
float avgDefr, avgPayRate, avgRegHrs, avgOvtHrs, avgGross, avgFed, avgSt, avgSSIT, avgNet;
int i, yesno = 1;
InitializeAccumulators(&totDefr, &totPayRate, &totRegHrs, &totOvtHrs, &totGross,
&totFed, &totSt, &totSSIT, &totNet); //call 3.1
for (i = 0; i < MAXEMP && yesno == 1; i++)
{
InputEmployeeData(ln, fn, hr, pRate, defr, i); //call 3.2
CalculateGross(hr, pRate, reghr, ovthr, gross, i); //call 3.3
calculateTaxes(gross, defr, ft, st, ssit, i); //call 3.4
CalculateNetPay(gross, defr, ft, st, ssit, netPay, i); //call 3.5
printf("Do you have another employee you wish to enter? (1 = Yes, 0 = No) ");
scanf("%d", &yesno);
}
CalculateTotAvg(pRate, reghr, ovthr, gross, ft, st, ssit, netPay, defr, i, &totDefr, &totPayRate, &totRegHrs, &totOvtHrs, &totGross, &totFed, &totSt, &totSSIT, &totNet, &avgDefr, &avgPayRate, &avgRegHrs, &avgOvtHrs, &avgGross, &avgFed, &avgSt, &avgSSIT, &avgNet); //call 3.6
PrintReportSummary(fn, ln, pRate, reghr, ovthr, gross, ft, st, ssit, netPay, defr, i, totDefr, totPayRate, totRegHrs, totOvtHrs, totGross, totFed, totSt, totSSIT, totNet, //call 3.7
avgDefr, avgPayRate, avgRegHrs, avgOvtHrs, avgGross, avgFed, avgSt, avgSSIT, avgNet);
fflush(stdin), getchar(); // hold screen
return 0;
}
这是PrintReportSummary子功能:
> #include <stdio.h>
> #include <string.h>
> #include "maxEmployee.h"
>
> #define REPORTHEADERFORMAT "\n%-20s%16.2f%18.2f%18.2f%18.2f%18.2f%18.2f\n%54.2f%36.2f%18.2f\n"
> #define REPORTHEADERTOT "\nTotals%30.2f%18.2f%18.2f%18.2f%18.2f%18.2f\n%54.2f%36.2f%18.2f\n"
> #define REPORTHEADERAVG "\nAverages%28.2f%18.2f%18.2f%18.2f%18.2f%18.2f\n%54.2f%36.2f%18.2f\n"
>
> void PrintReportSummary(char fn[][10+1], char ln[][10+1], float
> pRate[], float reghr[], float ovthr[], float gross[], float ft[],
> float st[], float ssit[], float netPay[], float defr[], int i, float
> totDefr, float totPayRate, float totRegHrs, float totOvtHrs, float
> totGross, float totFed, float totSt, float totSSIT, float totNet,
> float avgDefr, float avgPayRate, float avgRegHrs, float avgOvtHrs,
> float avgGross, float avgFed, float avgSt, float avgSSIT, float
> avgNet); //3.7 Prototype
>
> void PrintReportSummary(char fn[][10+1], char ln[][10+1], float
> pRate[], float reghr[], float ovthr[], float gross[], float ft[],
> float st[], float ssit[], float netPay[], float defr[], int i, float
> totDefr, float totPayRate, float totRegHrs, float totOvtHrs, float
> totGross, float totFed, float totSt, float totSSIT, float totNet,
> float avgDefr, float avgPayRate, float avgRegHrs, float avgOvtHrs, float avgGross, float avgFed, float avgSt, float avgSSIT, float
> avgNet) //3.7 {
>
> char fullname[MAXEMP][20+1];
>
> printf("Employee Pay Reg Hrs Gross Fed SSI
> Net\n"); printf("Name Rate Ovt Hrs Pay State
> Defr Pay\n");
> printf("==============================================================================================================================");
>
> for (int k = 0; k <= i; k++) { strcpy (fullname[k], ln[k]);
> strcat(fullname[k], ", "); strcat(fullname[k], fn[k]);
>
> printf(REPORTHEADERFORMAT, fullname[k], pRate[k], reghr[k], gross[k],
> ft[k], ssit[k], netPay[k], ovthr[k], st[k], defr[k]); }
> printf(REPORTHEADERTOT, totPayRate, totRegHrs, totGross, totFed,
> totSSIT, totNet, totOvtHrs, totSt, totDefr); printf(REPORTHEADERAVG,
> avgPayRate, avgRegHrs, avgGross, avgFed, avgSSIT, avgNet, avgOvtHrs,
> avgSt, avgDefr); }
这是我的make文件:
dataReport.exe: main.o InitializeAccumulators.o InputEmployeeData.o CalculateGross.o calculateTaxes.o CalculateNetPay.o CalculateTotAvg.o PrintReportSummary.o
g++ main.o InitializeAccumulators.o InputEmployeeData.o CalculateGross.o calculateTaxes.o CalculateNetPay.o CalculateTotAvg.o PrintReportSummary.o -o dataReport.exe
main.o: main.cpp maxEmployee.h
g++ -c main.cpp -o main.o
InitializeAccumulators.o: InitializeAccumulators.cpp
g++ -c InitializeAccumulators.cpp -o InitializeAccumulators.o
InputEmployeeData.o: InputEmployeeData.cpp
g++ -c InputEmployeeData.cpp -o InputEmployeeData.o
CalculateGross.o: CalculateGross.cpp
g++ -c CalculateGross.cpp -o CalculateGross.o
calculateTaxes.o: calculateTaxes.cpp taxrates.h
g++ -c calculateTaxes.cpp -o calculateTaxes.o
CalculateNetPay.o: CalculateNetPay.cpp
g++ -c CalculateNetPay.cpp -o CalculateNetPay.o
CalculateTotAvg.o: CalculateTotAvg.cpp
g++ -c CalculateTotAvg.cpp -o CalculateTotAvg.o
PrintReportSummary.o: PrintReportSummary.cpp maxEmployee.h
g++ -c PrintReportSummary.cpp -o PrintReportSummary.o
clean:
rm *.o dataReport.exe
那么我的问题是如何解决这个问题?我已经被困在这几天了......
答案 0 :(得分:0)
在main中你循环1次,所以变量“i”在for循环结束时的值为1。
在PrintReportSummary中,您使用以下循环:
for (int k = 0; k <= i; k++)
代替1给我:
for (int k = 0; k <= 1; k++)
所以循环体执行2次。 Windows中的行为应该是相同的,所以你可能会重新测试:) 此外,如果输入40条记录,或者将MAXEMP设置为1并输入1条记录,则程序将在第二个for循环中崩溃或显示未定义的行为,因为它将从数组中访问超出范围的元素。
解决方案:将&lt; =更改为&lt;。
为了清理代码,您可以使用结构而不是那么多函数参数:)