Search code examples
c++arraysstringpointersstack

Values in dynamic array cannot be assigned


I am using a stack to reverse a given string. Each character of the string is pushed onto the stack, then they are popped back into a new string which is returned to main. But for some reason, the value of stack[top] in the stack class code never seems to change.

#include <iostream>
#include <string>
using namespace std;

const int size = 4;

class Stack{
    private:
        //char stack[size];
        char* stack;
        int top;
    
    public:
        Stack()
        {
            top = -1;
        }
        
        void Init_Size(int size)
        {
            stack = new char[size];
        }
        
        bool isFull()
        {
            if (top == (size - 1))
                return true;
            return false;
        }
        
        bool isEmpty()
        {
            if (top == -1)
                return true;
            return false;
        }
        
        void push(char c)
        {
            if (isFull())
            {
                cout << "STACK IS FULL" << endl;
                return;
            }
            top++;
            stack[top] == c;
            cout << "PUSHED" << stack[top] << " ONTO STACK" << endl;
        }
        
        char pop()
        {
            if (isEmpty())
            {
                cout << "STACK IS EMPTY" << endl;
                return '/'; //error
            }
            
            char temp = stack[top];
            cout << stack[top] << endl;
            top--;
            cout << "POPPED" << temp << " OFF STACK" << endl;
            return temp;
        }
};

string ReverseString(string str)
{
    Stack stack;
    string result;
    
    stack.Init_Size(str.length());
    
    for (int i=0; i < str.length(); i++)
    {
        stack.push(str[i]);
    }
    
    for (int i=0; i < str.length(); i++)
    {
        result[i] = stack.pop();
    }
    
    return result;
}

int main()
{
    string str;
    
    cin >> str;
    
    cout << ReverseString(str);
}


I tried using a normal array for the stack instead of a dynamic one, but no change, however, I did notice that the value of the stack array can be changed but only in the void Init_Size(int size) function and I'm not sure why.


Solution

  • I see a number of errors in your program:

    1. size: You have a global constant size set to 4 which is used in your isFull function, independent of the local size parameter used in Init_Size. Make this a class variable instead.

    2. Your push function has the line stack[top] == c with two = instead of one - thus comparing the value against stack instead of setting stack. Turn on warnings and you should have found this yourself

    3. ReverseString assigns directly to result[i] without ever setting the size of result. You could have used the str variable instead, since that is just a copy.

    Those three are the major errors that need fixing for your program to work, but there are several minor issues as well, like using namespace std; thereby making size ambigious, making unnecessary copies of strings, using new and c-style arrays etc.