Why Does My C++ Linked List Program Freeze After One Use?

  • Context: Comp Sci 
  • Thread starter Thread starter vermin
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
vermin
Messages
22
Reaction score
0

Homework Statement



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".

Homework Equations



n/a

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:
#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..
 
on Phys.org
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 count << "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.
 
yeah don't really have access to a debugger. I've tried using count messages, that's how I knew it was getting stuck on that loop.
 
You can make it print pointers as hex values, to see if they are changed as expected.
 
vermin said:
yeah don't really have access to a debugger.
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.
 
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.