调用Palindrome函数后获取空白行

时间:2013-02-28 04:36:54

标签: c++ getline palindrome

所以我在程序的顶部有这两个函数:

string deletespaces(string sentence){
    sentence.erase(std::remove(sentence.begin(), sentence.end(), ' '), sentence.end());
    return sentence;
}

string checkpalindrome(string sentence){
    deletespaces(sentence);
    if (sentence == string(sentence.rbegin(), sentence.rend())){
        cout << sentence << " is a palindrome." << endl;
    }
    if(sentence != string(sentence.rbegin(), sentence.rend())){
        cout << sentence << " isn't a palindrome." << endl;
    }
}

这是我的主要代码。

int main(){
    int x = 1;
    string originalsentence;

    while (x == 1) {
        int a = 1;
        int input;
        string sentence;
        cout << "Press 1 to reverse a string, 2 to check a palindrome, and anything else to quit: ";
        cin >> input;

        if(input == 1) {
            cout << "Enter a sentence to reverse: ";
            cin.ignore();
            getline(cin,originalsentence);
            originalsentence = sentence;
            cout << string(sentence.rbegin(), sentence.rend()) << endl;
        }

        if(input == 2) {
            cout << "Enter a sentence to check: ";
            cin.ignore();
            getline(cin,originalsentence);
            originalsentence = sentence;
            checkpalindrome(sentence);
        }

        cout << "Hit 1 if you want to continue, anything else to quit: ";
        cin >> x;
    }
}

所以这是我的问题。我可以选择是否反转字符串或检查字符串是否是回文。但是,当我尝试反转字符串时,它只是给出一个空行,然后询问我是否要继续。当我要求检查回文时,无论我输入什么,它都只是说“是回文”。有什么想法吗?

0 个答案:

没有答案