Question: Stuck on a program in C++ please help... I have finished the majority of a large program successfully, but one part is giving me a

Stuck on a program in C++ please help...

I have finished the majority of a large program successfully, but one part is giving me a really hard time....

The program is a binary search tree, and so far my program works as intended with the following inputs

Stuck on a program in C++ please help... I have finished the

and it works correctly with...

majority of a large program successfully, but one part is giving me

The output looks like...

a really hard time.... The program is a binary search tree, and

But when the first two or more characters are operators I get a segmentation fault. The following input causes a seg fault...

so far my program works as intended with the following inputs and

I just need to rewrite the code I have so that it can account for the different file input. There is a lot of code, but clearly the majority of the code works, so I will only include the portions of the code that reads in the file and sends the data on to the binary tree. I will include the main.cpp and the Executive.h and Executive.cpp. Main does nothing except check for the correct number of starting parameters and passes the name of the file to open to Executive. Executive then opens the file and reads the content into the tree, and afterwards displays the contents in terminal....

main.cpp

#include

#include "Executive.h"

#include "Executive.cpp"

int main(int argc, char* argv[]){

if(argc

std::cout

exit(0);

}

else{

string filename = "postfix2.txt";

Executive exec;

exec.run(filename);

}

return(0);

}

Executive.h

#ifndef EXECUTIVE_H

#define EXECUTIVE_H

#include

#include

#include

#include "BinaryNodeTree.h"

#include "LinkedStack.h"

using namespace std;

class Executive

{

public:

Executive();

~Executive();

void run(string filename);

private:

BinaryNodeTree* bt;

static void visit(string& myItem);

void printTreePreorder(BinaryNodeTree* bt);

void printTreeInorder(BinaryNodeTree* bt);

void printTreePostorder(BinaryNodeTree* bt);

};

#endif

Executive.cpp

#include "Executive.h" using namespace std; Executive::Executive(){ } void Executive::run(string filename){ ifstream file; file.open(filename); LinkedStack*> newStack; string operators = "* + - / ="; string temp; if(file){ while (file >> temp){ if((temp.length()== 1 || operators.find(temp)!= std::string::npos)){ try{ cout * rChild = newStack.peek(); newStack.pop(); BinaryNodeTree* lChild = newStack.peek(); newStack.pop(); BinaryNodeTree* updated = new BinaryNodeTree (temp, rChild, lChild); newStack.push(updated); delete rChild; delete lChild; } catch (PrecondViolatedExcep except){

std::cout * operands = new BinaryNodeTree(temp); newStack.push(operands); } } } else{ cout * bt){ cout preorderTraverse(visit); cout * bt){ cout inorderTraverse(visit); cout * bt){ cout postorderTraverse(visit); cout

The problem lies in the run function...

void Executive::run(string filename){

ifstream file; file.open(filename); LinkedStack*> newStack; string operators = "* + - / ="; string temp; if(file){ while (file >> temp){ if((temp.length()== 1 || operators.find(temp)!= std::string::npos)){ try{ cout * rChild = newStack.peek(); newStack.pop(); BinaryNodeTree* lChild = newStack.peek(); newStack.pop(); BinaryNodeTree* updated = new BinaryNodeTree (temp, rChild, lChild); newStack.push(updated); delete rChild; delete lChild; } catch (PrecondViolatedExcep except){

std::cout * operands = new BinaryNodeTree(temp); newStack.push(operands); } } } else{ cout

PLEASE HELP!!!! Will give a thumbs up and comments if the program works for both files!!!!!!

1 alpha vall beta *val2 gamma*+* 2 1 alpha vall beta *val2 gamma*+* 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!