0

So I have these two functions at the top of my program:

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

and

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;
    }
}

This is my main body of code.

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;
    }
}

So here is my problem. I am able to put in my choice about whether to reverse a string or check whether the string is a palindrome. However, when I try to reverse the string, it simply gives a blank line, then asks whether I want to continue. When I ask to check a palindrome, it simply says "is a palindrome" regardless of what I input. Any ideas?

Jonas
  • 6,915
  • 8
  • 35
  • 53
  • A suggestion: why don't you just enclose `cout << sentence << " isn't a palindrome." << endl;` with an `else`? – Mark Garcia Feb 28 '13 at 04:38
  • So I made that change, and now it outputs "is a palindrome" Segmentation fault (core dumped) –  Feb 28 '13 at 04:42
  • You're not using the return value from `deletespaces`, and `checkpalindrome` should be returning a string but isn't. You should also think about why you get data into `originalsentence` and then immediately overwrite it with `sentence`. – Retired Ninja Feb 28 '13 at 05:02
  • How would I do those things? I thought that I already typecast `checkpalindrome` as a string when I started the function with `string checkpalindrome`. –  Feb 28 '13 at 05:13
  • Why the hell do you do this `originalsentence = sentence;` after this `getline(cin,originalsentence);` ??? – borisbn Feb 28 '13 at 05:35
  • Ok I've got it fixed to everything works! But after it prints out whether something is a palindrome or not, it core dumps. –  Feb 28 '13 at 05:38
  • and borisbn, I did that because I want to, after I get the kinks out of here, print out the original sentence without the spaces removed. I made the mistake of having `originalsentence = sentence` instead of `sentence = originalsentence`. –  Feb 28 '13 at 05:39

0 Answers0