看板 EE_DSnP 關於我們 聯絡資訊
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
ric2k1:Basically this line "sum == x+y" just do the comparison 10/16 18:57
ric2k1:without affecting the value of sum. 10/16 18:58
ric2k1:the value of sum is basically the uninitialized value 10/16 18:58
ric2k1:in the line "int x, y, sum". 10/16 18:58
ric2k1:You can try to initialize sum by, say "int sum = 38" 10/16 18:59
ric2k1:and you should see sum is = 38. 10/16 19:00
ric2k1:Actually please turn on "-Wall" and you will see the 10/16 19:00
ric2k1:warning message "sum is used without initialization" 10/16 19:00
cg12037:那如果要計算sum 要怎麼做? 10/21 00:02