1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pointers/Linked Lists in C++

  1. May 7, 2014 #1
    1. The problem statement, all variables and given/known data

    The program is supposed to take in a text file that has a letter and an integer on each line separated by a space. The integer is the order the letter is supposed to come in. The assignment is to write a program that takes the letters and inserts them using pointers into a linked list. For example;

    1 e
    3 c
    0 b
    4 h
    2 a

    should spell out "beach".

    2. Relevant equations

    n/a

    3. The attempt at a solution

    The below code is what I wrote - it will successfully take a file in and decode a message from it once - but attempts after that will cause it to freeze. It gets stuck in an infinite loop (?) somehow in the "while (infile.get(letter))..." loop.

    Since it's compiling and not crashing I'm getting no feedback on what's going wrong. All I can suppose is that I'm not handling the pointers properly, but I have no idea in which way.

    Code (Text):

    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std;

    struct Node {
      int place;
      char letter;
      Node *next;
    };

    class secretMsgs {
    public:
        void addAtHead(Node* &h, char letterGiven, int placeGiven);
        void insertInOrder(Node* &h, char letterGiven, int placeGiven);
        void readList(Node* &h);
        void deleteList(Node* &h);
        string getPath();
    private:
        const char SPACE = ' ';
        const int SCREEN_CHECK = 73;
        int charCount = 0;
    };


    int main()
    {
        string filename;
        char letter;
        int place;
        char goAgain = 'y';
       
        ifstream infile;
       
        secretMsgs sMsg;
       
        Node *head = new Node;
       
        while (goAgain=='y') {
            cout << "Enter filename (with path): ";
            cin >> filename;
            infile.open(filename);
            if (!infile.fail()) {
               
                while (infile.get(letter)) {
                    infile >> place;
                    infile.ignore();
                    sMsg.insertInOrder(head, letter, place);
                }
                infile.close();
                             
                cout << "Here's the decoded message: " << endl;
                sMsg.readList(head);
               
                sMsg.deleteList(head);
               
                cout << "Do it again on another file? (y/n)";
                cin >> goAgain;
            } else {
                cout << "Cound not read file.\n";
                infile.close();
            }
        }
       
        return 0;
       
    }

    // find the head, insert new data, replace the head
    void secretMsgs::addAtHead(Node* &h, char letterGiven, int placeGiven) {
      Node* ptr;
      if (h==nullptr) {
        h = new Node;
        h->place = placeGiven;
        h->letter = letterGiven;
        h->next = nullptr;
      } else {
        ptr = new Node;
        ptr->place = placeGiven;
        ptr->letter = letterGiven;
        ptr->next = h;
        h = ptr;
      }
    }

    void secretMsgs::deleteList(Node* &h) {
        Node* ptr;
        Node* deleteMe;
        ptr = h;
        while (ptr->next!=nullptr) {
            deleteMe = ptr;
            ptr = ptr->next;
            delete deleteMe;
        }
        delete ptr;
    }

    // crawls through list starting at head and inserts letter, place
    // in order (after next highest 'place')
    void secretMsgs::insertInOrder(Node* &h, char letterGiven, int placeGiven) {
        if (h==nullptr || h->place >= placeGiven) {
            addAtHead(h, letterGiven, placeGiven);
        } else {
            Node* ptr;
            Node* insert = new Node;
            insert->place = placeGiven;
            insert->letter = letterGiven;
            insert->next = nullptr;
            ptr = h;
            while (ptr->next!=nullptr && ptr->next->place < placeGiven) {
                ptr = ptr->next;
            }
            if (ptr->next!=nullptr) {
                insert->next = ptr->next;
            }
            ptr->next = insert;
        }
    }

    // print the entire chain, wrap @ 80 cols by replacing spaces with endl;
    void secretMsgs::readList(Node* &head) {
        charCount = 0;
        Node* ptr = new Node;
        ptr = head;
        while (ptr!=nullptr) {
            if (charCount>=SCREEN_CHECK && ptr->letter==SPACE) {
                cout << endl;
                ptr = ptr->next;
                charCount = 0;
            }
            cout << ptr->letter;
            ptr = ptr->next;
            charCount++;
        }
        cout << "\n";    
    }

     
    any advice appreciated, thank you..
     
  2. jcsd
  3. May 8, 2014 #2

    Borek

    User Avatar

    Staff: Mentor

    Have you heard about debuggers? Using a debugger to trace the code step by step will help you find the error. Or set the breakpoints in your code.

    Alternatively, you can go the old way - insert some cout << "I am here!" lines in your code to localize where the program gets stuck.

    These methods are universal and they will help you regardless of what the problem is. The sooner you master them, the better for you.

    Disclaimer: I didn't even tried to analyze your code. Debugger is a fishing rod, not a fish.
     
  4. May 8, 2014 #3
    yeah don't really have access to a debugger. I've tried using cout messages, that's how I knew it was getting stuck on that loop.
     
  5. May 8, 2014 #4

    Borek

    User Avatar

    Staff: Mentor

    You can make it print pointers as hex values, to see if they are changed as expected.
     
  6. May 8, 2014 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That is very dubious. What compiler / what operating system are you using? If you are using an IDE, the debugger is almost inevitably an integral part of the IDE. If you are using a command line compiler you almost inevitably have a debugger that goes along with that compiler. Debuggers are a programmers best friend.
     
  7. May 8, 2014 #6

    dwn

    User Avatar

    Try searching for Eclipse or Netbeans. I prefer Eclipse because it will help to import different utilities depending on what you are trying to do.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Pointers/Linked Lists in C++
Loading...