Link List Insert problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 3
Reputation: jakejacobson is an unknown quantity at this point 
Solved Threads: 0
jakejacobson jakejacobson is offline Offline
Newbie Poster

Link List Insert problems

 
0
  #1
Jul 3rd, 2005
I have the following code to initilize struct booknode in a link list.
  1. // Here is how I defined booknode
  2. typedef struct booknode
  3. {
  4. char *title; // char[], holds book title
  5. char *author; // char[], holds book author
  6. char *isbn; // char[], holds book isbn
  7. float price; // float, holds sales price of book
  8. struct booknode *next; // Pointer to next booknode
  9. } BOOKNODE;
Code that initilizes it with four books to form a link list
  1. void initializebookdata( void )
  2. {
  3. // Get memory for first book node
  4. BOOKNODE *firstbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
  5.  
  6. headptr = firstbook; // Set the head pointer equal to the first book node memory
  7.  
  8. // Add the first books data
  9. firstbook->title = "All the King's Men";
  10. firstbook->author = "Robert Penn Warren";
  11. firstbook->isbn = "0156004801";
  12. firstbook->price = 13.46F;
  13. // Get memory for second book node
  14. BOOKNODE *secondbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
  15. firstbook->next = secondbook;
  16.  
  17. secondbook->title = "The C++ Programming Language (Special 3rd Edition)";
  18. secondbook->author = "Bjarne Stroustrup";
  19. secondbook->isbn = "0201700735";
  20. secondbook->price = 54.34F;
  21. // Get memory for third book node
  22. BOOKNODE *thirdbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
  23. secondbook->next = thirdbook;
  24.  
  25. thirdbook->title = "The C++ Standard Library : A Tutorial and Reference";
  26. thirdbook->author = "Nicolai M. Josuttis";
  27. thirdbook->isbn = "0201379260";
  28. thirdbook->price = 51.19F;
  29. // Get memory for fourth book node
  30. BOOKNODE *forthbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
  31. thirdbook->next = forthbook;
  32.  
  33. forthbook->title = "C++ Primer (4th Edition)";
  34. forthbook->author = "Stanley B. Lippman, Josée Lajoie, Barbara E. Moo";
  35. forthbook->isbn = "0201721481";
  36. forthbook->price = 32.99F;
  37. forthbook->next = 0; // Mark it end of node list
  38. }
Code that prints out the the above link list
  1. int displayinventory( void )
  2. {
  3. int s=0; // User's selection
  4. int i=1; // Counter. Starts at one to display number for user to make selection
  5. wkptr = headptr; // Used to walk the structure
  6.  
  7. // Start endless loop. Break out when user entered valid input
  8. while (1) // Start of while()
  9. {
  10. clrscr(); // Clear the console's screen
  11.  
  12. // Print out the title of this screen to user
  13. printf("%s", INVENTORYTITLE);
  14.  
  15. while( wkptr != 0) // Start while loop.
  16. {
  17. // Print inventory line
  18. printf("%d: %s\n By: %s - %s - %0.2f\n\n", (i++), wkptr->title, wkptr->author, wkptr->isbn, wkptr->price );
  19. wkptr = wkptr->next; // Advance pointer to next address in list
  20. } // End while()
  21.  
  22. // Print user instructions. Use i-1 to display the upper choice limit
  23. printf("Enter 1-%d or (-1) to return to main menu: ", i-1);
  24. scanf("%d", &s); // Scan input into s
  25.  
  26. // Test for valid answer
  27. if ( ( s == -1 ) || ( ( s > 0 ) && ( s<i ) ) ) // Valid response
  28. {
  29. break; // break out of endless while()
  30. }
  31. else // Invalid response. Reset everything and start again
  32. {
  33. i=1; // Counter. Starts at one to display number for user to make selection
  34. wkptr = headptr; // Used to walk the structure
  35. }
  36. } // End while()
  37.  
  38. return s; // Return users input which is in variable s
  39. }
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:
  1. void getBookData(void)
  2. {
  3. BOOKNODE *newbook = (BOOKNODE*) malloc(sizeof(BOOKNODE));
  4. wkptr = headptr; // Used to control the while loop
  5. clrscr(); // Clear the console's screen
  6. char booktitle[256];
  7. char author[256];
  8. char isbn[15];
  9. float price = 0.0F;
  10. cout << MENU2TITLE;
  11.  
  12. // Get books data
  13. cout << "Please enter books title: ";
  14. cin.getline(booktitle, 256);
  15. cout << "Please enter books author: ";
  16. cin.getline(author, 256);
  17. cout << "Please enter books ISBN: ";
  18. cin.getline(isbn, 15);
  19. cout << "Please enter books price: $[00.00] ";
  20. cin >> price;
  21.  
  22. newbook->title=booktitle;
  23. newbook->author=author;
  24. newbook->isbn=isbn;
  25. newbook->price=price;
  26. newbook->next=0;
  27. // Add Book to end of link list
  28. if ( wkptr == 0 ) // First one on the list
  29. {
  30. wkptr->next = newbook;
  31. }
  32. else // Have other books
  33. {
  34. while ( wkptr->next != 0 ) // Find the end of the list
  35. {
  36. wkptr = wkptr->next;
  37. }
  38. wkptr->next = newbook; // Last on the list so point to new book
  39. }
  40. // addinventory(booktitle, author, isbn, price);
  41. }
I would be grateful for any help you can give me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Link List Insert problems

 
0
  #2
Jul 3rd, 2005
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:
  1. #include <stdio.h>
  2.  
  3. char *p;
  4.  
  5. void initialize ( void )
  6. {
  7. char bad[] = "This is a test";
  8.  
  9. p = bad;
  10. }
  11.  
  12. int main ( void )
  13. {
  14. initialize();
  15.  
  16. puts ( p );
  17.  
  18. return 0;
  19. }
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char *p;
  6.  
  7. void initialize ( void )
  8. {
  9. p = malloc ( 15 );
  10. strcpy ( p, "This is a test" );
  11. }
  12.  
  13. int main ( void )
  14. {
  15. initialize();
  16.  
  17. puts ( p );
  18. free ( p );
  19.  
  20. return 0;
  21. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 3
Reputation: jakejacobson is an unknown quantity at this point 
Solved Threads: 0
jakejacobson jakejacobson is offline Offline
Newbie Poster

Re: Link List Insert problems

 
0
  #3
Jul 3rd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Link List Insert problems

 
0
  #4
Jul 3rd, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 3
Reputation: jakejacobson is an unknown quantity at this point 
Solved Threads: 0
jakejacobson jakejacobson is offline Offline
Newbie Poster

Re: Link List Insert problems

 
0
  #5
Jul 3rd, 2005
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."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Link List Insert problems

 
0
  #6
Jul 3rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Link List Insert problems

 
0
  #7
Jul 4th, 2005
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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC