我寫了一個工作得很好的代碼,但是,一旦我將代碼外化並將代碼轉移到像Puttty或MAC系統控制檯這樣的UNIX環境中,我就會發現異常。我有一個for循環執行一個額外的時間,這很奇怪。 Here is a picture of the report爲什麼我的for循環在UNIX環境中執行額外的時間?
對不起,我不太確定如何在此處輸入我的代碼,因爲我在預覽時看到#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
那麼我的問題是如何解決這個問題?我一直堅持這一兩天...
你真的要使用'<='和'不<'? – Siguza
至少在Linux下,這個'fflush(stdin)'(沖洗標準輸入)會引發未定義的行爲。 – alk
使用-O2 -W -Wall或者你丟失了大多數的警告g ++可以針對錯誤代碼發出警告。很多時候未定義的行爲顯示爲警告。 –