[英]I don't know why it can run on calculation?
My programming is about making a water bill.I had trouble running on my calculation part and it just show 0 when i key in any data. 我的编程工作是制作水费单,我在计算部分运行时遇到麻烦,当我输入任何数据时它只会显示0。 The calculation is amount=sqrfeet*2.05;
计算方式为数量= sqrfeet * 2.05; is it because of the floating part??
是因为有浮动部分吗? And i am using if else statement.
我正在使用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;
}
I don't know why it can run on calculation?
我不知道为什么它可以进行计算?
Either the compiler is weak or warnings are not fully enabled. 编译器功能弱或警告未完全启用。
Save time and enable all warnings and receive ones like the below. 节省时间并启用所有警告并接收如下警告 。 This will help locate many, though not all, problems.
这将有助于找到许多(尽管不是全部)问题。
// gcc example
gcc -std=c11 -O3 -pedantic -Wall -Wextra -Wconversion -c ...
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
警告:建议在赋值周围使用括号作为真值[-括号]
if (a = 1) {
The compiler sees this assignment and suggests if ((a = 1)) {
to quiet the warning. 编译器将看到此分配,并建议
if ((a = 1)) {
使警告安静。 Yet OP certainly does not want an assignment here, but a compare. 然而,OP当然不想在这里分配任务,而是比较。 @dbush
@dbush
if (a == 1) {
warning: 'sqrfeet' is used uninitialized in this function [-Wuninitialized]
警告:此功能未初始化时使用了'sqrfeet'[-Wuninitialized]
Code is using sqrfeet
before it is assigned. 代码在分配之前正在使用
sqrfeet
。 Certainly scanf("%f",&sqrfeet);
当然是
scanf("%f",&sqrfeet);
was desired. 是需要的。 @Tomek Piechocki
@Tomek Piechocki
// scanf("%f",&amount);
scanf("%f",&sqrfeet);
if(sqrfeet<=35){
warning: too many arguments for format [-Wformat-extra-args]
警告:格式[-Wformat-extra-args]的参数过多
The printf()
call is mal-formed. printf()
调用格式错误。
// printf("\nChoose one of the category:", a);
printf("\nChoose one of the category:");
warning: conversion to 'float' from 'double' may alter its value [-Wfloat-conversion]
警告:从“ double”转换为“ float”可能会更改其值[-Wfloat-conversion]
0.57
is a double
. 0.57
是double
。 No reason to use float
for typical floating point math. 没有理由将
float
用于典型的浮点数学运算。 Use double
. 使用
double
。 Save float
for large arrays or timing critical concerns. 为大型阵列或时序关键问题节省
float
。
// float amount, sqrfeet;
//scanf("%f", &amount);
double amount, sqrfeet;
scanf("%lf", &amount);
amount = sqrfeet * 0.57;
If you want to stay with float
, use a float
constant: 0.57f
. 如果要保留
float
,请使用float
常数: 0.57f
。
Also: all scanf()
specifiers consume leading white-space except, c, [, n
. 另外:所有
scanf()
说明符都使用前导空白,除了c, [, n
。 Add a space to consume the prior input line's left-over '\\n'
( Enter ). 添加一个空格以占用前一个输入行的剩余字符
'\\n'
( Enter )。
// scanf("%c",&ch);
// v
scanf(" %c",&ch);
I think that the problem is this line 我认为问题是这条线
scanf("%f",&amount);
I assume that you want it like this 我假设你想要这样
scanf("%f",&sqrfeet);
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;
}
Several issues: 几个问题:
sqrfeet
, so all your computations with sqrfeet
are going to be bad. sqrfeet
分配值,因此使用sqrfeet
进行的所有计算都会很糟糕。 You probably want a separate scanf
statement to read that when you also read amount
. amount
时,您可能希望使用单独的scanf
语句来读取该语句。 if
statements are using =
(assignment) instead of ==
(comparison), which is re-assigning a
. if
语句是使用=
代替(分配) ==
(比较),这是重新分配a
。 Instead of if ( a = 1 )
use if ( a == 1 )
. if ( a = 1 )
使用if ( a == 1 )
。 goto
when possible. goto
。 Your input loop can be better written as 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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.