I have the following code to initilize struct booknode in a link list.

// Here is how I defined booknode
typedef struct booknode
{
	char *title;			// char[], holds book title 
	char *author;			// char[], holds book author
	char *isbn;				// char[], holds book isbn
    float price;			// float, holds sales price of book
    struct booknode *next;  // Pointer to next booknode
} BOOKNODE;

Code that initilizes it with four books to form a link list

void initializebookdata( void )
{
	// Get memory for first book node
	BOOKNODE *firstbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));

	headptr = firstbook;	// Set the head pointer equal to the first book node memory

	// Add the first books data
	firstbook->title = "All the King's Men";
	firstbook->author = "Robert Penn Warren";
	firstbook->isbn = "0156004801";
	firstbook->price = 13.46F;
	// Get memory for second book node
	BOOKNODE *secondbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
	firstbook->next = secondbook;

	secondbook->title = "The C++ Programming Language (Special 3rd Edition)";
	secondbook->author = "Bjarne Stroustrup";
	secondbook->isbn = "0201700735";
	secondbook->price = 54.34F;
	// Get memory for third book node
	BOOKNODE *thirdbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
	secondbook->next = thirdbook;			

	thirdbook->title = "The C++ Standard Library : A Tutorial and Reference";
	thirdbook->author = "Nicolai M. Josuttis";
	thirdbook->isbn = "0201379260";
	thirdbook->price = 51.19F;
	// Get memory for fourth book node
	BOOKNODE *forthbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
	thirdbook->next = forthbook;			

	forthbook->title = "C++ Primer (4th Edition)";
	forthbook->author = "Stanley B. Lippman, Josée Lajoie, Barbara E. Moo";
	forthbook->isbn = "0201721481";
	forthbook->price = 32.99F;
	forthbook->next = 0;			// Mark it end of node list
}

Code that prints out the the above link list

int displayinventory( void )
{
	int s=0;			// User's selection
	int i=1;			// Counter.  Starts at one to display number for user to make selection
	wkptr = headptr;	// Used to walk the structure

	// Start endless loop.  Break out when user entered valid input
	while (1)	// Start of while()
	{
		clrscr();	// Clear the console's screen

		// Print out the title of this screen to user
		printf("%s", INVENTORYTITLE);	

		while( wkptr != 0)	// Start while loop.
		{
			// Print inventory line
			printf("%d: %s\n   By: %s - %s - %0.2f\n\n", (i++), wkptr->title, wkptr->author, wkptr->isbn, wkptr->price );
			wkptr = wkptr->next;	// Advance pointer to next address in list
		}	// End while()

		// Print user instructions.  Use i-1 to display the upper choice limit
		printf("Enter 1-%d or (-1) to return to main menu: ", i-1);
		scanf("%d", &s);	// Scan input into s

		// Test for valid answer
		if ( ( s == -1 ) || ( ( s > 0 ) && ( s<i ) ) )	// Valid response
		{
			break;	// break out of endless while()
		}
		else	// Invalid response.  Reset everything and start again
		{
			i=1;				// Counter.  Starts at one to display number for user to make selection
			wkptr = headptr;	// Used to walk the structure
		} 
	}	// End while()

	return s;	// Return users input which is in variable s
}

Here is where I am having problems. I have the following function to ask for books information and I want to add it to my link list. It seems to work, but does not print out what I entered. I am using the same printing function as above. The price is ok, but the book's name, author, and isbn are all garbage. Looks like it is reading memory segments some where is space. :cry:

void getBookData(void)
{
	BOOKNODE *newbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
	wkptr = headptr;					// Used to control the while loop
	clrscr();	// Clear the console's screen
	char booktitle[256];
	char author[256];
	char isbn[15];
	float price = 0.0F;
	cout << MENU2TITLE;

	// Get books data
	cout << "Please enter books title: ";
	cin.getline(booktitle, 256);
	cout << "Please enter books author: ";
	cin.getline(author, 256);
	cout << "Please enter books ISBN: ";
	cin.getline(isbn, 15);
	cout << "Please enter books price:  $[00.00] ";
	cin >> price;

	newbook->title=booktitle;
	newbook->author=author;
	newbook->isbn=isbn;
	newbook->price=price;
	newbook->next=0;
	// Add Book to end of link list 
	if ( wkptr == 0 )					// First one on the list
	{
		wkptr->next = newbook;
	}
	else								// Have other books
	{
		while ( wkptr->next != 0 )		// Find the end of the list
		{
			wkptr = wkptr->next;
		}
		wkptr->next = newbook;			// Last on the list so point to new book
	}
//	addinventory(booktitle, author, isbn, price);
}

I would be grateful for any help you can give me.

Recommended Answers

All 6 Replies

Use code tags. I've added them for you this time, but next time I won't be so nice about it.

>but the book's name, author, and isbn are all garbage
That's no surprise. You're initializing the string data of the new node with the addresses of local arrays. When the function returns, memory for the arrays is released and will likely contain "garbage" when you print them out and expect what you left there. Watch:

#include <stdio.h>

char *p;

void initialize ( void )
{
  char bad[] = "This is a test";

  p = bad;
}

int main ( void )
{
  initialize();

  puts ( p );

  return 0;
}

The fix, of course, is to avoid initializing the pointer with memory that has a smaller scope. Consider the same program that uses dynamic memory rather than an array:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *p;

void initialize ( void )
{
  p = malloc ( 15 );
  strcpy ( p, "This is a test" );
}

int main ( void )
{
  initialize();

  puts ( p );
  free ( p );

  return 0;
}

Sorry for not being "smart" enough for you. I am only in my third week of C++. I have no experience in this language so everything is new.

PS. Did you do a link list in your third week of class? I would guess not.

What the hell is wrong with you people tonight? I didn't say anything provocative this time and you still take offense. :mad: Next time you ask a question I'll remember how grateful you were and ignore you. Apparently that's what you want.

Your reply was ok but your signature block is what I took offense to.

"I'll try to be nicer if you try to be smarter."

>Your reply was ok but your signature block is what I took offense to.
Congratulations, you're one of the few people to render me speechless. But I'm not changing my signature just because you take some irrational offense to it.

Sorry for not being "smart" enough for you. I am only in my third week of C++. I have no experience in this language so everything is new.

If this is C++ (right now it's looking like C code with cin and cout statements) I recommend using the standard library string datatype. Is this for a class at school? Why are they teaching C in C++ trappings? You shouldn't be playing with character pointers in your first three weeks.

PS. Did you do a link list in your third week of class? I would guess not.

Yes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.