palindrome trouble

This is a discussion on palindrome trouble within the C++ Programming forums, part of the General Programming Boards category; I'm doing a data structure program where i write in a palidrome it puts it into a stack then into ...

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    palindrome trouble

    I'm doing a data structure program where i write in a palidrome it puts it into a stack then into a queue then back to the stack and it also verifies that it is or isnt a palindrome what am i doing wrong

    Code:
    class Pali{
    public:
          Pali();
          Pali(char str[]);
         ~Pali();
          void instack();
         void inqueue();
         void outstack();
          bool verify();
    private:
             stack<char>stackelems;
              queue<char>queueelems;
             stack<char>stackelems2;
               CString char cstrelems[];}
    
    Pali::Pali(){instack();};
    Pali::Pali(char str[]){
     strcpy(str,cstrelems);
    instack();};
    Pali::~Pali(){};
    void Pali::instack(){
        while(!stackelems.empty()){
              cstrelems.push();}
        inqueue();}
    void Pali::inqueue(){
        while(!queueelems.empty()){
             stackelems.pop();
              queueelems.push();}
       outstack();}
    void Pali::outstack(){
           while(!stackelems2.empty()){
                   queueelems.pop();
                    stackelems2.push();}
     if(verify()==true){
          while(!stackelems2.empty()){
                    cout<<stackelems2.top();
                     stackelems2.pop();}
      else{cout<<"This is not a palindrome!"<<endl;}}
                   cout<<"\n";}
    bool Pali::verify(){
     if((stackelems==stackelems2)==true){
                  return true;}
       else 
                   return false;
      }
    int main(){
               char arr;
               Pali sarr;
                  cout<<"Enter a palindrome: "<<endl;
                   cin>>arr;
                  sarr(arr);
     return 0;}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    int main()
    {
        char arr;
        Pali sarr;
        cout<<"Enter a palindrome: "<<endl;
        cin>>arr;
        sarr(arr);
        return 0;
    }
    If you want to test whether something is a palindrome or not, you should allow the user to enter in more than a single character as input.

    Do you really need all of this? The simplest way to check if something is a palindrome or not is to get the users input into a string variable, assign a second string as the reversed version of the input string, then do a compare of the two strings. This is only a few lines of code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    yes i do need all of this code it has to be a data structure use Stacks and Queues as i stated
    whatelse am i doinng wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 11:36 AM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 04:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21