1

I'm working on an assignment for class. We were provided with these three classes. (Linked here, the editor was flipping out when I tried to do them on separate lines).

We're supposed to take in a string and then use these classes to test if it's a palindrome. We can't modify said classes.

And here's my int main:

#include <iostream>
#include <cstring>
#include <cctype>
#include "stack.h"

using namespace std;

int main () {
   char testString[100];
   Stack<char> charStack;

   cout << "Please enter a string:\n> ";
   cin.getline(testString, 100);

   char caps;
   for (int i = 0; i < strlen(testString); i++) {
      if (isalpha(testString[i])) {
         caps = toupper(testString[i]);
         charStack.push(caps);
      }
   }

   for (int j = 0; j < strlen(testString); j++) {
      if (isalpha(testString[j])) {
         caps = toupper(testString[j]);
         if (charStack.firstPtr->getData() == caps) { // This part is the issue. firstPtr is private in list.h, and I can't figure out another way to compare the char array to the data in the stack
            charStack.pop(caps);
         }
      }
   }

   if (charStack.isStackEmpty()) {
      cout << endl << endl << "\"" << testString << "\"  IS a palindrome." << endl;
   }
   else {
      cout << endl << endl << "\"" << testString << "\"  is NOT a palindrome." << endl;
   }
}

As you can see, I can't quite figure out how to compare the popped data to the data in the char array. The Stack class only returns a boolean, and the pointer to the ListNode object in class List is private, so I can't use the "getData" function of that class! Anyone have any tips that could help me out?

Thanks!

user1888527
  • 543
  • 1
  • 7
  • 12
  • palindromes's are easy. Why use a class for them? – Deduplicator Apr 23 '14 at 21:59
  • That's the assignment. Our professor is aware that it's much easier to do it other ways. – user1888527 Apr 23 '14 at 22:00
  • Are you kidding, having three links in 'these three classes' !??? –  Apr 23 '14 at 22:02
  • Pop, compare, push back if not equal? – indiv Apr 23 '14 at 22:03
  • First pop it, thus yyou get the data, then compare. – Deduplicator Apr 23 '14 at 22:03
  • @DieterLücking: The editor thought I had code in my post when I tried to separate them by line. Sorry. – user1888527 Apr 23 '14 at 22:03
  • 1
    @indiv: Wait, how do I compare after popping? Edit: Actually, just figured it out! Didn't notice that "value" was passed in by reference. So I can just pass in a blank test char, then compare that to the c-string! Thanks! – user1888527 Apr 23 '14 at 22:05
  • 2
    Please tell your professor that `List` is missing a copy constructor, [which is bad](http://stackoverflow.com/q/4172722/592323). – leemes Apr 23 '14 at 22:24
  • I don't understand why your stack `pop` method even takes an argument. Stacks pop elements off the top of the stack, which is why it's called a stack. –  Apr 24 '14 at 00:15

1 Answers1

2

Look at function Stack::pop(STACKTYPE &data)

It takes a non-const reference argument in which it stores the element being removed (this actually happens in the implementation of List::removeFromFront(NODETYPE &value) ).

That means that you can pass a char to the pop() function and afterwards it will contain the data you're looking for.

cangrejo
  • 2,189
  • 3
  • 24
  • 31