程序4
如果你是一个程序员,肯定犯过下文程序中包含的错误,如果你正在学习编程,也将会犯这样的错误,而且如果找不出问题所在,它也会令你焦头烂额
1 /*******************************************
2 * test logic for a simple accounting *
3 * program *
4 *******************************************/
5 #include<iostream>
6
7 int main()
8 {
9 //Amount owed (if any) by the user
10 int amount;
11
12 std::cout<<“enter current balance:”;
13 std::cin>>amount;
14
15 if(amount=0)
16 std::cout<<“you owe noting\n”;
17 else
18 std::cout<<“you owe”<<amount<<“\n”;
19
20 return(0);
21 }
答案:g++编译器输出如下警告:
equal.cpp:In function ‘int main()’:
equal.cpp:15:warning:suggest parentheses around assignment used as truth value
典型的运行过程如下
$equal
enter current balance:10
you owe 0
$equal
enter current balance:0
you owe 0
$equal
enter current balance:-10
you owe 0
因为语句 if (amount=0) 并不比较amount 和 0 的值
应该写成 if (amount==0)