I am writing a program to create a text editor using a main.cpp file and a linked list class called LineList, and I keep hitting a segmentation fault when the marked line in triggered. I don't know what's causing this at all, I've tried multiple things to fix it, so any help is appreciated.
//Main code
int main(int argc, char* argv[])
{
char menuchoice = 'a', quitchoice = 'a';
int linenumber = 0;
constint ARGUMENTS = 2;
LineList linelist;
if (argc != ARGUMENTS)
{
cout << "It needs one command line argument: <input filename>" << endl;
return 1;
}
//Read input file into a LineList
string line;
//Input user-directed file
ifstream myfile(argv[1]);
if (myfile.is_open())
{
//Input current file contents into list, using InsertAtEnd function
int linenumber = 0;
while (getline(myfile, line)) //Problem happens around here
{
linelist.Insert(line, linenumber);
//Update linenumber
linenumber++;
}
myfile.close();
}
else
{
cout << "Unable to open file";
}
//Show menu and ask for user choice
showmenu;
//Run Print function
linenumber = 0;
while (linenumber < (listsize = linelist.GetListSize())
{
string printline = linelist.GetLine(linenumber);
cout << linenumber << "> " << printline << endl;
linenumber++;
}
//Run while user has not chosen to exit program
while (menuchoice != 'Q')
//Rest of code
//Relevant LineList code
class LineList
{
private:
NewLine* head;
int listsize;
string copystring;
public:
LineList(); //Constructor for LineList
~LineList(); //Destructor for LineList
void Insert(string text, int linenumber); //Insert new line at selected line number
void LineList::Insert(string text, int linenumber)
{
if (linenumber == 0)
{
NewLine* newline = new NewLine(text); //create new node
newline->next = head;
head = newline; //Insert new node at front of list
}
elseif (linenumber > 0 && linenumber < listsize)
{
NewLine* cur; //Create variables to traverse list
NewLine* pre;
cur = head;
pre = NULL;
int i = 0;
while (cur != NULL) //Go while list is not at end
{
i++;
if (i == linenumber) break; //Exit if line number is reached
pre = cur; cur = cur->next;
}
NewLine* newline = new NewLine(text); //create new line
newline->next = cur;
pre->next = newline; //Place new line in list
}
else
{
NewLine* newline = new NewLine(text); //create new node
newline->next = NULL;
NewLine* cur;
cur = head;
while (cur->next != NULL) //get to end of list
{
cur = cur->next;
}
while (listsize < linenumber)
{
NewLine* blankline = new NewLine(""); //create blank lines until desired line is reached
blankline->next = NULL;
cur->next = blankline; //insert blank line at end of list
listsize++; //update list size
}
cur->next = newline; //insert new line at end of list
}
listsize++; //Increase list size
}
Thanks again for any help!
Edit: I should mention that this program is designed to run through a UNIX terminal and that argv [1] is a text file provided by the user
All the code necessary to completely run the program would probably make it a very long forum post, so I put everything in a drive folder to save space here. Included is a readme with instructions on how to compile and the format for doing so