Don't know why program keeps crashing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Don't know why program keeps crashing

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    Don't know why program keeps crashing

    I am reading in values from a file, and am not sure what's wrong with my code? My main issue is within the area in bold, can anyone tell me what's wrong?

    Thanks.

    Code:
    void createList(intNode*& intList)
    {
        intNode* lastInt; //points to last integer in file
        lastInt = NULL;
        int fileInt; //int read from input file
        ifstream intInputFile;
    
        intInputFile.open("intInput.txt");
        if (intInputFile.is_open())
        {
            cout << "intInput.txt open successful" << endl;
        }
        else
        {
            cout << "intInput.txt open unsuccessful" << endl;
        }
        intInputFile >> fileInt;
        while(!intInputFile.eof())
        {
            intNode* anotherInt;
            anotherInt = new intNode;
            if(intList==NULL)
            {
                intList = anotherInt;
                lastInt = anotherInt;
            }
            else
            {
                lastInt->nextNode = anotherInt;
            }
            lastInt = lastInt->nextNode;
            lastInt->intValue = fileInt;
            lastInt->nextNode = NULL;
            intInputFile >> fileInt;
        }
        intInputFile.close();
        cout << "List created from input file" << endl;
    }

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

    Re: Don't know why program keeps crashing

    Quote Originally Posted by SMA01 View Post
    I am reading in values from a file, and am not sure what's wrong with my code? My main issue is within the area in bold, can anyone tell me what's wrong?
    First, post the entire call to the function. We have no idea if the parameter you passed in is bogus or not, and everything relies on that parameter being valid. Note that your loop is wholly dependent on what was passed, as you're checking for NULL and doing different things if intList is NULL or not.

    Second, did you debug your code, or are you just running it and having it crash? If it is the latter, please use the debugger to debug the code. More than likely those pointers are invalid or uninitialized , and you're trying to write to an uninitialized pointer.

    Bottom line is this -- you're asking us to figure out your logic. You wrote the program, so you had some sort of logic in mind, and translated what you had in mind to code. So does your program follow your logic? Where does the program go counter to what you expected? That's what debugging is all about.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 19th, 2012 at 01:00 AM.

  3. #3
    Join Date
    Dec 2012
    Posts
    4

    Re: Don't know why program keeps crashing

    This is the entire code up to the function I am having trouble with. I have fixed it a bit. It's not crashing anymore, but it is not creating the list correctly. Not sure why it's still not working right. Thanks.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    
    
    using namespace std;
    
    //Define struct
    struct intNode
    {
        int intValue;
        intNode* nextNode;
    };
    
    //Functions used
    void createList(intNode*& intList);
    void addItem(int, intNode*& intList);
    void deleteItem(int, intNode*& intList);
    void searchItem(int, intNode*& intList);
    void lengthList(intNode*& intList);
    void printList(intNode*& intList);
    void outputList(intNode*& intList);
    
    int main()
    {
        intNode* intList; //Points to beginning of integer list
        intList=NULL;
        int userEnteredInt;
        char choice;
    
        createList (intList);
    
        cout << "Enter A to add an item, D to delete an item, S to search for an item, L for the length of the list, P to print list, or E to exit:"<< endl;
        cin >> choice;
    
        while(choice!='E')
        {
            switch(choice)
            {
                case 'A': case 'a': //Add item
                {
                    cout <<"Enter the integer you wish to add:" << endl;
                    cin >> userEnteredInt;
                    addItem(userEnteredInt, intList);
                    break;
                }
                case 'D': case 'd': //Delete item
                {
                    cout << "Enter the integer you wish to delete:" << endl;
                    cin >> userEnteredInt;
                    deleteItem(userEnteredInt, intList);
                    break;
                }
                case 'S': case 's': //Search for item
                {
                    cout << "Enter the integer you wish to search for:" << endl;
                    cin >> userEnteredInt;
                    searchItem(userEnteredInt, intList);
                    break;
                }
                case 'L': case 'l': //Length of list
                {
                    lengthList(intList);
                    break;
                }
                case 'P': case 'p': //Print items on list
                {
                    printList(intList);
                    break;
                }
                default:
                    cout << "Invalid entry" <<endl;
            }
            cout << "Enter A to add an item, D to delete an item, S to search for an item, L for the length of the list, P to print list, or E to exit:"<< endl;
            cin >> choice;
        }
        outputList(intList); //Display items in list
        return 0;
    }
    
    void createList(intNode*& intList)
    {
        intNode* lastInt; //points to last integer in file
        lastInt = NULL;
        int fileInt; //int read from input file
    
        ifstream intInputFile;
        intNode* anotherInt;
        anotherInt = new intNode;
    
        intInputFile.open("intInput.txt");
        if (intInputFile.is_open())
        {
            cout << "intInput.txt open successful" << endl;
            intInputFile >> fileInt;
            cout << "check" <<endl;
            while(intInputFile>>fileInt)
            {
                if(intList==NULL)
                {
                    intList = anotherInt;
                    lastInt = anotherInt;
                    lastInt->nextNode = NULL;
                    lastInt->nextNode = new intNode;
                }
                else
                {
                    lastInt = lastInt->nextNode;
                    lastInt->nextNode = NULL;
                    lastInt->nextNode = new intNode;
                }
                lastInt->intValue = fileInt;
                intInputFile >> fileInt;
                cout <<"good"<<endl;
            }
             intInputFile.close();
             cout << "List created from input file" << endl;
        }
        else
        {
            cout << "intInput.txt open unsuccessful" << endl;
        }
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Don't know why program keeps crashing

    Quote Originally Posted by SMA01 View Post
    This is the entire code up to the function I am having trouble with. I have fixed it a bit. It's not crashing anymore, but it is not creating the list correctly. Not sure why it's still not working right.
    Before writing any code, did you draw on paper how a link list works, i.e. adding, removing, etc.? If not, you need to do that, and then take that visual on paper and translate it to code.

    Most of the time, all it takes is to draw on paper two boxes representing two links, and arrows representing what links go where when adding to a linked list. Once that's done, the coding becomes simple, practically automatic.

    Second, forget about reading from a file for the moment. Does your linked list work, regardless of where the data comes from? The code you have now is an intermingling of file handling and other non-linked-list related things, causing confusion and code that is hard to maintain and debug.

    Your "CreateList" function should just do that -- create a list with no nodes. Then you take this list that has no nodes in it returned to you. Then you write your loop to read in an item by repeatedly call "addItem" for each data in the file. Currently, your createList is not correct in that it doesn't just try to create a list -- it tries to do everything, including building every node. Why is your CreateList trying to do something that addItem is supposed to do? The CreateList creates an empty list, and addItem adds an item to the list. Nothing else than that.

    By doing things the way I stated, you can easily test adding an item without reading anything from a file. Just hard-code an int and add it to the list to see if it works. Once you have that working, then and only then do you consider writing a loop to add items to the list via file reading.
    Code:
    MyList *theList = createList();
    for_each_item_in_the_file
    {
          input_stream >> theItem;
          theList->addItem( theItem, theList );
    }
    This is the psuedo-code for what I described.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 19th, 2012 at 04:28 PM.

  5. #5
    Join Date
    Dec 2012
    Posts
    4

    Re: Don't know why program keeps crashing

    Thank you, I have createList working now. I am currently fixing the other functions. I know what you mean, but I followed an algorithm provided by my professor, and this is how we wrote it out. This is an intro Computer Science course, so I have developed the best programming techniques yet. Thanks again.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Don't know why program keeps crashing

    Quote Originally Posted by SMA01 View Post
    Thank you, I have createList working now. I am currently fixing the other functions. I know what you mean, but I followed an algorithm provided by my professor, and this is how we wrote it out.
    Regardless of the algorithm, you should write the code in a manner where you're not writing code over and over again.

    If you look at your addItem() function (if you wrote it already), you should see that the code is exactly the same as the internals of the createList() function. If you write the addItem() once, and then just have createList() repeatedly call addItem() to add to the linked list, then you get a real test of whether addItem() works or not. If there is a problem in adding an item to a list, the problem is isolated to one place, not several places where code is duplicated.

    Also, it is very rare for any new C++ coder to write things like linked lists correctly the first time, even if the program seems to work. For example, you need to destroy the list of nodes at the end of the program, else you get a memory leak. Did you do that? There are other holes in the code that you posted, all due to the complexity of the C++ language and handling of dynamically allocated memory properly. In all honesty, using C++ for data structures is not really for the beginner coder. A language that is forgiving and not highly dependent on user-controlled memory management such as Java should be used instead.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Dec 2012
    Posts
    4

    Re: Don't know why program keeps crashing

    Thank you. Yes, I am actually not a Computer Science major. I'm a math major, and we have to take a computer science course. It was a little difficult. Thank you for your help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center