- #1
vermin
- 22
- 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..