C语言函数调用参数不足报错求助:pkgA/B/C问题排查
C语言函数调用错误与代码优化指南
问题背景
我正在编写第一个大型C语言程序,调用pkgA()、pkgB()、pkgC()时一直报“too few arguments to function”错误。之前没接触过函数调用,试过用&或*,也修改过函数参数声明(比如int pkgA(int inputHours)或int pkgA(int &inputHours)),都没解决,也不清楚这两个符号的用法。另外需要代码格式的指导。
原代码
#include <stdio.h> #include <ctype.h> int inputHours() { int hours; char hours_verify; do { do { printf("Number of hours from 0 to 720: "); scanf(" %i", &hours); }while(hours<0||hours>720); printf("%i hours, correct?\n", hours); printf("Y/N\n"); scanf(" %c", &hours_verify); }while(toupper((unsigned char)hours_verify) != 'Y'); printf("%i hours of internet were used.\n", hours); return (hours); } int pkgA(int hours) { int pkgprice= 15; //The package price under 50 hours int extra_price= 2; //Price per hour over 50 hours if(hours<=50) //Price under 50 hours { printf("The total price is:$%i", pkgprice); } else if(hours<=720) //Calculate anything over 50 hours { int extra_hours=hours - 50; float total_price=extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f", total_price); } return 0; } int pkgB(int hours) { int pkgprice= 20; //The package price under 100 hours float extra_price= 1.5; //Price per hour over 100 hours if(hours<=100) //Price under 100 hours { printf("The total price is:%i", pkgprice); } else if(100<hours<720) //Calculate anything over 100 hours { int extra_hours=hours - 100; printf("%i\n", extra_hours); float total_price=extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f", total_price); } return 0; } int pkgC(int hours) { int pkgprice= 25; //The package price under 150 hours int extra_price= 1; //Price per hour over 150 hours scanf("%i", &hours); printf("%i\n", hours); if(hours<=150) //Price under 150 hours { printf("The total price is:%i", pkgprice); } else if(150<hours<720) //Calculate anything over 150 hours { int extra_hours=hours - 150; printf("%i\n", extra_hours); float total_price=extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f", total_price); } return 0; } int main(void) { char package_choice; char package_verify; do { printf("Which package would you like to select? A, B, or C "); scanf(" %c", &package_choice); printf("You chose %c, correct?\n", package_choice); printf("Y/N\n"); scanf(" %c", &package_verify); }while(toupper((unsigned char)package_verify) != 'Y'); printf("You chose package %c.\n", package_choice); inputHours; int hours=inputHours(); switch(package_choice) { case 'A': pkgA(); break; case 'B': pkgB(); break; case 'C': pkgC(); break; } }
报错信息
main.c:123:9: error: too few arguments to function ‘pkgA’ 123 | pkgA(); | ^~~~ main.c:35:5: note: declared here 35 | int pkgA(int hours) | ^~~~ main.c:127:9: error: too few arguments to function ‘pkgB’ 127 | pkgB(); | ^~~~ main.c:56:5: note: declared here 56 | int pkgB(int hours) | ^~~~ main.c:131:9: error: too few arguments to function ‘pkgC’ 131 | pkgC(); | ^~~~ main.c:76:5: note: declared here 76 | int pkgC(int hours) | ^~~~
核心错误修复
1. 补充函数调用参数
你声明pkgA(int hours)时明确要求传入一个int类型参数,但调用时写的pkgA()没有传参,这是直接触发报错的原因。修改main函数的switch分支,把获取到的hours变量传递进去:
switch(package_choice) { case 'A': pkgA(hours); break; case 'B': pkgB(hours); break; case 'C': pkgC(hours); break; }
2. 移除pkgC中冗余的输入逻辑
pkgC里的scanf("%i", &hours);完全多余——你已经通过inputHours()获取了用户输入的小时数,这里再次读取会覆盖传入的参数,直接删掉这行和对应的printf("%i\n", hours);即可。
3. 纠正无效的参数声明写法
你尝试的int pkgA(int &inputHours)是C++的引用语法,C语言不支持这种写法,会导致编译错误。C语言中传递参数只有值传递,若要修改原变量需使用指针。
&和*符号的使用场景
&(取地址符):- 用于获取变量的内存地址,比如
scanf(" %i", &hours),因为scanf需要知道变量的地址才能将输入值存入其中; - 当函数需要修改传入的变量时,需传递变量的地址,此时函数参数声明为指针类型,比如
void modify(int *num),调用时用modify(&num)。
- 用于获取变量的内存地址,比如
*(解引用符/指针声明):- 声明变量时用在类型前,表示这是一个指针变量,比如
int *p,表示p是存储int类型变量地址的指针; - 对指针变量使用时,表示获取指针指向的内存中的值,比如
*p = 10,就是把10赋值给p指向的变量。
- 声明变量时用在类型前,表示这是一个指针变量,比如
你的场景中不需要使用指针,因为pkgA/B/C仅需读取hours的值,无需修改它,直接传值即可。
代码格式优化建议
- 统一缩进:用4个空格作为缩进单位,保持代码层级清晰,原代码缩进存在混乱(比如函数内变量声明无缩进);
- 变量声明单独成行:将
int hours;、char hours_verify;这类变量声明单独占一行,避免拥挤; - 运算符加空格:在
=、<、||等运算符前后添加空格,比如hours < 0 || hours > 720,而非hours<0||hours>720; - 删除冗余代码:原main函数中的
inputHours;既不是函数调用(函数调用需加())也不是变量定义,直接删除; - 修正条件表达式:
100 < hours < 720在C语言中逻辑错误,C会先计算100 < hours得到0或1,再与720比较,结果永远为真。正确写法是hours > 100 && hours < 720,pkgB和pkgC中的条件都需要修改; - 注释清晰化:将英文注释改为中文或简化表述,比如
// 50小时以内的套餐价格。
修复后的完整代码
#include <stdio.h> #include <ctype.h> // 获取用户输入的小时数并验证 int inputHours() { int hours; char hours_verify; do { do { printf("Number of hours from 0 to 720: "); scanf(" %i", &hours); } while (hours < 0 || hours > 720); printf("%i hours, correct?\n", hours); printf("Y/N\n"); scanf(" %c", &hours_verify); } while (toupper((unsigned char)hours_verify) != 'Y'); printf("%i hours of internet were used.\n", hours); return hours; } // 计算套餐A的价格 int pkgA(int hours) { int pkgprice = 15; // 50小时以内的套餐价格 int extra_price = 2; // 超出50小时后的每小时价格 if (hours <= 50) { printf("The total price is:$%i\n", pkgprice); } else if (hours <= 720) { int extra_hours = hours - 50; float total_price = extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f\n", total_price); } return 0; } // 计算套餐B的价格 int pkgB(int hours) { int pkgprice = 20; // 100小时以内的套餐价格 float extra_price = 1.5; // 超出100小时后的每小时价格 if (hours <= 100) { printf("The total price is:$%i\n", pkgprice); } else if (hours > 100 && hours < 720) { int extra_hours = hours - 100; float total_price = extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f\n", total_price); } return 0; } // 计算套餐C的价格 int pkgC(int hours) { int pkgprice = 25; // 150小时以内的套餐价格 int extra_price = 1; // 超出150小时后的每小时价格 if (hours <= 150) { printf("The total price is:$%i\n", pkgprice); } else if (hours > 150 && hours < 720) { int extra_hours = hours - 150; float total_price = extra_hours * extra_price + pkgprice; printf("The total price is:$%.2f\n", total_price); } return 0; } int main(void) { char package_choice; char package_verify; do { printf("Which package would you like to select? A, B, or C "); scanf(" %c", &package_choice); printf("You chose %c, correct?\n", package_choice); printf("Y/N\n"); scanf(" %c", &package_verify); } while (toupper((unsigned char)package_verify) != 'Y'); printf("You chose package %c.\n", package_choice); int hours = inputHours(); switch(package_choice) { case 'A': pkgA(hours); break; case 'B': pkgB(hours); break; case 'C': pkgC(hours); break; } return 0; }
内容的提问来源于stack exchange,提问作者jtward17
相关产品推荐
相关产品推荐

