3
#include <iostream>
#include <ctype.h>

using namespace std;

void isPalindrome();

int main()
{
    char response;
    isPalindrome();
    cout << "Input another string(y/n)?" << endl;
    cin >> response;
    response = toupper(response);
    if (response == 'Y')
        isPalindrome();

    return 0;
}

void isPalindrome()
{
    char str[80], str2[80];
    int strlength;
    int j = 0;
    int front, back;
    bool flag = 1;

    cout << "Input a string:" << endl;
    cin.getline(str, 80);
    strlength = strlen(str);

    for (int i = 0; i < strlength; i++)
    {
        if (islower(str[i]))
            str[i] = toupper(str[i]);
    }

    for (int i = 0; i < strlength; i++)
    {
        if (isalpha(str[i]))
        {
            str2[j] = str[i];
            j++;

        }
    }

    str2[j] = '\0';
    front = 0;
    back = strlength - 1;

    for (int i = 0; i < j / 2; i++)
    {
        if (str2[front] != str2[back])
        {
            flag = 0;

            break;
        }
    }

    if (!(flag))
        cout << "It is not a palindrome" << endl;
    else
        cout << "It's a palindrome" << endl;

    cout << "str: " << str << " str2: " << str2 << " strlength: " << strlength << " j: " << j << endl;
    cout << "front: " << front << " back: " << back << " flag: " << flag << endl;
}

I was just wondering if anybody could help explain to me why my code isn't working.

I can run it once just fine and I get the right answer, but when the prompt asks if I want to input another string and I type 'y', the prompt just skips over the input and terminates on it's own.

I tried cin.ginore('\n', 80), but that just gave me a bunch of blank lines. I added the bit of code at the end to check the values and they all go to 0 and drop the strings.

Maybe a link to a proper explanation of how the system handles memory?

edit: I keep getting the same problem when running the input sequence a second time. The output looks like this:

    Input a string:
    Radar
    It's a palindrome
    Input another string(y/n)?
    y


    _ <- this being my cursor after pressing enter 3 times

I'll just re-build the program from scratch and try to do it without a function. I'd still appreciate a link to a page that explains how to process user input using modern c++.

lvl1_monk
  • 33
  • 3
  • 2
    I surely don't want to read through that *mess* to explain anything, but if you're interested in how to process user input using modern C++, let me know and I can post that. – Kerrek SB Nov 24 '11 at 03:33
  • @KerrekSB Or you can post a link so you don't have to reinvent the wheel, right? :) – Mateen Ulhaq Nov 24 '11 at 03:49
  • I would certainly appreciate that. I'm going to be working on this all night if I have to, so any help would be greatly appreciate. – lvl1_monk Nov 24 '11 at 03:49

3 Answers3

1

The problem is with:

cin >> response;

This reads the user input y/n into the variable response but a newline is left in the input buffer which is picked by the getline function the isPalindrome function.

To fix this you need to remove the newline from the input buffer after you read the user response. You do it by using:

cin >> response;
std::cin.ignore(INT_MAX);

With the above fix you can retry the palindrome check just once. To make multiple retries possible you'll need a loop. I would recommend a do-while loop in your main as:

char response;
do {
        isPalindrome();
        cout << "Input another string(y/n)?" << endl;
        cin >> response;
        std::cin.ignore(INT_MAX);
        response = toupper(response);
} while(response == 'Y');
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

You need a loop. There's no code that instructs the program to go back to the top.

char response = 'Y';
while (response == 'Y') {
    isPalendrome();
    cout << "Input another string(y/n)?" << endl;
    cin >> response;
}

This isn't your entire program, just key elements that you need for a while loop. You should get an understanding of how while works and make this work for your program.

Godwin
  • 9,739
  • 6
  • 40
  • 58
  • I just ran your code, there's another issue that's preventing isPalendrome from working more than once. – Godwin Nov 24 '11 at 03:47
  • @lvl1_monk If you want to reply to me, you should add an `@` symbol in front of my name. The only reason I saw your comment just now was because I had this tab open in the background, and I refreshed the page. – Mateen Ulhaq Nov 24 '11 at 03:48
0

In contemporary C++ one would typically use standard library components for string processing:

#include <iostream>
#include <string>

int main()
{
    std::string line1, line2, response;

    do
    {
        std::cout << "First string: ";
        if (!std::getline(std::cin, line1)) { /* error */ }

        std::cout << "Second string: ";
        if (!std::getline(std::cin, line2)) { /* error */ }

        // check line1 and line2 for palindromy

        std::cout << "Again (y/n)? ";
        std::getline(std::cin, response);

    } while (std::cin && (response == "y" || response == "Y"));
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Ah, yes one would hope so, however homework C++ is not contemporary C++, in many cases it cannot even be called C++ it is more like C, compiled with some non-standard compiler the school got for a small (huge) fee sometime before 1998. – r_ahlskog Nov 24 '11 at 07:03
  • @r_ahlskog: I'm never sure if "homework C++" is the result of terrible, terrible teaching, or just of massive confusion on part of the learner. I imagine there's a selection bias since we only see those who post on SO, but it's fairly unsettling... – Kerrek SB Nov 24 '11 at 13:00
  • Well in my school at least it was "This is not how you would do it in reality but we are going to do it like this now because otherwise it would be too complicated" when approaching the subject of reading a sample temperature log from a database and presenting min-max-average in a C# program, 4th year CS majors mind you. But yes I guess there is a bias, and every day I hope I won't ever have to work with some of the people posting here. – r_ahlskog Nov 24 '11 at 17:12