Click here to Skip to main content
16,006,531 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Disabling Close in Console Pin
Chris Richardson1-May-03 17:25
Chris Richardson1-May-03 17:25 
GeneralGetting binary values from registry Pin
Steve L.1-May-03 10:44
Steve L.1-May-03 10:44 
GeneralRe: Getting binary values from registry Pin
Michael Dunn1-May-03 11:01
sitebuilderMichael Dunn1-May-03 11:01 
GeneralRe: Getting binary values from registry Pin
Steve L.1-May-03 11:24
Steve L.1-May-03 11:24 
GeneralRe: Getting binary values from registry Pin
David Crow2-May-03 4:30
David Crow2-May-03 4:30 
GeneralItemData in a virtual list control Pin
Gilfrog1-May-03 10:39
Gilfrog1-May-03 10:39 
GeneralRe: ItemData in a virtual list control Pin
Phil Hamer1-May-03 14:17
Phil Hamer1-May-03 14:17 
GeneralCan't display items from an array. Pin
grscot1-May-03 10:32
grscot1-May-03 10:32 
Dear all,
All I'm trying to do is to enter 3 books from the keyboard to an array and then traverse the array to display the elements and also prompt the user to enter the book names to delete them from the array(if the book exists into the array). 

The code that adds to the array is:

<br />
template<class Object><br />
void List<Object>::addElement(char* type)<br />
{<br />
	if (this->num_elements == MAX_ELEMENTS)<br />
	{<br />
		cout<<"No more room in the "<<type<<" array.\n";<br />
		cout<<"The maximum number of "<<type<<" is "<<MAX_ELEMENTS<<"."<<endl;<br />
	}<br />
	else<br />
	{<br />
		this->element_list[num_elements]=new Object;<br />
		(this->num_elements)++;<br />
	}<br />
}<br />

The code that displays the elements is:


<br />
template<class Object><br />
void List<Object>::displayElements(char* type)<br />
{<br />
	if (num_elements == 0)<br />
		cout<<"No "<<type<<" is found in the "<<type<<" array.\n";<br />
	else<br />
		for(int element=0; element<this->num_elements; element++)<br />
		{<br />
			cout<<'\n';<br />
			this->element_list[element]->display(association_list.get_data(element_list[element]));<br />
		}<br />
}<br />

The code for deleting is:

<br />
template<class Object><br />
void List<Object>::removeElement(char* type)<br />
{<br />
	char* item;<br />
	if (this->num_elements == 0)<br />
		cout<<"There are no "<<type<<"s to delete from the "<<type<<" array."<<endl;<br />
	else<br />
		item=get_string_ver2("Input element to delete: ");<br />
	for(int element=0; element<this->num_elements; element++)<br />
		if (strcmp(item,element_list[element]->get_data()) == 0)<br />
		//cout<<"Element found\n";<br />
		element_list[element]='\0';<br />
	else<br />
		cout<<"Element not found\n";<br />
}<br />


But there is a problem after deleting a book from the array.
I can't display the elements of the array, after a book has been deleted. For example if I enter a, a as the first book and b, b as the second book and then after deleting b, b, try to display the books into the array, I will only see the details of the first book and after that the program will be terminated due to an error. The error occurs at the file:


<br />
Book.cpp<br />
Book::Book()<br />
{<br />
	cout<<"Book constructor called called\n";<br />
	this->bookDetails=get_string_ver2("input book title and author separated by a comma and a space character: ");<br />
}<br />
<br />
Book::~Book()<br />
{<br />
	if (this->bookDetails)<br />
		delete [] this->bookDetails;<br />
}<br />
<br />
<br />
void Book::display(Member* borrower)<br />
{<br />
	cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl;<br />
	if (borrower)<br />
		cout<<"The member's name is: "<<borrower->get_data()/*get_name()*/<<"."<<endl;<br />
	else<br />
		cout<<"No member has borrowed a book."<<endl;<br />
}<br />

in the first line of code of the display function.(cout<<"Book title ....)

What is wrong?
If you need more details or anything more about the program, please let me know.

Regards,
grscot

GeneralRe: Can't display items from an array. Pin
Neville Franks1-May-03 11:55
Neville Franks1-May-03 11:55 
GeneralRe: Can't display items from an array. Pin
Toni781-May-03 12:09
Toni781-May-03 12:09 
GeneralUsing modem Pin
Anonymous1-May-03 10:04
Anonymous1-May-03 10:04 
GeneralRe: Using modem Pin
Jason Henderson1-May-03 20:29
Jason Henderson1-May-03 20:29 
GeneralRe: Using modem Pin
Anonymous2-May-03 0:07
Anonymous2-May-03 0:07 
GeneralRe: Using modem Pin
John R. Shaw2-May-03 20:28
John R. Shaw2-May-03 20:28 
GeneralGetting keyboard input. Pin
Anonymous1-May-03 9:46
Anonymous1-May-03 9:46 
GeneralRe: Getting keyboard input. Pin
Neville Franks1-May-03 11:58
Neville Franks1-May-03 11:58 
GeneralRe: Getting keyboard input. Pin
John R. Shaw2-May-03 20:40
John R. Shaw2-May-03 20:40 
GeneralWindows Media Player Pin
nlecren1-May-03 9:41
nlecren1-May-03 9:41 
QuestionHow to get the handle to a pane in the status bar Pin
thedex1-May-03 8:06
thedex1-May-03 8:06 
AnswerRe: How to get the handle to a pane in the status bar Pin
David Crow1-May-03 8:32
David Crow1-May-03 8:32 
GeneralRe: How to get the handle to a pane in the status bar Pin
thedex1-May-03 8:49
thedex1-May-03 8:49 
GeneralRe: How to get the handle to a pane in the status bar Pin
David Crow1-May-03 9:05
David Crow1-May-03 9:05 
AnswerRe: How to get the handle to a pane in the status bar Pin
Michael Dunn1-May-03 11:02
sitebuilderMichael Dunn1-May-03 11:02 
AnswerRe: How to get the handle to a pane in the status bar Pin
John R. Shaw2-May-03 21:10
John R. Shaw2-May-03 21:10 
QuestionHow to import a graphic image to MFC Pin
Anonymous1-May-03 7:50
Anonymous1-May-03 7:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.