C++ linked list help??

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

Join Date: Jun 2005
Posts: 8
Reputation: yaan is an unknown quantity at this point 
Solved Threads: 0
yaan yaan is offline Offline
Newbie Poster

C++ linked list help??

 
0
  #1
Jun 25th, 2005
I've gotten feedback from my instructor to modify my code because it contains poor design techniques. I need to add a " return " after each function without it going back to " main " and also to avoid the use of global headers.

I've tried to modify it several ways, but I cannot seem to do it. Any help would be greatly appreciated as I am still learning the c++ language.

  1. # include<iostream.h>
  2. # include<stdlib.h>
  3. # include<conio.h>
  4. # include<string.h>
  5.  
  6.  
  7. struct ListEntry
  8. {
  9. char name[10];
  10. int idnumber;
  11. struct ListEntry *next;
  12. }
  13. start,*node,*prev;
  14.  
  15. void add();
  16. void viewdb();
  17.  
  18. void main()
  19.  
  20. {
  21. int choice;
  22.  
  23. system("cls");
  24. cout<<"Choose option from the menu below:\n\n";
  25. cout<<"[1] Insert an ID Number and Name\n";
  26. cout<<"[2] View the database\n";
  27. cout<<"[3] Exit\n";
  28. cout<<"\t\tEnter your choice: ";
  29. cin>>choice;
  30.  
  31. switch(choice)
  32. {
  33. case 1:
  34. add();
  35. break;
  36. case 2:
  37. viewdb();
  38. break;
  39. case 3:
  40. exit(1);
  41.  
  42.  
  43. }
  44. }
  45.  
  46. void add()
  47. {
  48. char choice;
  49. start.next=NULL;
  50. node=&start;
  51. do
  52. {
  53.  
  54. system("cls");
  55. node->next=(struct ListEntry*)malloc(sizeof(struct ListEntry));
  56. node=node->next;
  57.  
  58. cout<<"Enter an ID Number and Name:\t";
  59. cin>>node->idnumber>>node->name;
  60.  
  61. cout<<"Record saved. Input another? (Y,N): ";
  62. cin>>choice;
  63.  
  64. node->next=NULL;
  65. }
  66.  
  67. while(choice=='Y'||choice=='y');
  68. main();
  69. }
  70.  
  71. void viewdb()
  72. {
  73.  
  74. node=start.next;
  75. system("cls");
  76. while(node)
  77. {
  78. cout<<node->idnumber<<"\t"<<node->name<<endl;
  79. node=node->next;
  80. }
  81.  
  82. cout<<endl;
  83. getch();
  84. main();
  85. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: BruceWilson512 is an unknown quantity at this point 
Solved Threads: 0
BruceWilson512 BruceWilson512 is offline Offline
Newbie Poster

Re: C++ linked list help??

 
0
  #2
Jun 25th, 2005
yaan,

Inside your function definitions, you need to replace "main()" with "return".

You are calling the main function instead of actually returning to it.

You should also add some white space to your code so it is more readable. See below.

Take care,
Bruce

  1. # include<iostream.h>
  2. # include<stdlib.h>
  3. # include<conio.h>
  4. # include<string.h>
  5.  
  6.  
  7. struct ListEntry
  8. {
  9. char name[10];
  10. int idnumber;
  11. struct ListEntry *next;
  12. }
  13. start,*node,*prev;
  14.  
  15. void add();
  16. void viewdb();
  17.  
  18. void main()
  19.  
  20. {
  21. int choice;
  22.  
  23. system("cls");
  24. cout << "Choose option from the menu below:\n\n";
  25. cout << "[1] Insert an ID Number and Name\n";
  26. cout << "[2] View the database\n";
  27. cout << "[3] Exit\n";
  28. cout << "\t\tEnter your choice: ";
  29. cin >> choice;
  30.  
  31. switch(choice)
  32. {
  33. case 1:
  34. add();
  35. break;
  36. case 2:
  37. viewdb();
  38. break;
  39. case 3:
  40. exit(1);
  41.  
  42.  
  43. }
  44. }
  45.  
  46.  
  47.  
  48. void add()
  49. {
  50. char choice;
  51.  
  52. start.next=NULL;
  53.  
  54. node=&start;
  55.  
  56. do
  57.  
  58. {
  59.  
  60.  
  61.  
  62. system("cls");
  63.  
  64. node->next=(struct ListEntry*)malloc(sizeof(struct ListEntry));
  65.  
  66. node=node->next;
  67.  
  68.  
  69. cout<<"Enter an ID Number and Name:\t";
  70.  
  71. cin>>node->idnumber>>node->name;
  72.  
  73.  
  74. cout<<"Record saved. Input another? (Y,N): ";
  75.  
  76. cin>>choice;
  77.  
  78.  
  79. node->next=NULL;
  80.  
  81. }
  82.  
  83.  
  84. while(choice=='Y'||choice=='y');
  85.  
  86. return;
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void viewdb()
  96. {
  97.  
  98.  
  99. node=start.next;
  100.  
  101. system("cls");
  102.  
  103. while(node)
  104.  
  105. {
  106.  
  107. cout<<node->idnumber<<"\t"<<node->name<<endl;
  108.  
  109. node=node->next;
  110.  
  111. }
  112.  
  113.  
  114. cout<<endl;
  115.  
  116. getch();
  117.  
  118. return;
  119. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: BruceWilson512 is an unknown quantity at this point 
Solved Threads: 0
BruceWilson512 BruceWilson512 is offline Offline
Newbie Poster

Re: C++ linked list help??

 
0
  #3
Jun 25th, 2005
Yaan,

Eliminating the calls to main is correct.

Your logic must be updated to allow your main menu to be shown until exit.

I have provided an update to your code below.

The update also seperates the requests for id and name.

Take care,
Bruce




  1. # include<iostream.h>
  2. # include<stdlib.h>
  3. # include<conio.h>
  4. # include<string.h>
  5.  
  6.  
  7. struct ListEntry
  8. {
  9. char name[10];
  10. int idnumber;
  11. struct ListEntry *next;
  12. }
  13. start,*node,*prev;
  14.  
  15. void add();
  16. void viewdb();
  17.  
  18. void main()
  19.  
  20. {
  21. int choice;
  22.  
  23.  
  24. do
  25. {
  26.  
  27. system("cls");
  28. cout << "Choose option from the menu below:\n\n";
  29. cout << "[1] Insert an ID Number and Name\n";
  30. cout << "[2] View the database\n";
  31. cout << "[3] Exit\n";
  32. cout << "\t\tEnter your choice: ";
  33. cin >> choice;
  34.  
  35. switch(choice)
  36. {
  37. case 1:
  38. add();
  39. break;
  40. case 2:
  41. viewdb();
  42. break;
  43. //case 3:
  44. // exit(1);
  45. }
  46.  
  47. } while (choice != 3);
  48.  
  49.  
  50. }
  51.  
  52.  
  53.  
  54. void add()
  55. {
  56. char choice;
  57.  
  58. start.next = NULL;
  59.  
  60. node = &start;
  61.  
  62. do
  63.  
  64. {
  65. system("cls");
  66.  
  67. node->next = (struct ListEntry*) malloc(sizeof(struct ListEntry) );
  68. node = node->next;
  69.  
  70. cout << "Enter an ID Number:\t";
  71. cin >> node->idnumber;
  72.  
  73. cout << "Enter a Name:\t";
  74. cin >> node->name;
  75.  
  76.  
  77. cout << "Record saved. Input another? (Y, N): ";
  78. cin >> choice;
  79.  
  80.  
  81. node->next = NULL;
  82.  
  83. } while (choice == 'Y' || choice == 'y');
  84.  
  85. return;
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. void viewdb()
  95. {
  96.  
  97.  
  98. node = start.next;
  99.  
  100. system("cls");
  101.  
  102. while (node)
  103.  
  104. {
  105.  
  106. cout << node->idnumber << "\t" << node->name << endl;
  107.  
  108. node = node->next;
  109.  
  110. }
  111.  
  112.  
  113. cout << endl;
  114.  
  115. getch();
  116.  
  117. return;
  118. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: yaan is an unknown quantity at this point 
Solved Threads: 0
yaan yaan is offline Offline
Newbie Poster

Re: C++ linked list help??

 
0
  #4
Jun 25th, 2005
I see how adding more space makes the code easier to read. I tried your modifications and they worked. I was adding a " return 0; " instead of just a " return; "

Would you happen to know how to remove the global header node?

Thanks Bruce.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: BruceWilson512 is an unknown quantity at this point 
Solved Threads: 0
BruceWilson512 BruceWilson512 is offline Offline
Newbie Poster

Re: C++ linked list help??

 
0
  #5
Jun 25th, 2005
Yaan,

There is another bug in your code.

Each time you start inserting records you are setting start.next = NULL. This destroys your pointer to any previously entered data.

Try inserting data, go back to main menu, insert more data, go back to main menu and view the db. The first set of data is gone.

Take care,
Bruce
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 19
Reputation: BruceWilson512 is an unknown quantity at this point 
Solved Threads: 0
BruceWilson512 BruceWilson512 is offline Offline
Newbie Poster

Re: C++ linked list help??

 
0
  #6
Jun 25th, 2005
You aren't even using the variable "prev".

Do you really need the "start" variable?

Bruce
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