Segmentation Fault 11 on return - c++

I realize there are other answers like this one but seg faults are pretty general and none of them fit my situation exactly. I am creating a binary tree and storing data. In the printInOrder function, it recurses all the way to the furthest leftChild and then seg faults when it reaches the return statement. I am not sure why it is doing this. Thanks in advance for the help.
#include <string>
#include <cstdlib>
#include <iostream>
#include "BST.h"
using namespace std;
BST::BST(void) {
root = NULL;
cout << "Default object is being creating." << "\n";
};
/*
BST::BST(string word, int count) {
cout << "Default object is being creating." << "\n";
this->root->word = word;
this->root->count = count;
};
BST::BST(string word) {
cout << "Default object is being creating." << "\n";
this->root->word = word;
this->root->count = 1;
};
*/
bool BST::hasWord(string word) {
return true;
};
void BST::printInOrder() {
pio(this->root);
}
void BST::pio(struct node* node) {
cout << "root: " << root << endl;
cout << "node: " << node << endl;
cout << "leftChild: " << node->leftChild->leftChild << endl;
if (node != NULL){
/* first recur on left child */
pio(node->leftChild);
/* then print the data of node */
cout << "Word: " << node->word << endl;
cout << "Count: " << node->count << endl;
/* now recur on right child */
pio(node->rightChild);
}
if (node == NULL) {
return;
}
}
void BST::insert(string word)
{
if(this->root != NULL)
insert(word, this->root);
else
{
cout << "obj has been inserted " << endl;
this->root = new node;
cout << root << endl;
this->root->word = word;
this->root->count = 1;
this->root->leftChild = NULL;
this->root->rightChild = NULL;
}
}
void BST::insert(string word, struct node *leaf)
{
if(word< leaf->word)
{
if(leaf->leftChild != NULL)
insert(word, leaf->leftChild);
else
{
cout << "obj has been inserted " << endl;
leaf->leftChild = new node;
cout << leaf->leftChild << endl;
leaf->leftChild->word = word;
leaf->leftChild->count = 1;
leaf->leftChild->leftChild = NULL; //Sets the leftChild child of the child node to null
leaf->leftChild->rightChild = NULL; //Sets the rightChild child of the child node to null
}
}
else if(word>leaf->word)
{
if(leaf->rightChild != NULL)
insert(word, leaf->rightChild);
else
{
cout << "obj has been inserted " << endl;
leaf->rightChild = new node;
cout << leaf->rightChild << endl;
leaf->rightChild->word = word;
leaf->rightChild->count = 1;
leaf->rightChild->leftChild = NULL; //Sets the leftChild child of the child node to null
leaf->rightChild->rightChild = NULL; //Sets the rightChild child of the child node to null
}
}
//word is already in the tree so we increment counter
else leaf->count += 1;
}
Main file:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <functional>
#include <algorithm>
#include <sstream>
#include "BST.h"
using namespace std;
void parseFile(string fullPath, BST bst) {
ifstream infile(fullPath); // Open it up!
std::string line;
char c;
string word = "";
while (std::getline(infile, line))
{
// Iterate through the string one letter at a time.
for (int i = 0; i < line.length(); i++) {
c = line.at(i); // Get a char from string
tolower(c);
// if it's NOT within these bounds, then it's not a character
if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {
//if word is NOT an empty string, insert word into bst
if ( word != "" ) {
bst.insert(word);
//reset word string
word = "";
}
}
else {
word += string(1, c);
}
}
}
}
void getReqDirs(BST bst, const string path, vector<string> files,const bool showHiddenDirs = false){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(path.c_str());
int count = 0;
if (dpdf != NULL){
while ((epdf = readdir(dpdf)) != NULL){
if(showHiddenDirs ? (epdf->d_type==DT_DIR && string(epdf->d_name) != ".." && string(epdf->d_name) != "." ) : (epdf->d_type==DT_DIR && strstr(epdf->d_name,"..") == NULL && strstr(epdf->d_name,".") == NULL ) ){
getReqDirs(bst,path+epdf->d_name+"/",files, showHiddenDirs);
}
if(epdf->d_type==DT_REG){
//files.push_back(path+epdf->d_name);
//parseFile(path+epdf->d_name, bst);
cout << path+epdf->d_name << " ";
}
}
}
closedir(dpdf);
}
int main()
{
vector <string> words; // Vector to hold our words we read in.
string str; // Temp string to
cout << "Read from a file!" << endl;
BST bst;
string path = "test/"; //name of directory that holds the data base
vector<string> files; //create vector for file names
/*
//collect all the file names in the data base
getReqDirs(bst,path,files,false);
for (int i = 0; i < files.size(); i++) {
cout << files[i] << " ";
}*/
bst.insert("gary");
bst.insert("Mil");
bst.insert("Taylor");
bst.insert("BrIaN");
bst.insert("fart");
bst.insert("Juan");
bst.insert("James");
bst.insert("Gary");
bst.printInOrder();
//parseFile(path, bst);
return 0;
}
Please let me know if any other info is required.

The following line is the only problematic line of code I found in pio:
cout << "leftChild: " << node->leftChild->leftChild << endl;
It is a problem since node is bound be NULL at some point.
Remove that line.
Also, you don't need the lines
if (node == NULL) {
return;
}
They don't do anything useful and can be removed. Here's an updated version of the function:
void BST::pio(struct node* node) {
cout << "root: " << root << endl;
cout << "node: " << node << endl;
if (node != NULL){
/* first recur on left child */
pio(node->leftChild);
/* then print the data of node */
cout << "Word: " << node->word << endl;
cout << "Count: " << node->count << endl;
/* now recur on right child */
pio(node->rightChild);
}
}

Related

AVLTree can't find data when searched for

I am working on an assignment for school. I have to create an AVLTree, read data from a text file and then create Professor object with that data. However, when I try and search for a Professor by name, it can't be found. It keeps defaulting to Michael Scott in the program and can't find who I'm looking for. My readFromFile function works perfectly and gets me all the data from the file, so I have the data. At this point it's a matter of issues with the AVL Tree. I'll link everything below.
Driver.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "AVLTree.h"
using namespace std;
void readFromFile(AVLTree *treeAccess) {
int numOfProfs = 0;
string line;
const char *path="/Users/collinchappell/Desktop/Program4Official/Program4Official/ProfessorData.txt";
fstream myFile (path, ios::in);
if (!myFile) {
cout << "Sorry can't read ProfessorData.txt!" << endl;
}
else {
// Splitting up all the data in the .txt file and the looping through using string stream
// Not the best solution, however nothing else would work for me so this is what I went with.
while (getline(myFile, line)) {
string n;
string nT;
string s;
int b1,b2,b3,b4,b5,b6,b7;
replace(line.begin(), line.end(), '$', ' ');
stringstream ss(line);
for (int k = 0; k < 11; k++) {
ss >> n;
ss >> nT;
n = n + " " + nT;
ss >> s;
ss >> b1;
ss >> b2;
ss >> b3;
ss >> b4;
ss >> b5;
ss >> b6;
ss >> b7;
numOfProfs++;
Professor p(n, s, b1, b2, b3, b4, b5, b6, b7);
cout << p;
treeAccess->insertNode(n, &p);
}
}
}
cout << numOfProfs << " Proffesors have added from the file." << endl << endl;
}
int main() {
AVLTree tree;
readFromFile(&tree);
bool cont = true;
while (cont) {
cout << "Which Computer Science professor do you want details about?" << endl;
tree.displayInOrder();
cout << endl << endl << "Type Professor Name: ";
string typeName;
getline(cin, typeName);
while (tree.searchNode(typeName) == NULL) {
cout << "Couldn't find that professor! Please type another: ";
getline(cin, typeName);
}
cout << tree.searchNode(typeName);
cout << "Would you like to search another? (Y/N): ";
string ans;
cin >> ans;
if (ans == "y" || ans == "Y") {
continue;
}
else if (ans == "n" || ans == "N") {
return 0;
}
}
return 0;
}
AVLTree.h:
#ifndef AVLTree_h
#define AVLTree_h
#include <iostream>
#include "Professor.h"
using namespace std;
class AVLTree {
private:
// TreeNode structure
struct TreeNode {
string name;
Professor *prof;
struct TreeNode* left;
struct TreeNode* right;
};
// Root of tree
TreeNode* root;
// Function declarations
void insert(TreeNode *&nodePtr, TreeNode *&newNode);
void destroySubTree(TreeNode *nodePtr);
void displayInOrder(TreeNode *nodePtr) const;
int height(TreeNode *nodePtr);
int diff(TreeNode *nodePtr);
void balance(TreeNode *&temp);
TreeNode* l_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
TreeNode* r_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
TreeNode* lr_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = (l_rotation(temp));
return r_rotation(parent);
}
TreeNode* rl_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = (r_rotation(temp));
return l_rotation(parent);
}
public:
AVLTree() { // Constructor
root = NULL;
}
~AVLTree() { // Destructor
destroySubTree(root);
}
void insertNode(string n, Professor* professor);
Professor* searchNode(string);
// Call private displayInOrder()
void displayInOrder() const {
displayInOrder(root);
}
};
#endif /* AVLTree_h */
AVLTree.cpp:
#include <iostream>
#include "AVLTree.h"
using namespace std;
// Insert a node into the tree recursively
void AVLTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) {
if (nodePtr == NULL) {
nodePtr = newNode;
}
else if (newNode->name < nodePtr->name) {
insert(nodePtr->left, newNode);
balance(nodePtr);
}
else {
insert(nodePtr->right, newNode);
balance(nodePtr);
}
}
// Destory the sub tree
void AVLTree::destroySubTree(TreeNode *nodePtr) {
if (nodePtr) {
if (nodePtr->left) {
destroySubTree(nodePtr->left);
}
if (nodePtr->right) {
destroySubTree(nodePtr->right);
}
delete nodePtr;
}
}
// Display the tree in order
void AVLTree::displayInOrder(TreeNode *nodePtr) const {
if (nodePtr) {
displayInOrder(nodePtr->left);
cout << nodePtr->name << endl;
displayInOrder(nodePtr->right);
}
}
// Get the tree's height
int AVLTree::height(TreeNode *temp) {
int leftHeight, rightHeight;
if (temp) {
leftHeight = height(temp->left);
rightHeight = height(temp->right);
if (leftHeight > rightHeight) {
return leftHeight + 1;
}
else if (leftHeight < rightHeight) {
return rightHeight + 1;
}
else {
return 0;
}
}
return 0;
}
// Get the difference
int AVLTree::diff(TreeNode *temp) {
int l_height = height(temp->left);
int r_height = height(temp->right);
return l_height - r_height;
}
// Balance the tree
void AVLTree::balance(TreeNode *&temp) {
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff(temp->left) > 0) // 2, 1 RIGHT
{
temp = r_rotation(temp);
cout << "\nRIGHT rotation";
}
else // 2, -1 LEFT-RIGHT
{
temp = lr_rotation(temp);
cout << "\nLEFT-RIGHT rotation";
}
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0) // -2, 1 RIGHT-LEFT
{
temp = rl_rotation(temp);
cout << "\nRIGHT-LEFT rotation";
}
else // -2, -1 LEFT
{
temp = l_rotation(temp);
cout << "\nLEFT Rotation";
}
}
}
// Insert a node, calls the private insert function
void AVLTree::insertNode(string n, Professor *proffesor) {
TreeNode *newNode = NULL; // Pointer to a new node.
newNode = new TreeNode;
newNode->name = n;
newNode->prof = proffesor;
newNode->left = newNode->right = NULL;
// Insert the node.
insert(root, newNode);
}
Professor* AVLTree::searchNode(string s) {
cout << "Name inputted into search: " << s << endl;
cout << endl << "Actual name: " << root->name << endl;
TreeNode *nodePtr = root;
while (nodePtr != NULL) {
if (nodePtr->name == s) {
return nodePtr->prof;
}
else if (nodePtr->name > s) {
nodePtr = nodePtr->left;
cout << "Left -> " << nodePtr << endl;
}
else {
nodePtr = nodePtr->right;
cout << "Right -> " << nodePtr << endl;
}
}
return NULL;
}
Professor.h:
#ifndef PROFESSOR_H
#define PROFESSOR_H
#include <iostream>
#include <iomanip>
using namespace std;
class Professor
{
private:
string name;
string course;
bool clearGrading;
bool goodFeedback;
bool caring;
bool reachable;
bool toughGrader;
bool lectureHeavy;
bool attendance;
public:
Professor(string n, string course, bool cG, bool gF, bool c, bool r, bool tG, bool lH, bool a) {
this->name = n;
this->course = course;
this->clearGrading = cG;
this->goodFeedback = gF;
this->caring = c;
this->reachable = r;
this->toughGrader = tG;
this->lectureHeavy = lH;
this->attendance = a;
}
friend ostream &operator << (ostream &strm, Professor &p)
{
strm << endl << endl;
strm << setw(30) << "Professor:" << setw(20) << p.name << endl;
strm << setw(30) << "Course:" << setw(20) << p.course << endl;
strm << setw(30) << "Clear Grading Criteria:" << setw(20);
if(p.clearGrading == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Provides Good Feedback:" << setw(20);
if(p.goodFeedback == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Caring:" << setw(20);
if(p.caring == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Reachable Outside of Class:" << setw(20);
if(p.reachable == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Tough Grader:" << setw(20);
if(p.toughGrader == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Lecture Heavy:" << setw(20);
if(p.lectureHeavy == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Attendance Mandatory:" << setw(20);
if(p.attendance == 0) strm << "no"; else strm << "yes";
strm << endl;
return strm;
}
};
#endif
ProfessorData.txt:
Michael Scott$CSC1310$0$0$1$1$0$1$1$Jim Halpert$CSC1310$1$1$1$0$0$0$0$Pam Beesly$CSC1300$1$1$1$0$1$0$1$Dwight Schrute$CSC2400$1$1$0$0$1$1$1$Angela Martin$CSC1300$1$0$0$0$1$1$1$Kelly Kapoor$CSC1300$0$0$0$0$0$0$0$Andy Bernard$CSC2400$1$0$1$1$0$1$1$Kevin Malone$CSC1310$0$1$1$1$0$0$0$Meredith Palmer$CSC1310$0$1$1$1$0$0$0$Phyllis Vance$CSC1300$1$1$1$0$1$0$0$Oscar Nunez$2400$1$1$1$1$1$1$1$
Running the program:
11 Proffesors have added from the file.
Which Computer Science professor do you want details about?
Andy Bernard
Angela Martin
Dwight Schrute
Jim Halpert
Kelly Kapoor
Kevin Malone
Meredith Palmer
Michael Scott
Oscar Nunez
Pam Beesly
Phyllis Vance
Type Professor Name: Andy Bernard
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
0x7ffeefbfee18Would you like to search another? (Y/N):
As you can see, I've put in some prints to kind of debug what's happening. I believe my objects are not getting inserted into the tree right, or my search function is off. Also, I'm not 100% on the balance function. Any help would be amazing.

Getting Misaligned access runtime error in c++ for a vector of pointers

Trie()
{
nxt.assign(26, NULL);
cout << nxt.size() << endl;
Trie* root = this;
isWord = false;
}
/** Inserts a word into the trie. */
void insert(string word)
{
auto node = root;
for (int i = 0; i < word.size(); i++)
{
cout << "node: " << node<< ":" << "word[i] -'a': " << word[i] - 'a' << endl;
if (node->nxt[word[i] - 'a'] == NULL ) // misaligned memory access here
node->nxt[word[i]-'a'] = new Trie();
node = node->nxt[word[i]-'a'];
}
node->isWord = true;
}
I am getting the following error although the vector seems to have correct length: Member access within misaligned address 0xbebebebebebebe for type 'Trie' which requires 8 byte alignment. Why is the nxt vector elements not accessible?
Update:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Trie {
public:
Trie() {
nxt.assign(26, NULL);
isWord = false;
}
void insert(string word) {
auto node = this;
for(int i = 0; i < word.size(); i++) {
if (!node->nxt[word[i]-'a']) {
cout << " nxt entry not found... :" << word[i] << endl;
node->nxt[word[i]-'a'] = new Trie();
}
else {
cout << "nxt entry found! :" << word[i] << endl;
node = node->nxt[word[i]-'a'];
}
node->isWord = true;
}
}
bool search(string word) {
auto node = this;
for(int i = 0; i < word.size(); i++) {
if (!node->nxt[word[i]-'a']) return false;
node = node->nxt[word[i]-'a'];
}
return node->isWord ? true: false;
}
private:
vector<Trie*> nxt;
bool isWord;
};
int main() {
Trie* trie = new Trie();
trie->insert("test");
cout << "found test: " << trie->search("test") << endl;
cout << "success!" << endl;
return 0;
}
This runs! Bailey's comment helped me realize my mistake. I had declared a local root in ctor. The intent was to only initialize the class member, root! Thank you!

Using a print function to output to a new file

I have a program analyzing the number of distinct and total words in a text file and then writing it to a new output file. I've got the first part down, but I don't know how to get my print function to print onto the new text file. Printing the total words or distinct words onto the new file works but the Print2() function doesn't seem to work. Please take a look at the //POINT OF INTEREST// section, where i believe the problem stems.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
int distinctWord = 0;
using namespace std;
typedef int ItemType;
struct Node {
string word;
int count = 1;
Node* left;
Node* right;
};
class TreeType {
private:
Node* root;
void Insert(Node*& root, string word);
void Destroy(Node*& root);
void PrintTree(Node* root);
void Print2File(Node* root, ofstream& fout);
public:
TreeType() {root = nullptr;}
~TreeType();
void PutItem(string word);
void Print();
void Print2(ofstream& fout);
};
void TreeType::Print() {
PrintTree(root);
cout << endl;
}
void TreeType::PrintTree(Node* root) {
if (root == nullptr)
return;
PrintTree(root->left);
cout << root->word << " " << root->count << endl;
PrintTree(root->right);
}
///////////////POINT OF INTEREST///////////////////////////
///////////////POINT OF INTEREST///////////////////////////
void TreeType::Print2File(Node* root, ofstream& fout){
if (root == nullptr)
return;
PrintTree(root->left);
fout << root->word << " " << root->count << endl;
PrintTree(root->right);
}
void TreeType::Print2(ofstream& fout) {
Print2File(root, fout);
cout << "Printed to another file" << endl;
}
///////////////POINT OF INTEREST///////////////////////////
///////////////POINT OF INTEREST///////////////////////////
void TreeType::PutItem(string word) {
Insert(root, word);
}
void TreeType::Insert(Node*& root, string word) {
if (root == nullptr) {
root = new Node;
distinctWord++;
root->word = word;
root->right = nullptr;
root->left = nullptr;
return;
}
if(root->word == word){
root->count++;
return;
}else if (word < root->word)
Insert(root->left, word);
else
Insert(root->right, word);
}
TreeType::~TreeType() {
Destroy(root);
}
int main(int argc, const char * argv[]) {
int totalwords = 0;
ifstream file;
string word;
char c;
ofstream fout;
ifstream file;
string filename;
cout << "Enter name of file with text to analyze: ";
cin >> filename;
fin.open(filename.c_str());
if (fin.fail()) {
cout << "Error opening file.\n";
exit(1);
}
cout << "\nAnalyzing " << filename << ".\n";
TreeType t;
while(!file.eof()){
file.get(c);
if(isalpha(c) || c == '\''){
word += c;
c = '\0';
}else if(c == ' ' || c == '\n' || c == '-' || c == '.'){
if(isalpha(word[0])){
transform(word.begin(), word.end(), word.begin(), ::tolower);
t.PutItem(word);
totalwords++;
word = "";
c = '\0';
}
}
}if(isalpha(word[0])){
transform(word.begin(), word.end(), word.begin(), ::tolower);
t.PutItem(word);
totalwords++;
}
file.close();
fout.open("results.txt");
cout << "\nWord counts:\n\n";
t.Print();
t.Print2(fout);
cout << "\nTotal number of words in text: " << totalwords << ".\n";
fout << "\nTotal number of words in text: " << totalwords << ".\n";
cout << "Number of distinct words appearing in text: "
<< distinctWord << ".\n";
fout << "Number of distinct words appearing in text: "
<< distinctWord << ".\n";
fout.close();
return 0;
}
You will have to pass your output file stream to your
PrintTree(root); too.
Right now you are printing to cout which will dump everything to whatever console your application is associated with.
The modified function after adding the ofstream parameter becomes:
void TreeType::PrintTree(Node* root, ofstream &fout) {
if (root == nullptr)
return;
PrintTree(root->left, fout);
fout << root->word << " " << root->count << endl;
PrintTree(root->right, fout);
}
You will need to pass the ofstream object throughout in all callers of PrintTree

Largest element of list

I need to find the largest element in the list. In the following code unsubscribed items and ordered them. How to find the last element of list? I think that I need add one more function void maksimum(), but I'm having trouble with that.
#include <iostream>
#include <string>
#include <time.h>
#include <conio.h>
#include <cstdlib>
using namespace std;
struct element
{
int number;
element* next;
element();
};
element::element()
{
next = NULL;
}
struct list
{
element* first;
void fill_list(int number);
void segregate();
void show_list();
void maksimum();
list();
};
list::list()
{
first = NULL;
}
void list::fill_list(int number)
{
element *nowy = new element;
nowy->number = number;
if(first == 0)
{
first = nowy;
}
else
{
element* temp = first;
while(temp->next)
{
temp = temp->next;
}
temp->next = nowy;
}
}
void list::show_list()
{
element* temp = first;
if(temp == 0)
{
cout << "List is empty." << endl;
cout << "No smallest element " << endl;
cout << "No largest element" << endl;
}
else
{
while(temp)
{
cout << temp->number << endl;
temp = temp->next;
}
cout << "the smallest element: : " << first->number << endl;
if(first->next == 0)
{
cout << "Largest element = Smallest element :)" << endl;
}
}
}
void list::segregate()
{
element* new_first = NULL;
element* prv;
element* temp;
element* maks;
while(first)
{
maks = first;
prv = NULL;
temp = first;
while(temp->next)
{
if(temp->next->number > maks->number)
{
prv = temp;
maks = temp->next;
}
temp=temp->next;
}
if (prv)
{
prv->next = maks->next;
}
else
{
first = maks->next;
}
maks->next = new_first;
new_first = maks;
}
first = new_first;
}
int main()
{
int n=0;
int number=0;
list* base = new list;
cout << "Size of list: " << endl;
cin >> n;
for(int i = 0; i < n; i++)
{
cout << "No " << i+1 << ": ";
cin >> number;
base->fill_list(number);
}
base->segregate();
base->show_list();
//base->maksimum();
delete(base);
return 0;
}
How can I do that?
ok. you're right, but I thought that my code shows my work. not matter :)
i solved my problem. My function: ^^
void list::show_list()
{
element * temp = first;
if( temp == 0 )
{
cout << "List is empty." << endl;
cout << "No smallest element " << endl;
cout << "No largest element" << endl;
}
else
{
while( temp->next != 0 )
{
temp = temp->next;
}
cout << "The largest element: " << temp->number << endl;
cout << "The smallest element: " << first->number << endl;
}
}

<<operator is only printing an empty line

In main the list prints out using the insertion operator, but all that I get is an empty line. Not sure why. Is it that there is nothing being stored in the list even when you use the set_coefficient? Any other critiquing is welcome. Thanks
here's the driver:
#include <iostream>
#include "polynomial.h"
using namespace std;
int main(){
Polynomial* poly = new Polynomial();
poly->set_coefficient(3,2);
poly->set_coefficient(0,2);
poly->set_coefficient(3,1);
cout << "trying to print data" << endl;
cout << *poly << endl;
return 0;
}
Here's the header:
#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
#include <iostream>
class Polynomial {
public:
struct PolyNode {
int coefficient, degree;
struct PolyNode* next;
PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};
PolyNode* firstTerm;
Polynomial(): firstTerm(0) {}
struct PolyNode* get_first(){
return firstTerm;
}
//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);
~Polynomial();
friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);
};
#endif
Here's the implementation:
#include "polynomial.h"
#include <iostream>
#include <ostream>
using namespace std;
void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)
firstTerm = new PolyNode(c,d,NULL);
else{
cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
cout << "Inside set_coefficient() while loop" << endl;
start = start->next;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
}
cout << "Leaving set_coefficient()" << endl;
}
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;
for(unsigned int i = 0; start->next != 0; i++){
o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
start = start->next;
}
return o;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
if (c != 0 && start == 0) // this situation may occur ? you have already judged surrounding if
else if (c == 0) {
cout << "deleting a term" << endl;
delete start; // that may be cause linkedList detach.
}
cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
cout << "Inside set_coefficient() while loop" << endl;
start = start->next;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
It appears you want a linked list, but you're neither creating nor removing links (start->next), only allocating memory. Without setting up the links, there is no list.
There are probably multiple bugs here :>
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;
for(unsigned int i = 0; start->next != 0; i++){
this won't print the last PolyNode...