pointer for my addrecord

Pages: 12
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
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

struct RentalRecord{
	string customerName;
	string RecordTitle;
	RentalRecord *next;
}

struct TreeNode{
	string cdTitle;
	TreeNode *left;
	TreeNode *right;
}

class RentalStore{
	TreeNode *node;
	RentalRecord *theRental;

public:
	RentalStore();
	~RentalStore();
	void addRecord( string );
	void RetrievalRecord();
	void deleteRecord( string );
	bool isEmpty();
	void clear();
};

class Tree{
private:
	TreeNode *node;
public:
	Tree();
	~Tree();

	void addNode();
	void checkTreeRecord();
	void writeTreeRecord();

};


1
2
3
4
5
6
7
8
9
10
11
12
13
#include "cd.h"

void RentalStore ::addRecord(string title){

	RentalRecord *newRental;
	newRental = new RentalRecord;

	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl
		<< "Enter title : ";
	getline( cin, title );
	//newRental->link = current->link;
}

how should i Allow new records to be added to the end of the list.? did i implement correctly? if wrong can someone help me to correct it in order to work for add record to the rental record please?
Last edited on
The first question would be: why would you pass title to addRecord() when you overwrite it on line 11?

In your constructor theRental must be set to NULL. That's important! Otherwise it does not work. The same applies to next, left, and right.

Adding newRental to the end of the (singly linked) list is not that hard (Pseudo code):
1
2
3
4
5
6
7
8
9
if theRental != NULL
{
  temp = theRental
  while temp->next
    { temp = temp->next }
  temp->next = newRental
}
else
  theRental = newRental
That's it

So next question: Why do you name your class Tree? It's not a tree it's a doubly linked list.
like what you said . because got senior told me to pass with string title?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void RentalStore :: addRecord(string &title){
	RentalRecord *temp;
	RentalRecord *newRental;
	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl
		<< "Enter title : ";
	getline( cin, title );
	if ( theRental != NULL ){
		temp = theRental;
		while( temp->next ){ 
			temp = temp->next;
		}
		temp->next = newRental;
	}
	else
		theRental = newRental;
}


if not what should i getline my input ? and it's right for my implementation right?
This string &title makes a little more sense. Well, stick to what works until you have a better grasp of what's going on.

line 3: RentalRecord *newRental = new RentalRecord;
remove line 2
line 9: RentalRecord *temp = theRental;

Again: it's absolutly vital to initialize pointer (with something valid or NULL). Otherwise your programm will crash and you might not even know why and where
RentalRecord *newRental = new RentalRecord; have no default constructor.
so i add on a
1
2
3
4
5
6
7
struct RentalRecord{
		RentalRecord();
		RentalRecord( string title, string name = "" ) : CDTitle( title ), customerName( name ), next( 0 ) {} 
		string customerName;
		string CDTitle;
		RentalRecord *next;
	}*list;


and thanks for your remind
beside that, for my title value isn't should point to where? actually having problem at here ...
It's not necessary that RentalRecord has a default constructor.

Place this RentalRecord *newRental = new RentalRecord(title) right after the getline() (and remove the original RentalRecord *newRental = new RentalRecord; line)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void RentalStore :: addRecord(string &title){
	RentalRecord *temp = theRental;
	
	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl
		<< "Enter title : ";
	getline( cin, title );
	RentalRecord *newRental = new RentalRecord( title );
	if ( theRental != NULL ){
		temp = theRental;
		while( temp->next ){ 
			temp = temp->next;
		}
		temp->next = newRental;
	}
	else
		theRental = newRental;
}


isn't like this?
Yes that should work. test it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void RentalStore :: addRecord(string &title){
	RentalRecord *temp = theRental;
	
	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl
		<< "Enter title : ";
	getline( cin, title );
	RentalRecord *newRental = new RentalRecord( title );
	if ( theRental != NULL ){
		RentalRecord *temp = theRental; // it's a good practice to define the variables where they're needed
		while( temp->next ){ 
			temp = temp->next;
		}
		temp->next = newRental;
	}
	else
		theRental = newRental;
}
ya . it's work.

how to do a checking for the list * ? because if it's looping it very easy to compare with the array only
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
void RentalStore ::deleteRecord(string &deleteTheRecord){

	int choice = 0;
	RentalRecord *deleteRental = NULL;

	cout << "Enter title : ";
	getline( cin , deleteTheRecord );


	//After find the record only out the menu
	cout << "\n1.Return CD " << endl
		<< "2.Cancel " << endl << endl
		<< "Enter Choice : ";
	cin >> choice;

	do{
		switch( choice ){
		case 1:
			deleteRecord = current->link;
			current->link = deleteRecord->link;
			delete deleteRecord;
			break;
		case 2:
			cout << endl;
			break;
		default:
			cout << "Invalid Input ! Please re-enter ! " << endl;
			break;
		}
	}while( choice !=1 || choice != 2 );

}


i do until here and i having some problem , the other variable which are not at above is i did it with last time implement and it's wrong totally
So first of all: did you understand the while loop in addRecord()? It is really important that you do. You need a loop similar to this each time you want to do something with a record.

link does not exists. use next

with while loop (similar to that) mentioned above you need to find the record which has to be deleted. In order to find that particular record you have to compare deleteRecord with probably RecordTitle within that while loop. If you found the record you may unlink it

deleteRecord is the string to find. deleteRental is the object to remove

Just give it a shot. And I tell you what you did wrong ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void RentalStore ::deleteRecord(string &deleteTheRecord){

	int choice = 0;
	RentalRecord *deleteRental = NULL;

	cout << "Enter title : ";
	getline( cin , deleteTheRecord );

	if( theRental != NULL ){
		RentalRecord *temp = theRental;
		while( deleteTheRecord == theRental->CDTitle )
			temp = temp->next;
		temp->next = deleteRental;
	}


isn't like this?
but if for first i want to cout the CD title which are same title first , then only let user to enter choice whether edit or not edit . isn't the switch case should inside the
while( deleteTheRecord == theRental->CDTitle )?
isn't like this?
Well, looking at that garbage code... It contains at least a very faint idea...

struct RentalRecord does not has a member CDTitle. It is RecordTitle. So either you rename it or you use RecordTitle.

Here's the pseudo code how the delete actually work. (I use more descriptive names):
1
2
3
4
5
6
7
8
9
10
11
12
13
previous_rental = NULL
current_rental = theRental
while current_rental
  current_rental -> check title against the entered title
    true: break the loop
    false: previous_rental = current_rental
      current_rental = current_rental->next

if current_rental
  if previous_rental
    true: previous_rental->next = current_rental->next
    false: theRental = current_rental->next
  delete current_rental
Note that the while loop demonstrates how to find a specific record, too
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
void RentalStore :: addRecord(string &title , string &customerName){
	
	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl << endl
		<< "Enter Customer Name : ";
	getline( cin,customerName );
	cout	<< "Enter title : ";
	getline( cin, title );
	RentalRecord *newRental = new RentalRecord( title , customerName );
	if ( theRental != NULL ){
		RentalRecord *temp = theRental;
		while( temp->next ){ 
			temp = temp->next;
		}
		temp->next = newRental;
	}
	else
		theRental = newRental;
}

void RentalStore ::deleteRecord(string &deleteTheRecord , string &customerName){

	system( "cls" );
	int choice = 0;
	bool found = 1;
	RentalRecord *previous_rental = NULL;
	RentalRecord *current_rental = theRental;
	
	cout << "\tAdd New Record\n-----------------------------" << endl << endl;

	cout << "Enter Customer Name : ";
	getline ( cin , customerName );

	cout << "Enter title : ";
	getline( cin , deleteTheRecord );

	while( !current_rental != NULL ){
		if ( current_rental->customerName == customerName ){
			if( current_rental->RecordTitle == deleteTheRecord ){
				cout << "Found Record ! " << endl;
				found = 1;
				break;
			}
		}
		else{
			cout << "No any Record found ! " << endl << endl;
			previous_rental = current_rental;
			current_rental = current_rental->next;
			found = 0;
		}
	}

	if( found ){
		cout << "Customer Name : " << current_rental->customerName << endl
			<< "Borrowed CD   : " << current_rental->RecordTitle << endl << endl;
		if( current_rental != NULL ){
			if( previous_rental !=NULL ){
				previous_rental->next = current_rental->next;
			}
			else{
				theRental = current_rental->next;
			}
			delete current_rental;
		}
	}
}


The deleteRecord is purposely when a customer Return a CD then only delete
so If the customer Name not inside the record it will be display that no record found , else it will go to second if to compare the title again. if both if are true , only delete the record .
i think I did it with this ? or any mistake i make again? T.T

and for your addRecord()
i thought it's should be
while( temp->next != NULL )?
Last edited on
Don't put your "No any Record found ! " within that loop (It would appear to many times). Do this in the else of the if on line 53.

line 37: remove that ! before current_rental

Apart from that it looks right, but test it yourself.
You should be able by now to show all records (using such a loop)


i thought it's should be
while( temp->next != NULL )?
Then you would exclude the last record from the comparison. The point of the addRecord() was just to find the last record. It is actually possible to use the one or other kind of loop to accomplish the task. Just try...

I recommend to test that code before you continue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while( current_rental != NULL ){
		if ( current_rental->customerName == customerName ){
			if( current_rental->RecordTitle == deleteTheRecord ){
				cout << "Found Record ! " << endl;
				found = 1;
				break;
			}
		}
		else{
			previous_rental = current_rental;
			current_rental = current_rental->next;
			found = 0;
		}
	}


tested . but in deleteRecordthen when the CD it's found. it's successfull to print out . but in the else statement. my window will stuck at there and do nothing. ( for line 9 until 13 )

by the way . can help me or teach me how to use binary search tree to print out that of my CD collection base on my project?
my binary search tree have to read CDRecord.txt
1
2
3
4
5
6
7
Titanic
Hello World
Dancing world
Michael Jackson
Underground
Dive
Sorry , I'm Falling love on her 


I can't get the concept. mind to provide some and i try to do and post to u to check?
i just know the Right side is bigger than the root side , the left Side is smaller than the Root Size , and use binary tree to print out the CD title. sorry
Last edited on
but in the else statement. my window will stuck at there and do nothing. ( for line 9 until 13 )
This looks like you didn't initialized next (or another pointer) properly and it gets into an infinite loop.

I strongly recommend something like showAll() just to check whether 'add' or 'remove' worked. It's easy: use your delete while loop where only line 11 and an appropriate cout << ...->RecordTitle << endl is left.

By the way: addRecord() could be implemented easier (if it is not required that the record is appended):
1
2
3
4
5
6
7
8
9
10
void RentalStore :: addRecord(string &title){
	system( "cls" );
	cout << "\tAdd New Record\n-----------------------------" << endl
		<< "Enter title : ";
	getline( cin, title );
	RentalRecord *newRental = new RentalRecord( title );

newRental->next = theRental;
theRental = newRental;
}


I can't get the concept.
This concept is usually based on recursion. So do you know recursion?

Based on addRecord() above addNode() could be implemented like so:
1
2
3
4
5
6
7
8
create new TreeNode tn
if node
  if node->cdTitle < addtitle
    true: tn->left = node
    false: tn->right = node
else
  node = tn
}
everything less than goes to the left the other to the right. You can decide the other way round
Last edited on
1
2
3
4
5
6
7
8
void RentalStore :: showAllRecord(){
	RentalRecord *showRental = NULL;
	RentalRecord *current_rental = theRental;

	while( current_rental != NULL ){
		cout << "CD Title : " << showRental->RecordTitle << endl;
	}
}


something like this? but it's fail. error message pop out after run to the function

and i declare the tree class like this
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
struct TreeNode{
	string cdTitle;
	TreeNode *left;
	TreeNode *right;
};

class Tree{
private:
	TreeNode *root;
public:
	Tree();
	~Tree();
	
	bool Empty();
	void Display();

};

Tree :: Tree (){
	root = NULL;
}

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 Print( TreeNode*& tree ){
	if( tree != NULL ){
		Print( tree->left );
		cout << tree->cdTitle;
		Print( tree->right );
	}
}

void Tree :: Display(){
	Print ( root );
}


did i implement correctly for addNode? and mind to show me that how my BST have to read the title from my txt and show it? not really know the BST . i know what is recursive , keep call back the function . as i did the implementation right now

Besides that, I get an error for my BST implementation
1
2
3
4
5
6
1>main.obj : error LNK2005: "public: __thiscall Tree::Tree(void)" (??0Tree@@QAE@XZ) already defined in cd.obj
1>main.obj : error LNK2005: "public: bool __thiscall Tree::Empty(void)" (?Empty@Tree@@QAE_NXZ) already defined in cd.obj
1>main.obj : error LNK2005: "void __cdecl addNode(struct TreeNode * &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?addNode@@YAXAAPAUTreeNode@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in cd.obj
1>main.obj : error LNK2005: "void __cdecl Print(struct TreeNode * &)" (?Print@@YAXAAPAUTreeNode@@@Z) already defined in cd.obj
1>main.obj : error LNK2005: "public: void __thiscall Tree::Display(void)" (?Display@Tree@@QAEXXZ) already defined in cd.obj
1>C:\Users\User\Desktop\Inti\Applied\Assignment 5\Debug\Assignment 5.exe : fatal error LNK1169: one or more multiply defined symbols found
So another troll...
Mean?
you need to split the tree into a header file lines 1 to 17 (.hpp)
and a implementation file (.cpp) from line 18 to end
add them to your project.
Last edited on
Pages: 12