pointer for my addrecord

Pages: 12
i have sent a message to your inbox that for my full code. mind to have a check? reply there or reply here.. Thanks
Tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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 )
		return true;
	else
		return false;
}

void addNode( TreeNode*& tree , string title ){
	if( tree == NULL ){
		tree = new TreeNode;
		tree->left = NULL;
		tree->right = NULL;
		tree->cdTitle = title;
	}
	else if( 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?
what is my problem currently?
I'd prefer if you'd tell me instead of letting me guess...

to show all my cd title value using class Treeright?
Yes, if you add items you should be able to display them
i got this error "
Unhandled exception at 0x77b715de in Assignment 5.exe: 0xC0000005: Access violation reading location 0xcccccccc.


and this is my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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();*/
	readRecordTree( inFile , theTree );
	Tree BST;
	BST.Display();
	system( "pause" );
	return 0;
}
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?
Last edited on
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
Last edited on
No, you don't get what I mean.

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;
}
Last edited on
Instead of using Print() and showAllRecord()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Print(RentalRecord *&theRecord){

	if( theRecord != NULL ){
		cout << "\tRental Customer List\n---------------------------- " << endl;
		Print( theRecord->next );
		cout << "\nCustomer Name : " << theRecord->customerName << endl;
		cout << "CD Title is : " << theRecord->RecordTitle << endl;
	}
	else
		cout << "Empty List ! " << endl;
}

void RentalStore :: showAllRecord(){
	Print ( theRental );
}


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() ?
Last edited on
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.
see the above thread i edit ( question over there ) . is minor mistake.
i just miss
 
inFile >> numberOfBooks;

I forget to edit thread with ampersand only. thanks
besides that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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..

for my concept
1
2
iterator_begin = RentalRecord.begin();
iterator_end   = RentalRecord.end();


so that when i retrieve the record just use a looping on it
Last edited on
an iterator according to the stl paradigm would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class RentalStore{
...
	class iterator
	{
		RentalRecord *m_r;
	public:
		iterator(RentalRecord *r=NULL) :
			m_r(r)
		{
		}

		void operator++() // This doesn't return anything, so that you can't be use it in larger expressions
		{
			if(m_r)
				m_r = m_r->next;
		}
	};
...
}

...

RentalStore::iterator RentalStore::begin()
{
  return iterator(theRental);
}

RentalStore::iterator RentalStore::end()
{
  return iterator();
}


learn more about operators:

http://www.learncpp.com/cpp-tutorial/97-overloading-the-increment-and-decrement-operators/

read more about iterators:

http://www.oreillynet.com/network/2005/10/18/what-is-iterator-in-c-plus-plus.html
1
2
3
4
5
6
7
8
9
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'


having the problem at
1
2
3
4
5
6
7
RentalStore::iterator RentalStore::begin(){
  return iterator(theRental);
}

RentalStore::iterator RentalStore::end(){
  return iterator();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class iterator{
		RentalRecord *endBook;
	public:

		iterator(RentalRecord *r=NULL) : endBook(r){ };

		void operator++(){ // This doesn't return anything
			if( endBook )
				endBook = endBook->next;
		}

		RentalRecord *operator->(){ return endBook; }

		// To see if you're at the end: 			
		bool operator==(const iterator& rv) const { 
			return endBook == rv.endBook; } 
		
		bool operator!=(const iterator& rv) const { 
			return endBook != rv.endBook; } 
	};
	// begin of the stack
	iterator begin() { return iterator(); } 
	// end of the stack
	iterator end() { return iterator();} 

};
Ok, I see. Simply remove those:
1
2
3
4
5
6
7
RentalStore::iterator RentalStore::begin(){
  return iterator(theRental);
}

RentalStore::iterator RentalStore::end(){
  return iterator();
}


Change that:
1
2
3
4
	// begin of the stack
	iterator begin() { return iterator(theRental); } 
	// end of the stack
	iterator end() { return iterator();} 
Topic archived. No new replies allowed.
Pages: 12