我的编程是关于制作水费单的,我在计算部分运行时遇到麻烦,当我输入任何数据时它只显示0。计算方式为数量= sqrfeet * 2.05;是因为有浮动部分吗?而且我正在使用if else语句。
int main()
{ char ch;
int a;
float amount, sqrfeet;
start:
printf("\n-------------Selangor Water Bill--------------\n");
printf("\n1.Domestic\n");
printf("\n2.Commercial\n");
printf("\n3.Industrial");
printf("\nChoose one of the category:",a);
scanf("%d",&a);
system("CLS");
if (a!=1&&a!=2&&a!=3){
goto start;
}
if (a=1){
printf("\n1.Domestic");
printf("\nEnter the amount of consumption water per sqrfeet in 35days:");
scanf("%f",&amount);
if(sqrfeet<=35){
amount=sqrfeet*0.57;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*1.03;
}
else if (sqrfeet>50){
amount=sqrfeet*2.00;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
else if(a=2){
printf("\n2.Commercial");
printf("\nEnter the amount of consumption water per sqrfeet in
35days:");
scanf("%f",&amount);
if(sqrfeet<=35){
amount=sqrfeet*2.07;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*2.28;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
else if (a=3){
printf("\n3.Industrial");
printf("\nEnter the amount of consumption water per sqrfeet in 35days:");
scanf("%f",&amount);
if(sqrfeet<=35){
amount=sqrfeet*2.49;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*2.70;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
printf("\nPress'q' to quit ");
scanf("%c",&ch);
return 0;
}
答案 0 :(得分:2)
我不知道为什么它可以在计算中运行?
要么编译器功能弱,要么警告未完全启用。
节省时间并启用所有警告,并收到如下警告。这将有助于发现许多(尽管不是全部)问题。
// gcc example
gcc -std=c11 -O3 -pedantic -Wall -Wextra -Wconversion -c ...
警告:建议在分配的圆括号内用作真值[-Wparentheses]
if (a = 1) {
编译器会看到此分配,并建议if ((a = 1)) {
静默该警告。然而,OP当然不想在这里分配任务,而是比较。 @dbush
if (a == 1) {
警告:'sqrfeet'在此功能中未初始化[-Wuninitialized]
代码在分配之前正在使用sqrfeet
。当然需要scanf("%f",&sqrfeet);
。 @Tomek Piechocki
// scanf("%f",&amount);
scanf("%f",&sqrfeet);
if(sqrfeet<=35){
警告:格式[-Wformat-extra-args]的参数过多
printf()
调用格式不正确。
// printf("\nChoose one of the category:", a);
printf("\nChoose one of the category:");
警告:从“ double”转换为“ float”可能会更改其值[-Wfloat-conversion]
0.57
是double
。没有理由为典型的浮点数学使用float
。使用double
。保存float
以应对大型阵列或对时序至关重要的问题。
// float amount, sqrfeet;
//scanf("%f", &amount);
double amount, sqrfeet;
scanf("%lf", &amount);
amount = sqrfeet * 0.57;
如果您想继续使用float
,请使用float
常量:0.57f
。
也:除scanf()
之外,所有c, [, n
指定符都使用前导空白。添加一个空间以占用前一个输入行的剩余'\n'
( Enter )。
// scanf("%c",&ch);
// v
scanf(" %c",&ch);
答案 1 :(得分:0)
我认为问题出在这行
scanf("%f",&amount);
我认为您想要这样
scanf("%f",&sqrfeet);
答案 2 :(得分:0)
int main()
{ char ch;
int a;
float amount, sqrfeet;
start:
printf("\n-------------Selangor Water Bill--------------\n");
printf("\n1.Domestic\n");
printf("\n2.Commercial\n");
printf("\n3.Industrial");
printf("\nChoose one of the category:",a);
scanf("%d",&a);
system("CLS");
if (a!=1&&a!=2&&a!=3){
goto start;
}
if (a=1){
printf("\n1.Domestic");
printf("\nEnter the amount of consumption water per sqrfeet in 35days:");
scanf("%f",&sqrfeet); // Change made here
if(sqrfeet<=35){
amount=sqrfeet*0.57;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*1.03;
}
else if (sqrfeet>50){
amount=sqrfeet*2.00;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
else if(a=2){
printf("\n2.Commercial");
printf("\nEnter the amount of consumption water per sqrfeet in
35days:");
scanf("%f",&sqrfeet); // Change made here
if(sqrfeet<=35){
amount=sqrfeet*2.07;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*2.28;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
else if (a=3){
printf("\n3.Industrial");
printf("\nEnter the amount of consumption water per sqrfeet in 35days:");
scanf("%f",&sqrfeet); // Change made here
if(sqrfeet<=35){
amount=sqrfeet*2.49;
}
else if(sqrfeet>35&&sqrfeet<=50){
amount=sqrfeet*2.70;
}
printf("\nTotal Amount is RM%.2lf", amount);
}
printf("\nPress'q' to quit ");
scanf("%c",&ch);
return 0;
}
答案 3 :(得分:0)
几个问题:
sqrfeet
分配一个值,因此使用sqrfeet
进行的所有计算都会很糟糕。当您还阅读scanf
时,您可能希望单独的amount
语句将其读取。
if
语句正在使用=
(分配)而不是==
(比较),后者正在重新分配a
。使用if ( a = 1 )
代替if ( a == 1 )
。
goto
。您的输入循环可以更好地写为int a = -1; // initialize a to a known bad value
do
{
// print menu options
/**
* Check the result of scanf - if it isn't 1, then the the
* user entered non-numeric input. Clear the bad input up
* to the next newline before trying again.
*/
if ( scanf("%d", &a ) != 1 )
while ( getchar() != '\n' )
;
} while ( a < 1 && a > 3 );