1

This class is a stack and I'm trying to get the user to call functions related to the stack. However, no changes seem to be made, since when calling the display function, my entire stack is filled with zeros. This is after pushing values onto the stack.

#include <iostream>
using namespace std;

class Stack   {

    public:
        int array[5];
        int top;


        Stack() {
            top = -1;
            for (int i = 0; i < 5; ++i) {
                array[i] = 0;
            }
        }   
 
        bool isEmpty()     {
            if (top == -1)  {
                return true;
            }
            return false;    }

        bool isFull()   {
            if (top == 4)   {
                return true;
            }
            return false;
        }

        void push(int val)  {
            if (isFull())   {
                cout <<"stack overflow" << endl;
            }
            else  {
                top++;
                array[top] == val;
            }    
        }       

        int pop()   {
            if (isEmpty())  {
                cout << "stack underflow" << endl;
            }
            else    {
                int val = array[top];
                array[top] = 0;
                top--;
                return val;
            }
        }

        int count() {
            return(top + 1);
        }

        int peek(int pos)  {
            if (isEmpty())    {
                cout << "stack underflow";
                    return 0;
            }
            else  {
                return array[pos];
            }
        }

        void change(int pos, int val) {
            array[pos] = val;
        }

        void display()  {
            for (int i = 4; i >= 0; --i) {
                cout << array[i];
            }
        }   

};
        

int main()  {
    Stack stack;
    int option, position, value;
    do
    {

        cout << "What operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
        cout << "1. Push" << endl;
        cout << "2. Pop" << endl;
        cout << "3. isEmpty()"<< endl;
        cout << "4. isFull()"<< endl;
        cout << "5. peek()"<< endl;
        cout << "6. count()"<< endl;
        cout << "7. change()"<< endl;
        cout << "8. display()"<< endl;
        cout << "9. Clear Screen"<< endl<< endl;

        cin >> option;
        switch(option)  {
            case 1:
                cout << "Enter item to push onto stack: " << endl;
                cin>>value;
                stack.push(value);
                break;
            case 2:
                cout << "Popping from stack: " << stack.pop() << endl;
                break;
            case 3:
                if (stack.isEmpty())    {
                    cout << "True" << endl;
                }
                else {
                    cout << "False" << endl;
                }
                break;
            case 4:
                if (stack.isFull()) {
                    cout << "True" << endl;
                }
                else    {
                    cout << "False" << endl;
                }
                break;
            case 5:
                cout << "Enter position to peek" << endl;
                cin >> position;
                cout << stack.peek(position) << endl;
                break;
            case 6:
                cout << stack.count() << endl;
                break;
            case 7:
                cout << "Enter position followed by value: " << endl;
                cin >> position >> value;
                cout << "Position changed" << endl;
                break;
            case 8:
                stack.display();
                break;
            case 9:
                system("cls");
                break;
            }
        } 
            while (option != 0);
            return 0;
    } 

For example the user would press 1 to call push() and push some input value. Then, they'd enter 8 to call display and it should show that input value in the stack, but prints 00000

  • [What is a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – PaulMcKenzie Dec 18 '20 at 18:11
  • 1
    Turn on your compiler warnings, or read the warnings that the compiler is giving you. You have several errors. For example, what do you return if `pop` detects that the stack is empty? – PaulMcKenzie Dec 18 '20 at 18:15
  • 1
    *"For example the user would press [...]"* -- this is a sign that you have distractions lying around that could obstruct your debugging. Forget having a user. Don't let user input interfere with your tests, with your [mre]. If you want `push()` to be called, then call `push()`. Create a copy of your program for your debugging, remove the I/O from that copy, and simply list the functions and data necessary to reproduce your issue. Perhaps reduce your main function to something like `int main() { Stack stack; stack.push(2); stack.display(); }`. – JaMiT Dec 18 '20 at 18:48
  • The program is still buggy, even if you fix the `==` error. If the user chooses choice `2` right from the start, your program encounters undefined behavior, since `int pop()` function does not return a value if the stack is empty. Not returning a value when it should return value is undefined behavior. Undefined behavior means that the program can crash, not crash, cause memory corruption, or seem to "work". You need to redo your `pop` function to actually work correctly when the stack is empty. – PaulMcKenzie Dec 18 '20 at 23:00

1 Answers1

4

You never actually put anything in the stack:

        void push(int val)  {
            if (isFull())   {
                cout <<"stack overflow" << endl;
            }
            else  {
                top++;
                array[top] == val;      // Change == to =
            }    
        } 

Currently, you are making a comparison.

MorningDewd
  • 501
  • 2
  • 6
  • There are other fatal errors. Can you spot them? – PaulMcKenzie Dec 18 '20 at 18:15
  • I don't see any fata errors, just some questionable design choices. `peek()` and `change()` don't do a boundary check for the pos argument, which is ok, except here where input to these functions comes from the user and can be anything - HOwever even that is fine since this clearly is for learning purposes. I also think it's problematic that `peek()` returns 0 if the stack is empty - i would prefer using exceptions or asserts for this, depending on how the structure is to be used. What are the fatal errors you are talking about? – MorningDewd Dec 18 '20 at 18:24
  • Look at the `pop` function closely, and the condition for an empty stack. See the comment I made in the main thread. Do you know what happens if you don't return anything from a function that is supposed to return a value? – PaulMcKenzie Dec 18 '20 at 18:25
  • The only thing i can see there is that pop() doesn't return a value in case `isEmpty()` returns true. `isEmpty()` itself looks fine to me. – MorningDewd Dec 18 '20 at 18:32
  • Not returning a value when a function is supposed to return a value is *undefined behavior*. Thus all someone has to do is choose choice `2` in the menu, and the program is in undefined behavior land. It seems you didn't grasp the entire point of this being a fatal flaw. – PaulMcKenzie Dec 18 '20 at 22:50