| | |
Link List Insert problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 3
Reputation:
Solved Threads: 0
I have the following code to initilize struct booknode in a link list.
Code that initilizes it with four books to form a link list
Code that prints out the the above link list
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:
I would be grateful for any help you can give me.
C++ Syntax (Toggle Plain Text)
// 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;
C++ Syntax (Toggle Plain Text)
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 }
C++ Syntax (Toggle Plain Text)
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 }
C++ Syntax (Toggle Plain Text)
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); }
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:
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:
>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:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> char *p; void initialize ( void ) { char bad[] = "This is a test"; p = bad; } int main ( void ) { initialize(); puts ( p ); return 0; }
C++ Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
•
•
•
•
Originally Posted by jakejacobson
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.
![]() |
Similar Threads
- Binary Tree with Link List (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
- circular link list (C)
Other Threads in the C++ Forum
- Previous Thread: Homework - error C2059: syntax error : ']'
- Next Thread: void sort2(char* &p, char* &q)
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






