Home > Software design >  Values in dynamic array cannot be assigned
Values in dynamic array cannot be assigned

Time:12-11

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.

CodePudding user response:

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.

CodePudding user response:

You have a problem with creation and deletion of your arrays here in your program as well as a few small misshaps on the way.

Fix:

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


class Stack{
private:
    //char stack[size];
    int size;
    char* stack;

public:
    Stack() : size(0), stack(NULL) {}
    ~Stack() {if (stack != NULL) delete stack;} //THIS IS NEEDED HERE

    void Init_Size(int newSize)
    {
        if (stack != NULL) delete stack;
        size = newSize;
        stack = new char[newSize]; //CREATES DYNAMIC ARRAY WHICH YOU NEED TO REMEMBER TO REMOVE OTHERWISE MEMORY LEAK
    }


    void push(char c) //adds
    {
        //adding here should work in a way:
        //a) create new dynamic array with different size
        //b) copy all elements into new array
        //c) delete array
        if (stack == NULL) Init_Size(1);
        char* newArray = new char[size 1];
        for(int i = 0; i < size; i  )
        {
            newArray[i] = stack[i];
        }
        newArray[size] = c;
        delete stack;
        stack = newArray;
        size  ;
        cout << "PUSHED " << stack[size-1] << " ONTO STACK" << endl;
    }

    char pop()
    {
        //removing here should work in a way:
        //a) create new dynamic array with different size
        //b) copy all elements into new array
        //c) delete array

        if (stack == NULL)
        {
            cout << "Stack empty" << endl;
            return '\0';
        }
        char* newArray = new char[size-1];
        for(int i = 0; i < size-1; i  )
        {
            newArray[i] = stack[i];
        }
        char temp = stack[size-1];
        delete stack;
        stack = newArray;
        size--;
        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);
}
  • Related