CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    52

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Linked List Problem

    You could have saved yourself all of this trouble and just use std::list.
    Code:
    #include <list>
    http://www.cplusplus.com/reference/stl/list/

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2011
    Posts
    52

    Re: Linked List Problem

    Quote Originally Posted by Paul McKenzie View Post
    You could have saved yourself all of this trouble and just use std::list.
    Code:
    #include <list>
    http://www.cplusplus.com/reference/stl/list/

    Regards,

    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    52

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Linked List Problem

    Quote Originally Posted by josh26757 View Post
    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.

  6. #6
    Join Date
    Mar 2011
    Posts
    52

    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.

  7. #7
    Join Date
    Mar 2011
    Posts
    52

    Re: Linked List Problem

    Quote Originally Posted by Paul McKenzie View Post

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Linked List Problem

    Quote Originally Posted by josh26757 View Post
    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

  9. #9
    Join Date
    Mar 2011
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured