Question
This program is supposed to read in the a3.txt and read each instruction with the % sign in front of each instruction and execute it
This program is supposed to read in the a3.txt and read each instruction with the % sign in front of each instruction and execute it accordingly. The problem is that the program just crashes and doesn't output the right thing at all. I have what is supposed to be outputted at the bottom out based on the a3.txt but I'm just not getting it. I'm really struggling with this program. This program is supposed to be a linked list.
Here's the a3.txt:
%insert Noah 99 %insert Emma 100 %insert Liam 75 %insert Olivia 82 %insert Mason 87 %insert Sophia 95 %insert Jacob 79 %insert Ava 97 %insert William 89 %insert Isabella 77 %insert Ethan 91 %insert Mia 94 %insert James 88 %insert Abigail 98 %write %erase William %erase Isabella %erase Michael %write %search Noah %search Emma %search Michael %sort %write %end
Here's the main program that reads that file:
#include #include #include #include #include
#include "node.h"
#include "llist.h"
using namespace std;
int main() { // stream to input instructions and information ifstream instrFile;
// open file with list of people and their data instrFile.open("a3.txt"); if (!instrFile) { cout << "Cannot open file 'a3.txt'" << endl; }
string instruction, name, tempStr; int score; node *currNode;
// Set "curNode" value to NULL currNode = NULL; // Create Object for llist class in list.h header file llist obj; while(!instrFile.eof()) { getline(instrFile, instruction, ' '); if (instruction == "%insert") { getline(instrFile, name, ' '); getline(instrFile, tempStr, ' '); const char* temp = tempStr.c_str(); score = atoi(temp); cout << instruction << " " << name << " " << score << endl; //your code currNode = obj.insert(currNode, name, score); } else if (instruction == "%erase") { getline(instrFile, name, ' '); cout << instruction << " " << name << endl; //your code // Search for element in list, ''search" returns the element position then erase it currNode = obj.search(name); obj.erase(currNode); } else if (instruction == "%search") { getline(instrFile, name, ' '); cout << instruction << " " << name << endl; //your code // Search the list using the name given in the list if present do something else do another thing bool present = obj.search(name); // present is true if given name is presetn in list else false } else if (instruction == "%write") { instrFile.ignore(100, ' '); cout << instruction << endl; //your code // Simply call writeList, it will print all the elements in the list obj.writeList(); } else if (instruction == "%sort") { instrFile.ignore(100, ' '); cout << instruction << endl; //your code //call sortAssend to sort in ascending order. obj.sortAssend(); } else if (instruction == "%end") { break; } } int hold; cin >> hold; }
Here's the list.h header:
#ifndef LLIST_H_INCLUDED #define LLIST_H_INCLUDED
#include #include "node.h"
using namespace std;
class llist { public:
//default constructor llist();
//destructor ~llist();
//Inserts node at before node at pointer currNode node *insert(node *currNode, string name, int score);
//Uses insert function to add names and scores to the in alphabetical order void insertInOrder(string name, int score);
//Search for a name and print it out node *search(string name);
//Erase a name from the list void erase(node *currNode);
//Write out the current list void writeList();
//Write out a single node (name and score) void writeNode(node *currNode);
//Swap two nodes in the list used by sort void swapNode(node *nodeA, node *nodeB);
//Sort the by scores in ascending order void sortAssend();
node *header;
private:
int listSize;
};
llist::llist() { header = new node; header->prev = header; header->next = header; listSize = 0; }
llist::~llist() { node *currNode, *tempNode; currNode = header->next; while(currNode != header) { tempNode = currNode; currNode = currNode -> next; delete tempNode; } delete header; }
void llist::writeList() { cout << endl << "list" << endl; node *currNode; currNode = header->next; while(currNode != header) { cout << setw(10) << currNode->name << "\t" << currNode->score <
currNode = currNode->next;
} cout << endl; } node *llist::insert(node *currNode, string name, int score) { node *newNode, *prevNode; prevNode = currNode->prev;
newNode = new node; newNode->name = name; newNode->score = score; ++listSize;
newNode->prev = currNode->prev; newNode->next = prevNode->next;
prevNode->next = newNode; currNode->prev = newNode;
return newNode; }
void llist::insertInOrder(string name, int score) { node *currNode; currNode = header->next; while(currNode != header) { if(name < currNode->name) { insert(currNode, name, score); break; } currNode= currNode-> next; } if (currNode == header) { insert(currNode, name, score); } }
node *llist::search(string name) { node *currNode; currNode = header->next; while(currNode != header) { if(currNode->name == name) { return currNode; break; } currNode = currNode->next; } return NULL; }
void llist::erase(node *currNode) { node *prevNode, *succNode; prevNode = currNode->prev; succNode = currNode->next;
prevNode->next = succNode; succNode->prev = prevNode;
delete currNode; --listSize; }
void llist::writeNode(node *currNode) { cout << endl << "Node" << endl; if(currNode != NULL) { cout << setw(10) << currNode->name << "\t" << currNode->score << endl; } }
void llist::swapNode(node *nodeA, node *nodeB) { string tempName; int tempScore;
tempName = nodeA->name; tempScore = nodeA->score;
nodeA->name = nodeB->name; nodeA->score = nodeB->score;
nodeB->name = tempName; nodeB->score = tempScore; } void llist::sortAssend() { node *passNode, *smallNode, *jNode; int pass, j;
passNode= header->next; for(pass = 0; pass < listSize-1;pass++) { smallNode = passNode; jNode = passNode->next; for(j= pass+1;j { if(jNode->score < smallNode->score) { smallNode = jNode; } jNode = jNode->next; } if(smallNode != passNode) { swapNode(passNode,smallNode); //passNode = smallNode; FOR OTHER WAY OF SWAPPING } passNode= passNode->next; }
} #endif // LLIST_H_INCLUDED
Here's the simple node.h header:
#ifndef NODE_H_INCLUDED #define NODE_H_INCLUDED
#include
using namespace std;
struct node { string name; int score; node *prev; node *next; };
#endif // NODE_H_INCLUDED
SAMPLE OUTPUT BASED ON THE a3.txt: ( This is what comes out after the first %write)
Noah 99 Emma 100 Liam 75 Olivia 82 Mason 87 Sophia 95 Jacob 79 Ava 97 William 89 Isabella 77 Ethan 91 Mia 94 James 88 Abigail 98
After the second %write:
Noah 99 Emma 100 Liam 75 Olivia 82 Mason 87 Sophia 95 Jacob 79 Ava 97 Ethan 91 Mia 94 James 88 Abigail 98
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started