Dragon Orb XIII
Dragon Orb XIII

Reputation: 1

Palindrome tester has logic flaws

This is just a basic palindrome tester for my C++ class, and there appears to be issues.

I already know that I have two separate flaws in here somewhere. At least one, I strongly suspect, is a logic issue. The first problem is that it runs fine the first time through, but when the loop initiates, it doesn't ask for user input to put in a new line to test as a palindrome, it simply retests the old one. The second issue is, I assume, that it is testing spaces, which I base off the fact that it's giving 'hannah' back as good, but 'never even or odd' comes back bad. This one I just don't know how to fix.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  bool repeater = true;
  do
    {
      string palindroneCheck;
      bool palindronity = true;

      cout << "Please enter a line to test for palindromity.\n";
      getline(cin, palindroneCheck);

      int stringSize = palindroneCheck.size();
      int cutOff = stringSize/2;

      for (int palindroneLength = 0; palindroneLength < cutOff; palindroneLength++)
        {
          if (palindroneCheck[palindroneLength] != palindroneCheck[stringSize - palindroneLength -1])
            {palindronity = false;
              break;}
        }

      if(palindronity == true)
        cout << "Congratulations! This line is a palindrone!\n\n";
      else
        cout << "Sorry, but this is not a palindrone.\n\n";

      palindroneCheck.clear();

      char repeat;
      cout << "Would you like to try another line? Y/N\n";
      cin >> repeat;
      if (repeat == "n" || repeat == "N")
        repeater = false;
    } while (repeater == true);

}

Upvotes: 0

Views: 160

Answers (2)

CS Pei
CS Pei

Reputation: 11047

The reading issue of getline is solved by comments. For the whitespaces, you can tackle it by removing all the spaces inside string palindroneCheck,

std::string::iterator new_end = std::remove(palindroneCheck.begin(), palindroneCheck.end(), ' ');
std::string palindroneCheckWithoutSpaces(palindroneCheck.begin(), new_end);

Then you use palindroneCheckWithoutSpaces to do the Palindrone test.

  int stringSize = palindroneCheckWithoutSpaces.size();
  int cutOff = stringSize/2;

  for (int palindroneLength = 0; palindroneLength < cutOff; palindroneLength++)
    {
      if (palindroneCheckWithoutSpaces[palindroneLength] != palindroneCheck[stringSize - palindroneLength -1])
        {palindronity = false;
          break;}
    }

  if(palindronity == true)
    cout << "Congratulations! This line is a palindrone!\n\n";
  else
    cout << "Sorry, but this is not a palindrone.\n\n";

(you need header algorithm to use remove)

Update:

std::remove remove an element from the input range (this is defined by begin and end here) based on the value you passed in , here is the whitespace ' '. Then it return the new end of the changed range (since you delete something, the range becomes smaller). The new range starts with begin and ends with the returned value.

So the second line you create a new string based on the new range.

Upvotes: 0

rabensky
rabensky

Reputation: 2934

OK, you are right about the spaces. Your code will demand that spaces are in the same location like every other character.

The other bug seems more subtle: it's where you ask to repeat or not.

Why? Because it asks, you enter 'n' and then 'enter'

The cin >> repeat only reads the 'n', but not the 'enter'

so the next time you do `readline(cin,PalindromCheck)' it will read an empty string.

Try to write palindromCheck just after reading it. You'll see.

Upvotes: 1

Related Questions