首页 > 解决方案 > 回文检查器代码卡在无限循环中

问题描述

我在设计回文检查器时遇到问题。我对单个单词(“noon”、“2002”等)没有问题,但是每次我输入一个包含多个带空格的单词的短语(例如“laminate pet animal”)时,我的程序就会失去理智并进入无限循环。也许它与我放在里面的检查有关(确保字符串不是 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;
}

标签: c++stringloopsiteratorpalindrome

解决方案


std::cin>>运算符只读取到下一个空白字符。如果要阅读整行,请使用std::getline().

cin >> temp; //reads until the next whitespace
getline(cin, temp); //reads until the next newline character

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

这与您的问题无关,但通常最好不要使用using namespace std. 原因有很多种,但最基本的原因是,如果您编写自己的名为 的函数getline(),您会遇到问题。


推荐阅读