Simple Text Editor

  • + 0 comments

    Can Someone Explain what is wrong with this solution? Passes Only 4 test cases, others are giving segmentation fault :C

    C++ Solution

    int main()
    {
        stack<string> st;
        string input = "";
        st.push(input);
        int choice;
        string data, disp_data;
        int n, pos;
        cout << "Operations: ";
        cin >> n;
        while (n--)
        {
            cout << "Choice: ";
            cin >> choice;
    
            if (choice == 1)
            {
                cin >> data;
                input = input + data;
                st.push(input);
            }
            else if (choice == 2)
            {
                cin >> pos;
                input.erase(input.size() - pos, input.size());
                st.push(input);
                pos = 0;
            }
            else if (choice == 3)
            {
                cin >> pos;
                disp_data = st.top();
                cout << disp_data[pos - 1] << endl;
            }
            else if (choice == 4)
            {
                st.pop();
                input = st.top();
            }
        }
        return 0;
    }