Thread: not getting the desired output. WHY???

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    not getting the desired output. WHY???

    Once again I am having a problem. I am trying to do varoius operations with stacks. It doesn't look like it is filling the stack. I only tried to print the stack and print the second value so far. I saw that they were not working so I stopped even though I have written the other functions. It also sounds like I have a loop in the print2nd function. Any advice???


    Code:
    #include <iostream>
    #include <cstdlib>
    #include "stackType.h"
    #include<fstream>
    
    //function prototypes
    
    void PrintStack(stack& s);
    void Print2nd(stack& s);
    void PrintBottom(stack& s );
    void Palindrome(stack& s );
    
    // declaring file stream variables
    
    ifstream inData;
    ofstream outData;
    
    
    int main()
    {
    //declaration of variables
    el_t element;
    stack original;
    const int SENTINEL = -999;
    int c = 0;
    
    
    //fills stack with at least 25 elements from an input file
    
       
        while((!original.StackIsFull() && element != SENTINEL))
        {
        inData>>element;
        original.Push(element);
        }
          
    //Opening of files
    inData.open("a:/stackinfo.txt");
    outData.open("a:/stackoutput.txt");  
    
    //Operations on Functions or Function Calls
    
    outData<<"copy of stack is printed and original stack is unchanged"<<endl;
    PrintStack(original);
    outData<<"print 2nd element of stack"<<endl;
    Print2nd(original);
    
    
    
    //Close files
    inData.close();
    outData.close();
    
    
    //  system("PAUSE");	
      return 0;
    }
    
    // Function to print all elements in a stack and leave stack unchanged 
    // after printing
    //Pre: The stack is non empty
    //Post: 
    
    void PrintStack(stack& s)
    {
        el_t e;
        stack temp;
        
        while (!s.StackIsEmpty())
        {
            s.Pop(e);
            temp.Push(e);
            outData<<e;
        }
            while(!temp.StackIsEmpty())
            {
                temp.Pop(e);
                s.Push(e);
            }
    }
    
    //Function prints 2nd element from the stack and leaves stack unchanged
    void Print2nd(stack& s)
    {
        el_t e1, e2;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(e1);
            s.Pop(e2);
            outData<<e2;
            s.Push(e2);
            s.Push(e1);
        }
    }
    
    //Function prints bottom of stack and leaves stack empty
    void PrintBottom(stack& s )
    {
        el_t e;
        stack temp;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(e);
            temp.Push(e);
        }
        cout<<e;
    }
    
    //Function determines whether the characters form a palindrome
    void Palindrome(stack& s )
    {
        stack u,u2;
        el_t ch, ch1;
        bool equal = true;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(ch);
            u.Push(ch);
            u2.Push(ch);
        }
            while(!u.StackIsEmpty())
            {
                u.Pop(ch);
                s.Push(ch);
            }
                while(!u2.StackIsEmpty() && equal)
                {
                     u2.Pop(ch);
                     s.Pop(ch1);
                       if (ch != ch1)
                           equal = false;
                }
                     if (equal)
                         cout<<"Palindrome"<<endl;
                              else
                                 cout<<"Not a Palindrome"<<endl;
    } //end of Palindrome Function
    
    //  File: stackType.h        Class, constant values, and type definitions for c++ static
    //  implementation of ADT stack.  This is the specification file for the class stack
    
    
    #ifndef STACK_H
    #define STACK_H
    
    // application dependent size of stack
    const unsigned int MAX_NUM_ELS = 256;
    
    typedef char el_t;       // type varies with application
    typedef int index_t;
    
    class stack
    {
        public:
            stack();                                //stack constructor
            ~stack();                               //stack deconstructor
            bool   StackIsEmpty(void);              //Is stack empty?
            bool   StackIsFull(void);               //Is stack full?
            void   Push(el_t);                      //push item
            void   Pop(el_t&);                      //pop to item
            
        private:
            void   StackError(char*);               //error handler
            index_t top;                            //top index: -1 if empty
            el_t   el[MAX_NUM_ELS];                 //container for elements
     };
     
     #endif
     
    //  File: stackao.cpp  Contents: C++ source code for static
    //  implementation of ADT stack.  This is the implementation file.
    //
    
    #include<iostream>
    //#include<stdlib.h>
    #include "stackType.h"
    
    using namespace std;
    
    
    //Default constructor
    //Pre:  None
    //Post: The stack object is initialized to be empty
    //
    
    stack::stack(void)
    {
        top = -1;
    }
    
    //Stack destructor
    //Pre: The stack has gone out of scope or an explicit call
    //     to ~stack has occurred.
    //Post: The stack is empty.
    
    stack::~stack()
    {
        top = -1;
    }
    
    //Boolean function that returns TRUE if the stack is empty
    //Pre:  The stack has been initialized.
    //Post: The function returns TRUE if the stack is empty, 
    //      FALSE otherwise.
    
    bool stack::StackIsEmpty(void)
    {
        return (top == -1 ? true : false);
    }
    
    //Boolean function that returns TRUE if a stack is full
    //Pre:  The sack has been initialized.
    //Post:  The function has returned TRUE if the stack is full,
    //       FALSE otherwise.
    
    bool stack::StackIsFull(void)
    {
        return (top == (MAX_NUM_ELS -1) ? true : false);
    }
    
    //Function to push an element onto a non_full stack
    //Pre:  e is a data element of typr el_t.
    //      The stack is not full.
    //Post: If the stack was not full, e was placed on top of
    //      the stack; otherwise StackError was called.
    
    void stack::Push(el_t e)
    {
        if ( StackIsFull())
            StackError("Stack is fulle\n");
        else
        {
            ++top;
            el[top] = e;
        }
    }
    
    //  Function to pop an element from a nonempty stack and
    //  store it in the refernced variable
    //  Pre:  The stack is  nonempty.
    //  Post: The top stack element has been removed and stored
    //        in e.  If the stack was empty, StackError was called.
    
    void stack::Pop(el_t& e)
    {
        if (StackIsEmpty())
            StackError(" Stack is empty \n");
        else
        {
            e = el[top];
            --top;
        }
    }
    
    //  Function to print an error message and abort the program
    //Pre:  An unrecoverable error occurred.
    //      ErrorMessage is an error message.
    //Post: ErrorMessage has been output and the program aborted.
    
    void stack::StackError( char* ErrorMessage)
    {
        cerr<< ErrorMessage;
        exit(1);
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm seeing areas that look problematic but I'll need to see some code from the stack class.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >//fills stack with at least 25 elements from an input file
    >
    >   
    >    while((!original.StackIsFull() && element != SENTINEL))
    >    {
    >    inData>>element;
    >    original.Push(element);
    >    }
          
    >//Opening of files
    >inData.open("a:/stackinfo.txt");
    You should open the input file before the while-loop, so:
    Code:
    //Opening of files
    inData.open("a:/stackinfo.txt");
    //fills stack with at least 25 elements from an input file
    
       
        while((!original.StackIsFull() && element != SENTINEL))
        {
        inData>>element;
        original.Push(element);
        }
    I gave you stack functions a cursory glance, and didn't see any obvious problems.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You should also initialize element to something besides the value of SENTINEL, so it has some value the first time thru the while-loop:
    el_t element = 0;

    It looks like that's what you were doing with c here, but never used c.
    int c = 0;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >//Function prints 2nd element from the stack and leaves stack unchanged
    >void Print2nd(stack& s)
    >{
    >    el_t e1, e2;
    >    
    >    while(!s.StackIsEmpty())
    
    This should be if instead of while:
        if(!s.StackIsEmpty())

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM