-4
我写下了下面的代码。问题是这样的:在我从输入流中读取x
和y
之后没有什么问题 - 意思是x
和y
是输入的确切值 - 但后来他们改变为其他值。为什么我的int在C++程序中间改变了?
这是怎么回事?我不明白!!
int count(char s[], char ss[] , long long int posF, long long int posE){}
int main()
{
char s[]{};
int q = 0;
cin >> s;
cin >> q;
int choise = 0;
while(q--)
{
cin>>choise;
if(choise == 1)
{
int x = 0;
cin>>x;
char c;
cin>>c;
s[x-1] = c;
}
else if(choise == 2)
{
int x = 0;
int y = 0;
cin>>x>>y;
//Fist LOG
cout<<"First log x and y are correct "<<x<<" "<<y<<endl;
char ss[]{};
cin>>ss;
//Second LOG
cout<<"Second log x and y are wrong?Why?"<<x<<" "<<y;
cout<<count(s, ss, x-1, y-1)<<endl;
}
}
return 0;
}
'个char [] {};'和'炭SS [ ] {};'不合法的C++。即使你的编译器提供了允许的扩展,cin >> s;和cin >> ss几乎肯定是未定义的行为。 –
答案很简单:未定义的行为。很可能是由于将数组引用超出了数组的范围。 –
我不知道你想用'char ss [] {}来做什么; cin >> ss;'但我敢肯定它的错误。 – Borgleader