C++ 回文检查器代码卡在无限循环中

C++ 回文检查器代码卡在无限循环中,c++,string,loops,iterator,palindrome,C++,String,Loops,Iterator,Palindrome,我在设计回文检查器时遇到了一个问题。我对单个单词(“中午”、“2002”等)没有问题,但每次我输入一个包含多个空格单词的短语(如“层压宠物动物”)时,我的程序就会失去理智,进入无限循环。也许这与我放在里面的检查有关(确保字符串不为NULL或大于80个字符)?我一直在一步一步地调试,但没有成功。我认为这与字符串如何存储在内存中有关,但我不能准确地放置它 //Preprocessor directives #include <iostream> #include

我在设计回文检查器时遇到了一个问题。我对单个单词(“中午”、“2002”等)没有问题,但每次我输入一个包含多个空格单词的短语(如“层压宠物动物”)时,我的程序就会失去理智,进入无限循环。也许这与我放在里面的检查有关(确保字符串不为NULL或大于80个字符)?我一直在一步一步地调试,但没有成功。我认为这与字符串如何存储在内存中有关,但我不能准确地放置它

    //Preprocessor directives
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <iterator>
    #include <vector>
    #include <stdlib.h>

    using namespace std;

    //Function declarations
    string input();
    bool check(string a);

    int main()
    {
        //Repeater variable
        bool rep = 1;
        bool result = 0;

    //Declares string to be checked
    string palin;
    while (rep == 1)
    {
        //Creates string and calls input function
        palin = input();

        //Close function if stopped midway
        if (palin == "2")
        return 0;

        result = check(palin);

        //Displays the results
        if (result == 1)
            cout << palin << " is a palindrome." << endl;
        else
            cout << palin << " is not a palindrome." << endl;

        //Ask if the user wants to enter another Palindrome
        cout << "Continue? (1 for yes, 0 for no): ";
        cin >> rep;
    }

    cout << "Closing program..." << endl;
    system("pause");
    return 0;
}

string input()
{
    //Asks for and receives input string
    string temp;
    cout << "Please enter a string (type zero or 0 to quit): ";
    cin >> temp;

    //Close program if user enters 0 or zero
    if (temp == "0" || temp == "zero")
    {
        cout << "Exiting program..." << endl;
        system("pause");
        return "2";
    }

    //Check if string is null, then ask for input again
    if (temp.empty())
    {
        cout << "There is nothing entered. Please enter a string: ";
        cin >> temp;
    }

    //Check if string is too long, ask for input again
    if (temp.length() >= 80)
    {
        while (temp.length() > 80)
        {
            cout << "The string is too long. Please enter a smaller string: ";
            cin >> temp;
        }
    }
    return temp;
}

bool check(string a)
{
    //Creates 2 iterators that traverse the string
    string::iterator test1;
    string::reverse_iterator test2;

    test1 = a.begin();
    test2 = a.rbegin();

    //Continue until the end of either side of the string
    while (test1 != a.end() && test2 != a.rend())
    {
        //Check if the current symbol is alphanumeric
        while (test2 != a.rend() && !isalnum(*test2))
            ++test2;
        while (test1 != a.end() && !isalnum(*test1))
            ++test1;
        //Break loop when you hit the end
        if (test1 == a.end() || test2 == a.rend())
            break;
        //If they're not the same it's not a palindrome, exit function
        if (tolower(*test1) != tolower(*test2))
            return 0;

        ++test1;
        ++test2;
    }
    return 1;
}
//预处理器指令
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//函数声明
字符串输入();
布尔检查(字符串a);
int main()
{
//中继器变量
bool-rep=1;
布尔结果=0;
//声明要检查的字符串
佩林;
while(rep==1)
{
//创建字符串并调用输入函数
佩林=输入();
//如果中途停止,则关闭功能
如果(佩林=“2”)
返回0;
结果=检查(佩林);
//显示结果
如果(结果==1)

cout
std::cin
>
运算符最多只能读取下一个空格字符。如果要读取整行,请使用
std::getline()

发生的情况是,输入“race car”后的第一个读取操作将读取“race”,然后将“car”留在流中,然后下一个读取操作将读取“car”,导致意外行为,因为您的代码预期为1或0


这与您的问题无关,但是,原因有很多,但最基本的原因是您自己编写了名为
getline()的函数
,您将遇到问题。

std::cin
>
运算符最多只能读取下一个空格字符。如果要读取整行,请使用
std::getline()

发生的情况是,输入“race car”后的第一个读取操作将读取“race”,然后将“car”留在流中,然后下一个读取操作将读取“car”,导致意外行为,因为您的代码预期为1或0


这与您的问题无关,但是。原因有很多,但最基本的原因是如果您编写自己的名为
getline()
,您将遇到问题。

cin>>temp;
替换为
getline(cin,temp)
获取空格分隔的字符串和在
cin>>rep之后的字符串;
添加
cin.ignore();
刷新换行符。

cin>>temp;
替换为
getline(cin,temp);
获取空格分隔的字符串和在
cin>>rep;
添加
cin.ignore();
刷新换行符。

我觉得这段代码过于复杂:我将在几行代码中向您展示一个简化的版本。代码干净、简洁、可读性强、表达力强;它将完全按照它所说的做。然后我将解释您在完成我的实现时出错的地方e使用适当的工具或算法完成工作:



我觉得这段代码过于复杂了:我将在几行代码中向您展示一个简化的版本。这段代码干净、简洁、可读性强、表达能力强;它将完全按照它所说的那样做。然后我将解释您在使用适当的工具或alg时,在完成对我的实现的描述时出错的地方完成工作的方法:



为什么不干脆
::std::string r=argv[1];::std::reverse(r.begin(),r.end());return r!=argv[1];
?为什么不干脆
::std::string r=argv[1];::std::reverse(r.begin(),r.end());return r!=argv[1];
cin >> temp; //reads until the next whitespace
getline(cin, temp); //reads until the next newline character
#include <algorithm>
#include <iostream>
#include <string>

void runPalindrome();

int main() {
    runPalindrome();
    return 0;
}

void runPalindrome() {
    std::string quit;
    do {
        std::cout << "Please enter text to test if it is a palindrome:\n";
        std::string input;
        std::getline(std::cin, input, '\n');

        std::string checker(input);
        std::reverse(checker.begin(), checker.end());

        if (input == checker)
            std::cout << input << " is a palindrome!\n";
        else 
            std::cout << input << " is not a palindrome!\n";

        std::cout << "Press Q or q to (Q)uit or any other character to continue...\n";
        std::getline(std::cin, quit, '\n');

    } while ( quit != std::string( "Q" ) && quit != std::string( "q" ) );

    std::cout << "Exiting the program!\n";
}
{
    std::string str( "HelLo" );
    std::cout << str << '\n';
    std::transform( str.begin(), str.end(), str.begin(), ::toupper );
    std::cout << str << '\n';
    std::transform( str.begin(), str.end(), str.begin(), ::tolower );
    std::cout << str << '\n';
}