我當時正在爲一堂課做練習,並且我決定看看如果我輸入char
,當代碼期待int
時會發生什麼。我在信'g'
只是爲了看看會發生什麼......它輸出-858993460
,我不知道爲什麼。爲什麼在這段代碼中將char'g'讀入int會產生數字-858993460?
下面是代碼:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int test;
cout << "Please enter a even integer or a odd integer. " << endl;
cin >> test;
cout << test; //here is where i got the -858993460
if (test % 2)
{cout << "TRIANGLE" << endl;}
else
{cout << "SQUARE" << endl;}
return 0;
}
那麼,這是否來自-858993460
?
-858993460是十六進制中的0xcccccccc。這意味着'int'在Visual Studio調試模式下未初始化。 – Marco167