Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why am i getting a segmentation fault(works in visusal studio)

+1 vote
asked Nov 22, 2019 by Samir Shahid (140 points)
/******************************************************************************

Welcome to GDB Online.

GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,

C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS

Code, Compile, Run and Debug online from anywhere in world.

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

#include <stdio.h>

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

struct node

{

char letter;

string bit;

string word;

node* next;

};

class S // only purpose of the stack is to fill the child threads correctly

{

public:

    node* top;

S()

{

top == NULL;

}

void push(char L, string B)

{

node* temp = new node;

temp->letter = L;

temp->bit = B;

if (top == NULL)

{

top = temp;

temp->next = NULL;

}

else

{

temp->next = top;

top = temp;

}

}

void pop()

{

if (top != NULL)

{

node* temp = top;

top = top->next;

delete temp;

}

}

int S_size()

{

int size = 0;

node* curr = top;

while (curr != NULL)

{

size++;

curr = curr->next;

}

return size;

}

void print()

{

node* curr = top;

while (curr != NULL)

{

if (curr->letter == '\n')

{

cout << "<EOL>" << " Binary code = " << curr->bit << endl;

curr = curr->next;

}

else

{

cout << curr->letter << " Binary code = " << curr->bit << endl;

curr = curr->next;

}

}

    return;

}

void decompress()

{

node* curr = top;

string m1 = "";

while (curr != NULL)

{

    if(curr == NULL)

    {

        return;

    }

int j = 0;

for (int i = 0; i < curr->bit.length(); i++)

{

if (curr->bit[i] == '1')

{

curr->word += curr->letter;

}

else if (curr->bit[i] == '0')

{

curr->word += m1[j];

j++;

}

}

m1 = curr->word;

curr = curr->next;

}

for (int i = 0; i < m1.length();i++)

{

if (m1[i] == '\n')

{

cout << "<EOL>";

}

else

{

cout << m1[i];

}

}

}

};

int main()

{

S o;

string word;

int s;

char a;

string b;

ifstream infile;

infile.open("input.txt");

while (getline(infile, word)/*!infile.eof()*/)

{

if (word != "") {

if (word.substr(0, 5) == "<EOL>")

{

a = '\n';

b = word.substr(6, word.length());

}

else

{

a = word[0];

b = word.substr(2, word.length());

}

//cout << a << ":" << b << endl;

}

//cout << a << ":" << b << endl;

o.push(a, b);

}

//s = o.S_size();

//cout << "size: " << s << endl;

o.print();

cout << "Decompressed file contents: " << endl;

o.decompress();

system("PAUSE");

return 0;

}

1 Answer

+1 vote
answered Nov 23, 2019 by gameforcer (2,950 points)
In your constructor you used 2 equation signs instead of 1. I don't know for sure if it's the only issue but it sure is a funny one.
commented Nov 23, 2019 by Samir Shahid (140 points)
fml i want to kill myself. that was it thanks its been bothering me like crazy <3
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...