Jos*_*osh 0 c++ infinite-loop while-loop
我试图为回文实现一个解决方案,我认为我的逻辑是正确的,但我的程序陷入了无限循环,我收到"Prep.exe已停止工作"的错误消息
int main()
{
string word, reverse;
cout << "Please enter a word to test for palindrome : ";
cin >> word;
cout << "Your word is: "<< word << endl;
int i = 0;
int size = word.length() - 1;
while (size >= 0)
{
reverse[i++] = word[size--];
//cout << reverse[i++];
}
cout << "The reversed word is: " << reverse << endl;
if (word == reverse)
cout << "It is palindrome" << endl;
else
cout << "It is not a palindrome" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我不确定我的while循环错误.我继续递减它并且我有一个有效的退出条件,那么为什么它会陷入循环?
你没有得到无限循环; 由于该行中的Out Of Bounds访问权限而导致崩溃,reverse[i++]因为它reverse是一个空字符串.请尝试使用该reverse.push_back()功能.