这只是我的 C++ 类的基本回文测试器,似乎存在问题。
我已经知道我在这里的某个地方有两个不同的缺陷。我强烈怀疑,至少有一个是逻辑问题。第一个问题是它第一次运行良好,但是当循环启动时,它不会要求用户输入新行来测试回文,它只是重新测试旧行。第二个问题是,我认为,它正在测试空间,我基于这样一个事实,即它让“汉娜”回归良好,但“从不偶数或奇数”回归糟糕。这个我只是不知道如何解决。
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool repeater = true;
do
{
string palindroneCheck;
bool palindronity = true;
cout << "Please enter a line to test for palindromity.\n";
getline(cin, palindroneCheck);
int stringSize = palindroneCheck.size();
int cutOff = stringSize/2;
for (int palindroneLength = 0; palindroneLength < cutOff; palindroneLength++)
{
if (palindroneCheck[palindroneLength] != palindroneCheck[stringSize - palindroneLength -1])
{palindronity = false;
break;}
}
if(palindronity == true)
cout << "Congratulations! This line is a palindrone!\n\n";
else
cout << "Sorry, but this is not a palindrone.\n\n";
palindroneCheck.clear();
char repeat;
cout << "Would you like to try another line? Y/N\n";
cin >> repeat;
if (repeat == "n" || repeat == "N")
repeater = false;
} while (repeater == true);
}