#include "tree.h"
Tree :: Tree (){
root = NULL;
}
void Destroy( TreeNode*& tree ){
if( tree != NULL ){
Destroy (tree->left);
Destroy (tree->right);
delete tree;
}
}
Tree :: ~Tree(){
Destroy (root);
}
bool Tree :: Empty(){
if ( root == NULL )
returntrue;
elsereturnfalse;
}
void addNode( TreeNode*& tree , string title ){
if( tree == NULL ){
tree = new TreeNode;
tree->left = NULL;
tree->right = NULL;
tree->cdTitle = title;
}
elseif( title < tree->cdTitle )
addNode( tree->left , title );
else
addNode( tree->right , title );
}
void Tree :: Insert( string newTitle ){
addNode ( root , newTitle );
}
void Print( TreeNode*& tree ){
if( tree != NULL ){
Print( tree->left );
cout << tree->cdTitle;
Print( tree->right );
}
}
void Tree :: Display(){
Print ( root );
}
void readRecordTree( ifstream &inFile , Tree *&theTree ){
int numberofCD = 0;
string tempCDTitle = "";
inFile.open("CDRecord.txt");
if( inFile.fail() ){
cout << "Unable to open CD RECORD list ! " << end; << endl;
}
if( inFile.is_open() ){
inFile >> numberofCD;
for( int i = 0 ; i < numberofCD ; i++ ){
inFile >> tempCDTitle;
theTree->Insert( tempCDTitle );
}
}
}
I insert my node and inside my insert function already call the addNode function.
what is my problem currently? for my Tree to store the CD collection title?
so after the readRecord i can use
1 2
Tree BST;
BST.Display();
to show all my cd title value using class Treeright?
Yes theTree in your main is left uninitialized and hence will crash when you use it in readRecordTree()
Btw: Tree *& (as parameter for readRecordTree/Print) why do you use *&?
You do this only if you want to modify the pointer (like in addNode()). I'd suggest that you remove the & where the pointer isn't modified.
Oh and: BST won't contain any data since no readRecordTree() or any other function is applied previously to Display(). Why do you create a second tree anyway?
Okay. sorry and modified. minor mistake
but now got such error: Unhandled exception at 0x77b715de in Assignment 5.exe: 0xC0000005: Access violation reading location 0x00000000.
and my driver program point to here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void readRecordTree( ifstream &inFile , Tree *theTree ){
int numberofCD = 0;
string tempCDTitle = "";
inFile.open("CDRecord.txt");
if( inFile.fail() ){
cout << "Unable to open CD RECORD list ! " << endl << endl;
}
if( inFile.is_open() ){
inFile >> numberofCD;
for( int i = 0 ; i < numberofCD ; i++ ){
inFile >> tempCDTitle;
theTree->Insert( tempCDTitle );
} }
}
change all ampersand. got it what you mean
the last third bracket. where my program stop at
NULL is not a valid pointer! In opposite: it's the indicator that a pointer is invalid (no space for the object is allocated). You must not process theTree->Insert( tempCDTitle ); when theTree == NULL
Do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "tree.h"
#include "cd.h"
int main(){
ifstream inFile;
Tree *theTree;
string title="";
string customerName="";
RentalStore theRental;
/*theRental.addRecord(title , customerName);
theRental.deleteRecord( title,customerName);
theRental.showAllRecord();*/
Tree BST;
readRecordTree( inFile , &BSTtheTree );
Tree BST;
BST.Display();
system( "pause" );
return 0;
}
My question mentioned that All printing rental options should use the iterator to retrieve the record(s) from the list.
so i only create a container for title. in this case i have to create another container to store name or?
because for my option 4: 4. Print all rental information
I have to use iterator to print it and not the Print() ?
but why i cannot show any data for the CD for that looping code?
Because of this: void ReadTitle( RentalStore theRental , ifstream &inFile , int numberOfBooks ){
You need a reference & or pointer * to theRental in order to keep the modification outside the function.
Seems that you still have trouble to understand the use of references/pointer?
If ReadTitle() was a member function of RentalStore you could manipulate v directly.
void ReadTitle( RentalStore& theRental , ifstream &infile , int& numberOfBooks ){
string tempTitle = "";
infile.open( "CDRecord.txt" );
if( infile.fail() ){
cout << "Unable to open CD Record ! " << endl;
}
if( infile.is_open() ){
infile >> numberOfBooks;
infile.ignore();
for( int i = 0 ; i < numberOfBooks ; i++ ){
getline( infile , tempTitle );
theRental.v.push_back( tempTitle );
}
}
}
i should not use vector because i read the question it's want us to create own iterator.
so mind i ask u that can provide a simple code base on my code ?
that create a iterator that point to my RentalRecord beginning and end?
quite blur for the iterator..
1>c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(59): error C2084: function 'RentalStore::iterator RentalStore::begin(void)' already has a body
1> c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(51) : see previous definition of 'begin'
1>c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(63): error C2084: function 'RentalStore::iterator RentalStore::end(void)' already has a body
1> c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(53) : see previous definition of 'end'
1> cd.cpp
1>c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(59): error C2084: function 'RentalStore::iterator RentalStore::begin(void)' already has a body
1> c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(51) : see previous definition of 'begin'
1>c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(63): error C2084: function 'RentalStore::iterator RentalStore::end(void)' already has a body
1> c:\users\user\desktop\inti\applied\assignment 5\assignment 5\cd.h(53) : see previous definition of 'end'