-
December 5th, 2011, 02:54 PM
#1
Linked List Problem
I am building a program for my brother to edit a few things on his website that I do not want to be bothered with. The one I am working on now is the EVENTS page. What is going on is that I am reading a JavaScript file and as the lines are called the elements are broken down and stored into a linked list. Once the editing is done the file is cleared and rewritten. A default page is saved as a backup if any problems arise. The problem I am having has to be me being stupid. I am in a function making a new node for the linked list. I ask the user for all the inputs the try to assign the next and last attributes to the node. At that point the program crashes? I have no idea why.
Code:
void NewEvent(Event *list)
{
Event *NewEvent = new Event;
NewEvent = SetEvent();/// gets the user data for the event
NewEvent->next = list;
list->last = NewEvent; /// this is where the debugger catches a problem
list = NewEvent;
WriteEventFile(list);
}
I will post all the code if the problem is not an obvious one.
-
December 5th, 2011, 03:20 PM
#2
Re: Linked List Problem
You could have saved yourself all of this trouble and just use std::list.
http://www.cplusplus.com/reference/stl/list/
Regards,
Paul McKenzie
-
December 5th, 2011, 03:23 PM
#3
Re: Linked List Problem
Originally Posted by Paul McKenzie
this is very true! But being a beginner I am trying to fully understand the basics and learn from problems like this. Any suggestions or is more code needed? I feel like I am missing some rule here.
-
December 5th, 2011, 03:38 PM
#4
Re: Linked List Problem
Here is the function called "IN" the current function:
Code:
Event *SetEvent()
{
string event = "", location = "", date = "", time = "", note = "";
Event *New = new Event;
cout<<"Please Enter The Event Name\n";
while(event == "")
getline(cin, event);
cout<<"Please Enter The Event Location\n";
while(location == "")
getline(cin, location);
cout<<"Please Enter The Event Date\n";
while(date == "")
getline(cin, date);
cout<<"Please Enter The Event Time\n";
while(time == "")
getline(cin, time);
cout<<"Please Enter Any Additional Notes You Want Added\n";
while(note == "")
getline(cin, note);
New->date = "Date:" + date;
New->event = "Event:" + event;
New->location = "Location:" + location;
New->note = "Note:" + note;
New->time = "Time: " + time;
New->next = NULL;
New->last = NULL;
return New;
}
Before this function was a simple menu and if...else statement.
This is the last function call to write the file, but again the problem lies before this.
The write function rely on a one event per line format.
Code:
Event *WriteEventFile(Event *list)
{
fstream myfile(EventJava, ios::out);
///formate beginning////////////////////////////////////////////////
myfile<<"function events(){"<<endl
<<"document.getElementById('center').innerHTML = '<p>'"<<endl;
///END FORMAT///////////////////////////////////////////////////////
///WRITING BODY/////////////////////////////////////////////////////
while(list != NULL)
{
myfile<<"+"<<"'"<<list->event<<"<br>"<<list->location<<"<br>"<<list->date
<<"<br>"<<list->time<<"<br>"<<list->note<<"<br><br>"<<"'"<<endl;
list = list->next;
}
///END OF BODY/////////////////////////////////////////////////////
///ENDING FORMAT///////////////
myfile<<"+'</p>';}"<<endl;
myfile.close();
///END ////////////////////////
}
The struct used for the Linked List -->
Code:
struct Event{
string event, date, location, time, note;
Event *next;
Event *last;
};
Last edited by josh26757; December 5th, 2011 at 03:41 PM.
-
December 5th, 2011, 03:52 PM
#5
Re: Linked List Problem
Originally Posted by josh26757
this is very true! But being a beginner I am trying to fully understand the basics and learn from problems like this. Any suggestions or is more code needed? I feel like I am missing some rule here.
Let me ask you:
Is your goal
1) To finish your program for your brother's website, bug-free and easy to maintain, probably in a matter of hours, if not minutes,
or
2) learn C++ up to the intermediate level, which may take weeks if not months, so that you know how to handle dynamically allocated memory properly?
Professional C++ programmers use std::list, and that is part of the "basics". Those basics entail learning how to use the C++ standard library properly.
Dynamic memory allocation and properly using pointers is not a beginner topic. Leave that alone until you take a data structures course in school. In the C++ programming world, most programmers use the standard library.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 5th, 2011 at 03:56 PM.
-
December 5th, 2011, 03:57 PM
#6
Re: Linked List Problem
Bug free is obviously the goal. I don't think I am that far from the program working though. It seems like the solution would be easy for a experienced programmer. If you feel the solution would be harder than re-writing with the template then I am open to do so.
-
December 5th, 2011, 03:59 PM
#7
Re: Linked List Problem
Originally Posted by Paul McKenzie
Dynamic memory allocation and properly using pointers is not a beginner topic. Leave that alone until you take a data structures course in school. In the C++ programming world, most programmers use the standard library.
Regards,
Paul McKenzie
I guess I should be patient because my next semester is algorithms and data structures.
-
December 5th, 2011, 04:07 PM
#8
Re: Linked List Problem
Originally Posted by josh26757
The struct used for the Linked List -->
Code:
struct Event{
string event, date, location, time, note;
Event *next;
Event *last;
};
Here is an example of what I'm referring to:
Code:
#include <list>
#include <string>
struct Event
{
std::string event, date, location, time, note;
Event(const std::string& event_,
const std::string& date_,
const std::string& location_,
const std::string& time_,
const std::string& note_) :
event(event_), date(date_), location(location_),
time(time_), note(note_) { }
};
typedef std::list<Event> EventList;
int main()
{
EventList evList;
// add 3 events to the eventList linked list
evList.push_back(Event("Event1", "","","",""));
evList.push_back(Event("Event2", "","","",""));
evList.push_back(Event("Event2", "","","",""));
}
I just added 3 Events to a linked list. I know this works correctly without even compiling or running it. Why? Because it is guaranteed to work due to usage of standard library, and not my home-made linked list which could have bugs.
No memory leaks, no pointers, no calls to new[]/delete[], nothing. You can also remove items from the linked list, sort it, etc.
Regards,
Paul McKenzie
-
December 5th, 2011, 04:29 PM
#9
Re: Linked List Problem
Thanks, I had already visited the reference and understand why even a professional would choose to use this. Type errors just happen and create bugs so this tested template makes since. Thanks for the great example. I am already rewriting the code and will let you know what I come up with for further criticism. Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|