C语言时间合法性校验代码编译报错:expected expression before 'int'求助
C语言时间校验代码编译错误修复
原代码
#include <stdio.h> #include "simpio.h" #include "genlib.h" #include <stdbool.h> bool Valid_Time(int h,int min,int sec); int main() { int h,min,sec; printf("Dwse tis ores: "); h=GetInteger(); printf("Dwse ta lepta: "); min=GetInteger(); printf("Dwse ta defterolepta: "); sec=GetInteger(); if ( Valid_Time (int h,int min,int sec) == true) { printf("Valid: yes"); } else { printf("Valid: no"); } return 0; } bool Valid_Time(int h,int min,int sec) { bool valid; valid=true; if(h<0 || h>23) { valid=false; } if(min<0 || min>59) { valid=false; } if(sec<0 || sec>59) { valid=false; } return valid; }
编译错误信息
error:expected expression before 'int'
error:too few arguments to function 'Valid_Time'
错误原因与修复方案
这两个错误根源是调用Valid_Time函数时的语法错误:
- 函数调用阶段不需要给参数添加类型声明,你错误地在
h、min、sec前加了int,导致编译器无法识别合法表达式,进而误判参数数量不足。
只需修改main函数里的函数调用语句,去掉参数前的int即可:
将Valid_Time (int h,int min,int sec)改为Valid_Time(h, min, sec)
修复后的完整代码
#include <stdio.h> #include "simpio.h" #include "genlib.h" #include <stdbool.h> bool Valid_Time(int h,int min,int sec); int main() { int h,min,sec; printf("Dwse tis ores: "); h=GetInteger(); printf("Dwse ta lepta: "); min=GetInteger(); printf("Dwse ta defterolepta: "); sec=GetInteger(); if (Valid_Time(h, min, sec) == true) { printf("Valid: yes"); } else { printf("Valid: no"); } return 0; } bool Valid_Time(int h,int min,int sec) { bool valid; valid=true; if(h<0 || h>23) { valid=false; } if(min<0 || min>59) { valid=false; } if(sec<0 || sec>59) { valid=false; } return valid; }
内容的提问来源于stack exchange,提问作者JELO
相关产品推荐
相关产品推荐

