944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4716
  • C++ RSS
Jul 3rd, 2005
0

Link List Insert problems

Expand Post »
I have the following code to initilize struct booknode in a link list.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakejacobson is offline Offline
3 posts
since Jul 2005
Jul 3rd, 2005
0

Re: Link List Insert problems

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 3rd, 2005
0

Re: Link List Insert problems

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakejacobson is offline Offline
3 posts
since Jul 2005
Jul 3rd, 2005
0

Re: Link List Insert problems

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 3rd, 2005
0

Re: Link List Insert problems

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."
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakejacobson is offline Offline
3 posts
since Jul 2005
Jul 3rd, 2005
0

Re: Link List Insert problems

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 4th, 2005
0

Re: Link List Insert problems

Quote 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.

Quote ...
PS. Did you do a link list in your third week of class? I would guess not.
Yes.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with Greatest common divisor
Next Thread in C++ Forum Timeline: void sort2(char* &p, char* &q)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC