作者jasonfly07 (kanata)
看板EE_DSnP
標題[問題] a question about ==
時間Tue Oct 16 18:47:24 2007
in a simple programm for addition as follows:
#include <iostream>
using namespace std;
int main()
{
int x, y, sum;
cout << "Input the first integer: ";
cin >> x;
cout << "Input the second integer: ";
cin >> y;
sum == x + y;
cout << "The sum is: " << sum << endl;
system ("pause");
return 0;
}
what I've midified is swaping the "=" for "=="
I know their difference-- just want to see what will happen,
and whichever two numbers i give to it, the "sum" is always a specific number:
2294360
any explanation, please?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.117.201.149
1F:推 ric2k1:Basically this line "sum == x+y" just do the comparison 10/16 18:57
2F:推 ric2k1:without affecting the value of sum. 10/16 18:58
3F:→ ric2k1:the value of sum is basically the uninitialized value 10/16 18:58
4F:→ ric2k1:in the line "int x, y, sum". 10/16 18:58
5F:→ ric2k1:You can try to initialize sum by, say "int sum = 38" 10/16 18:59
6F:推 ric2k1:and you should see sum is = 38. 10/16 19:00
7F:→ ric2k1:Actually please turn on "-Wall" and you will see the 10/16 19:00
8F:→ ric2k1:warning message "sum is used without initialization" 10/16 19:00
9F:推 cg12037:那如果要計算sum 要怎麼做? 10/21 00:02