我当时正在为一堂课做练习,并且我决定看看如果我输入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