The code isn't displaying and then reversing entered numbers.

     reverse.cpp

    //
    //
    #include <cstdlib>

    #include <iostream>
    #include "linkedStack.h"

    using namespace std;

    int main()
    {
        linkedStackType<int> stack1;
        linkedStackType<int> stack2;
        cout << "";  
        cout << "output your name\n\n";

        int number = 0;

        // AUSE LINKED STACKS 
            //
        cout << "Enter first number 1 to be stored in Stack 1: ";
        cin >> number;
        cout << "Enter first number 2 to be stored in Stack 1: ";
        cin >> number;
        cout << "Enter first number 3 to be stored in Stack 1: ";
    cin >> number;
        //
        //  PUSH NUMBERS ON STACK
       stack1.push(number);
        //
        // REVERSE NUMBERS IN STACK 1
        // AND SAVE THEM IN STACK 2;
       stack1.reverseStack(stack2);
        cout << "\n\nNumbers in Stack 1: ";
        while (!stack1.isEmptyStack())
        {
            cout<<stack1.top()<<" ";

            stack1.pop();
        }
        cout << endl;

        cout << "Numbers in Stack 2, which is Stack 1 in reverse order: ";

        while (!stack2.isEmptyStack())
        {
            // OUTPUT NUMBERS IN STACK 2
            cout<<stack2.top()<<" ";
            stack2.pop();
        }

        cout << endl;
        cout << "\n\n\n\n";
        system("pause");

        return 0;
    }



    /Header file: stackADT.h

    #ifndef H_StackADT
    #define H_StackADT

    //*************************************************************
    /
    //*************************************************************

    template <class Type>
    class stackADT
    {
    public:
        virtual void initializeStack() = 0;
           //Method to initialize the stack to an empty state.
           //Postcondition: Stack is empty.

        virtual bool isEmptyStack() const = 0;
          //Function to determine whether the stack is empty.
          //Postcondition: Returns true if the stack is empty,
          //    otherwise returns false.

        virtual bool isFullStack() const = 0;
          //Function to determine whether the stack is full.
          //Postcondition: Returns true if the stack is full,
          //    otherwise returns false.

        virtual void push(const Type& newItem) = 0;
          //Function to add newItem to the stack.
          //Precondition: The stack exists and is not full.
          //Postcondition: The stack is changed and newItem is added
          //    to the top of the stack.

        virtual Type top() const = 0;
          //Function to return the top element of the stack.
          //Precondition: The stack exists and is not empty.
          //Postcondition: If the stack is empty, the program 
          //    terminates; otherwise, the top element of the stack 
          //    is returned.

        virtual void pop() = 0;
          //Function to remove the top element of the stack.
          //Precondition: The stack exists and is not empty.
          //Postcondition: The stack is changed and the top element
          //    is removed from the stack.
    };

    #endif



    /Header file: stackADT.h

    #ifndef H_StackADT
    #define H_StackADT

    //*************************************************************

    //*************************************************************

    template <class Type>
    class stackADT
    {
    public:
        virtual void initializeStack() = 0;
           //Method to initialize the stack to an empty state.
           //Postcondition: Stack is empty.

        virtual bool isEmptyStack() const = 0;
          //Function to determine whether the stack is empty.
          //Postcondition: Returns true if the stack is empty,
          //    otherwise returns false.

        virtual bool isFullStack() const = 0;
          //Function to determine whether the stack is full.
          //Postcondition: Returns true if the stack is full,
          //    otherwise returns false.

        virtual void push(const Type& newItem) = 0;
          //Function to add newItem to the stack.
          //Precondition: The stack exists and is not full.
          //Postcondition: The stack is changed and newItem is added
          //    to the top of the stack.

        virtual Type top() const = 0;
          //Function to return the top element of the stack.
          //Precondition: The stack exists and is not empty.
          //Postcondition: If the stack is empty, the program 
          //    terminates; otherwise, the top element of the stack 
          //    is returned.

        virtual void pop() = 0;
          //Function to remove the top element of the stack.
          //Precondition: The stack exists and is not empty.
          //Postcondition: The stack is changed and the top element
          //    is removed from the stack.
    };

    #endif

Recommended Answers

All 5 Replies

I think it's an error on lines 39 and 50 but I'm not sure.

Your problem is between lines 23 - 31. You get 3 numbers from the user but you only use 1 variable so everytime you overwrite the previous number that the user input. Then at line 31 you push a single value, the last onbe entered, onto the stack. This comment // PUSH NUMBERS ON STACK at line 30 is distinctly confusing because it is not what the code is doing at all.

You need to push the value onto the stack after each time the user enters it.

What would be a better way to do this? I updated my cpp file to the one below and it worked. I was now wondering if there was a better way?

reverse.cpp

        //
        //
        #include <cstdlib>

        #include <iostream>
        #include "linkedStack.h"

        using namespace std;

        int main()
        {
            linkedStackType<int> stack1;
            linkedStackType<int> stack2;
            cout << "";  
            cout << "output your name\n\n";

            int number = 0;
            int number2 = 0;
            int number3 = 0;


            // AUSE LINKED STACKS 
                //
            cout << "Enter first number 1 to be stored in Stack 1: ";
            cin >> number;
            cout << "Enter first number 2 to be stored in Stack 1: ";
            cin >> number2;
            cout << "Enter first number 3 to be stored in Stack 1: ";
            cin >> number3;
            //
            //  PUSH NUMBERS ON STACK
            stack1.push(number);
            stack1.push(number2);
            stack1.push(number3);
            //
            // REVERSE NUMBERS IN STACK 1
            // AND SAVE THEM IN STACK 2;
           stack1.reverseStack(stack2);
            cout << "\n\nNumbers in Stack 1: ";
            while (!stack1.isEmptyStack())
            {
                cout<<stack1.top()<<" ";

                stack1.pop();
            }
            cout << endl;

            cout << "Numbers in Stack 2, which is Stack 1 in reverse order: ";

            while (!stack2.isEmptyStack())
            {
                // OUTPUT NUMBERS IN STACK 2
                cout<<stack2.top()<<" ";
                stack2.pop();
            }

            cout << endl;
            cout << "\n\n\n\n";
            system("pause");

            return 0;
        }

If by better way you mean you want to continue to use the same variable as before all you would have to do is push number on the stack after it has been input by the user like this:

            // AUSE LINKED STACKS 
                //
            cout << "Enter first number 1 to be stored in Stack 1: ";
            cin >> number;
            stack1.push(number);
            cout << "Enter first number 2 to be stored in Stack 1: ";
            cin >> number;
            stack1.push(number);
            cout << "Enter first number 3 to be stored in Stack 1: ";
            cin >> number;
            stack1.push(number);

I'm not sure if that's what you're reffering to as a better way.
Either way should work, one is more readable because you seperate what is going on, and the other way (this way) saves memory by only using one variable instead of 3, but unless you're running this on very old hardware, I doubt it matters.

I'm just curious of different ways to do things. I really appreciate the help. I tested your way and it works too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.