变量或字段声明无效”和其他错误
Posted
技术标签:
【中文标题】变量或字段声明无效”和其他错误【英文标题】:Variable Or Field Declared Void" And Other Errors 【发布时间】:2014-05-08 19:29:31 【问题描述】:我正在创建一个工资单程序,这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n"
#define REPORTHEADING2 " Name Rate Worked Pay Due Pay\n"
#define REPORTHEADING3 " =============== ==== ====== ===== ==== ====\n"
#define REPORTHEADING4 " ==== ====== ===== ==== ====\n"
#define REPORTLINEFORMAT1 " %-20s%6.2f%11.2f%9.2f%9.2f%10.2f\n"
#define REPORTLINEFORMAT2 " Totals %6.2f%11.2f%9.2f%9.2f%10.2f\n"
#define REPORTLINEFORMAT3 " Averages %6.2f%11.2f%9.2f%9.2f%10.2f\n"
#define COUNTLINEFORMAT " Number of employees: %-10i\n\n"
#define MAXREGHOURS 40
#define OVERTIMERATE 1.5
enter code here
void PrintReportHeadings(FILE *reportFile); //printReportHeadings prototype
void InitializeAccumulators(float *totRegHour,float *totOvtHours,float *totPayrate,
float *totGross,float *totdeferred,float *totFedtax,
float *totStatetax,float *totSSItax,float *totNet,int *empCount); //InitializeAccumulators prototype
void InputEmployeeData(char *firstName,char *lastName,
float *hours,float *payrate,float *deferred); //InputEmployeeData prototype
void CalculateGross(float hours,float payrate,float *regHours,float *ovtHours,
float *gross); //CalculateGross prototype
extern void CalculateTaxes(float gross,float deferred,float * fedtax,
float * statetax,float * ssitax); //CalculateTaxes prototype (external)
float CalculateNetPay(float gross,float fedtax,float statetax,float ssitax,
float deferred);
void AddDetailToAccumulators(float regHours,float ovtHours,float payrate,
float gross,float deferred,float fedtax,float statetax,
float ssitax,float net,float *totRegHours,float *totOvtHours,
float *totPayrate,float *totGross,float *totdeferred,
float *totFedtax,float *totStatetax,float *totSSItax,
float *totNet);
void PrintSummaryReport(FILE *reportFile,char fullName,float regHours,float ovtHours,
float payrate,float gross,float deferred,float fedtax,
float statetax,float ssitax,float net);
int main(void)
float ft,st,ssit;
char firstName[10+1];
char lastName[15+1];
char fullName[25+1];
float regHours, ovtHours, hours, payrate, deferred, gross, netpay;
float totRegHours, totOvtHours, totPayrate, totGross,totdeferred,
totFedtax, totStatetax, totSSItax, totNet;
int empcount;
char answer;
FILE * reportFile;
reportFile = fopen("./report.txt","wt");
if(reportFile = NULL)
printf(" Report open request failed...\n");
while(getchar() != '\n');
exit(-90);// reqs <stdlib.h>
PrintReportHeadings(reportFile);
InitializeAccumulators(&totRegHours,&totOvtHours,&totPayrate,&totGross,
&totdeferred,&totFedtax,&totStatetax,&totSSItax,&totNet,
&empcount);//set all accumulators to 0
do
InputEmployeeData(firstName,lastName,&hours,&payrate,&deferred);
CalculateGross(hours, payrate, ®Hours, &ovtHours, &gross);
CalculateTaxes(gross,deferred,&ft,&st,&ssit);
netpay = CalculateNetPay(gross,ft,st,ssit,deferred);
strcpy(fullName,lastName);
strcat(fullName,", ");
strcat(fullName,firstName);
AddDetailToAccumulators(regHours,ovtHours,payrate,gross,deferred,ft,st,
ssit,netpay,&totRegHours,&totOvtHours,&totPayrate,&totGross,
&totdeferred,&totFedtax,&totStatetax,&totSSItax,&totNet);
void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
ft,st,ssit,netpay)
empcount++;
printf(COUNTLINEFORMAT,empcount);
printf(" do you have anymore? (Y/N): ");
while(getchar() != '\n');
answer = getchar();
printf("\n");
while(answer != 'N' && answer != 'n');
while (getchar()!= '\n');
getchar();
return 0;
void PrintReportHeadings(FILE *reportFile)
reportFile = fopen("./report.txt","wt");
fprintf(reportFile,REPORTHEADING1);
fprintf(reportFile,REPORTHEADING2);
fprintf(reportFile,REPORTHEADING3);
void InitializeAccumulators(float *totRegHour,float *totOvtHours,float *totPayrate,
float *totGross,float *totdeferred,float *totFedtax,
float *totStatetax,float *totSSItax,float *totNet,int *empCount)
totRegHour, totOvtHours, totPayrate, totGross,totdeferred,
totFedtax, totStatetax, totSSItax, totNet, empCount = 0;
void InputEmployeeData(char *firstName,char *lastName,float *hours,
float *payrate,float *deferred)
printf(" Enter employee first name : ");
scanf("%s",firstName);
printf(" Enter employee last name : ");
scanf("%s",lastName);
printf(" Enter %s's hours worked : ",firstName);
scanf("%f",hours);
printf(" Enter %s's pay rate : ",firstName);
scanf("%f",payrate);
printf(" Enter %s's amount deferred : ",firstName);
scanf("%f",deferred);
void CalculateGross(float hours,float payrate,float *regHours,float *ovtHours,float *gross)
float overtimeHours(float hours);
if(hours <= MAXREGHOURS)
*regHours = hours;
*gross = hours * payrate;
else
*regHours = MAXREGHOURS;
*ovtHours = overtimeHours(hours);
*gross = payrate * MAXREGHOURS + OVERTIMERATE * payrate * (hours - MAXREGHOURS);
float overtimeHours(float hours)
return hours - MAXREGHOURS;
float CalculateNetPay(float gross,float fedtax,float statetax,float ssitax,
float deferred)
return gross - (fedtax + statetax + ssitax + deferred);
void AddDetailtoAccumulators(float regHours,float ovtHours,float payrate,
float gross,float deferred,float fedtax,float statetax,
float ssitax,float netpay,float *totRegHours,float *totOvtHours,
float *totPayrate,float *totGross,float *totDeferred,
float *totFedtax,float *totStatetax,float *totSSItax,
float *totNet)
*totRegHours =+ regHours;
*totOvtHours =+ ovtHours;
*totPayrate =+ payrate;
*totGross =+ gross;
*totDeferred =+ deferred;
*totFedtax =+ fedtax;
*totStatetax =+ statetax;
*totSSItax =+ ssitax;
*totNet =+ netpay;
void PrintSummaryReport(FILE *reportFile,char fullName,float regHours,float ovtHours,
float payrate,float gross,float deferred,float fedtax,
float statetax,float ssitax,float netpay)
reportFile = fopen("./report.txt","wt");
fprintf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
fprintf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
我得到的错误是: 变量或字段 PrintSummaryReport 声明为 void
然后我还得到: 在函数 voidPrintSummaryReport... 无法将 FILE 转换为 const car 以作为 int printf(const char*...) 的参数 这是以下几行
printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
【问题讨论】:
你的函数调用不正确 【参考方案1】:错误:无关的“void”;缺少“;”:
void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
ft,st,ssit,netpay)
正确:
PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
ft,st,ssit,netpay);
问:为什么在 main() 中打开“reportFile”...然后在“PrintReportHeading()”中再次打开它?又在“PrintSummaryReport()”中?只需打开一次,然后在需要的地方使用。
最后:
错误:“printf()”没有使用 FILE* 参数; “fprintf()” 会:
printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
正确:
fprintf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
fprintf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);
【讨论】:
非常感谢!我的大部分错误都已修复,但现在我只剩下一个。 puu.sh/7NHU5.png(只是第一个错误,感谢您的帮助,第二个错误已修复,至少我没有收到错误!尚未能够编译以查看其他所有内容是否正常) 1) “在此处输入代码”应在注释中。例如:/* enter code here */
2) 您不得使用“reportFile”(一个 FILE* 指针)调用 printf()。将它们更改为“fprintf()”,或删除“reportFile”参数。【参考方案2】:
在do
块中,您有以下语句:
void PrintSummaryReport(reportFile,fullName,regHours,ovtHours,payrate,gross,deferred,
ft,st,ssit,netpay)
删除void
关键字,因为您在那里调用该函数。
然后,关于printf
中的错误,我认为您不能将printf
与FILE*
一起使用,如错误所述。 fprintf
应该没问题。您可以查看printf
等here 的详细信息。请注意,这适用于 Linux,但也适用于在 Windows 上使用 C。
【讨论】:
以上是关于变量或字段声明无效”和其他错误的主要内容,如果未能解决你的问题,请参考以下文章
变量或字段 'nameOfVariable' 声明为 void 不知道为啥 :(
C ++接收纯虚函数错误,“变量或字段'x'声明为void”[关闭]