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.