title: Palindromes in C++
body: <p>I'm trying to make a program that uses stacks (w pop, push, etc.) to read in a text file with lots of sentences that are taken one at a time and outputs whether each line is a palindrome or not (words that are spelled the same forwards and backwards). I believe its very close to being a completed program, but it only returns false even when the string is a palindrome. I want it to return true when the string is in fact a palindrome.</p>
  
  <p>EDIT: Tried a new method with three stacks instead. Still getting a false return from bool tf every time.</p>
  
  <pre><code> int main() {
  Stack s(100);   // Initialize two different stacks
  Stack q(100);
  Stack temp(100);
  string line;        // String to hold each individual line of the file
  char letter;
   char x;            // For comparisons
   char y;
  
  // open the file
  ifstream input;
  
  input.open(READFILE);
  
  // Check that it is open/readable
  if (input.fail()) {
      cout &lt;&lt; endl &lt;&lt; "Sorry, file not available, exiting program. Press enter";
      cout &lt;&lt; endl;
  
      cin.get();  // Grab the enter
      return 0;
  }
  
  while (getline(input, line)) {                      // Read the file line-by-line into "line"
      cout &lt;&lt; "The line: " &lt;&lt; line &lt;&lt; endl;
  
      int length = line.length();                     // Sets length equal to string length
  
      for (int i =0; i&lt;length; i++){                  // Capitalizes string
              line[i] = toupper(line[i]);
      }
  
      for (int i = 0; i &lt; length; i++) {              // Loop through for every letter in the line
          if (line[i] == ' ' ) {
              line.erase(i,1);        // Takes spaces out of the line
              length--;
          }
          if (ispunct(line[i])){  
          length--;
          }
          if (!ispunct(line[i])){                     // Removes punctuation
          letter = line[i];                           // Push each letter onto the stack
          s.push(letter);         
          }
      }
  
      for (int i = 0; i &lt; length; i++) {          // Popping half the letters off of the s stack 
          s.pop(letter);                              // and pushing them onto the q stack
          q.push(letter);
          temp.push(letter);
      }
  
      for (int i = 0; i &lt; length; i++) {
          temp.pop(letter);
          s.push(letter);
      }
  
      bool tf = true;                                 // Pop off the top of each stack and compare
      while (!s.empty()) {                            // them to check for a palindrome
          s.pop(x);
          q.pop(y);
          if (x == y);
          else tf = false;
      }
      if (tf){
          cout &lt;&lt; "is a palindrome!" &lt;&lt; endl;
      }
      if (!tf) {
          cout &lt;&lt; "is NOT a palindrome" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>}</p>