单个函数有效,但从另一个函数添加变量不起作用



在计算包的总价格之前,我的代码运行得没有任何问题。从本质上讲,它是一个套餐选择,你输入小时数,它会计算出选择的总价格。每个包函数各自独立工作(我只是获取了代码并添加了另一个int和scanf作为输入)。然而,无论它在整个大代码的过程中,inputHours中的int都没有正确地传递过来。为了使代码更简单,我取出了包B和包C。我想弄清楚如何让包A工作,并以此为基础对其他两个包进行建模。

#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/Nn");
scanf(" %c", &hours_verify);

}while(toupper((unsigned char)hours_verify) != 'Y');

printf("%i hours of internet were used.n", 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 main(void)
{
char package_choice;
char package_verify;
do
{
do{
printf("Which package would you like to select? A, B, or C ");
scanf(" %c", &package_choice);
}while(toupper((unsigned char)package_choice) != 'A' 
&& toupper((unsigned char)package_choice) != 'B' 
&& toupper((unsigned char)package_choice) != 'C');

printf("You chose %c, correct?n", package_choice);
printf("Y/Nn");
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(hours);
break;

case 'B':
pkgB(hours);
break;

case 'C':
pkgC(hours);
break;
}
}

我已经把"int小时"使用"int inputhours()"在许多地方看看是否可能是这样,重新测试了个别代码(再次使用int小时和扫描,只是为了看看它是否按照预期的方式工作),没有问题。有时我得到的代码是"总价是15美元";当它应该更多的时候。51小时应该是17美元)。

作为评论指出,inputHours函数没有返回。您希望在函数结束时返回hours

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/Nn");
scanf(" %c", &hours_verify);

}while(toupper((unsigned char)hours_verify) != 'Y');

printf("%i hours of internet were used.n", hours);
return hours;
}

现在,当您使用以下命令时,您期望的值将被分配给main中的局部变量hours

int hours=inputHours();

首先要纠正的是返回inputHours()(credit @Barmar)的值。

然后,如果你想复制一个有效的版本,有这个:

void pkgA( int hours ) { // nothing is returned, so "void"
double cost = 15; // use "double" instead of "float"
if( hours > 50 ) // only deal with "excess hours" case
cost += (hours - 50) * 2.0; // add excess cost to the base cost
printf( "The total price is:$%.2f", cost ); // a single print statement
}

这个例子更小,这意味着你的代码将更小,更容易维护。

最后,您可能会尝试将3个函数合并为一个函数,并将3个不同的常量作为该函数的参数处理。例如:

double pkg( int hours, int baseHrs, double cost, double excess ) {
if( hours > baseHrs )
cost += (hours - baseHrs) * excess;
return cost;
}

编辑(新部分)
提出对该OP的先前响应,这里有一种验证用户输入的方法:

#include <string.h>
//...
do {
// Prompt for package choice and get input...
} while( !strchr( "ABCabc", package_choice ) );

现在,进入处理:

double val = 0; // another variable
// ...
switch(package_choice) {
case 'A':
case 'a':
val = pkg( hours, 50, 15.0, 2.0 );
break;
case 'B':
case 'b':
val = pkg( hours, 100, 30.0, 1.5 );
break;
case 'C':
case 'c':
val = pkg( hours, 200, 60.0, 1.0 );
break;
}
printf( "The total price of '%c' is:$%.2f", package_choice, val );
// ....

最新更新