| | |
C++ linked list help??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 8
Reputation:
Solved Threads: 0
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.
<< moderator edit: added code tags: [code][/code] >>
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.
C++ Syntax (Toggle Plain Text)
# include<iostream.h> # include<stdlib.h> # include<conio.h> # include<string.h> struct ListEntry { char name[10]; int idnumber; struct ListEntry *next; } start,*node,*prev; void add(); void viewdb(); void main() { int choice; system("cls"); cout<<"Choose option from the menu below:\n\n"; cout<<"[1] Insert an ID Number and Name\n"; cout<<"[2] View the database\n"; cout<<"[3] Exit\n"; cout<<"\t\tEnter your choice: "; cin>>choice; switch(choice) { case 1: add(); break; case 2: viewdb(); break; case 3: exit(1); } } void add() { char choice; start.next=NULL; node=&start; do { system("cls"); node->next=(struct ListEntry*)malloc(sizeof(struct ListEntry)); node=node->next; cout<<"Enter an ID Number and Name:\t"; cin>>node->idnumber>>node->name; cout<<"Record saved. Input another? (Y,N): "; cin>>choice; node->next=NULL; } while(choice=='Y'||choice=='y'); main(); } void viewdb() { node=start.next; system("cls"); while(node) { cout<<node->idnumber<<"\t"<<node->name<<endl; node=node->next; } cout<<endl; getch(); main(); }
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
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
<< moderator edit: added code tags: [code][/code] >>
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
C++ Syntax (Toggle Plain Text)
# include<iostream.h> # include<stdlib.h> # include<conio.h> # include<string.h> struct ListEntry { char name[10]; int idnumber; struct ListEntry *next; } start,*node,*prev; void add(); void viewdb(); void main() { int choice; system("cls"); cout << "Choose option from the menu below:\n\n"; cout << "[1] Insert an ID Number and Name\n"; cout << "[2] View the database\n"; cout << "[3] Exit\n"; cout << "\t\tEnter your choice: "; cin >> choice; switch(choice) { case 1: add(); break; case 2: viewdb(); break; case 3: exit(1); } } void add() { char choice; start.next=NULL; node=&start; do { system("cls"); node->next=(struct ListEntry*)malloc(sizeof(struct ListEntry)); node=node->next; cout<<"Enter an ID Number and Name:\t"; cin>>node->idnumber>>node->name; cout<<"Record saved. Input another? (Y,N): "; cin>>choice; node->next=NULL; } while(choice=='Y'||choice=='y'); return; } void viewdb() { node=start.next; system("cls"); while(node) { cout<<node->idnumber<<"\t"<<node->name<<endl; node=node->next; } cout<<endl; getch(); return; }
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
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
<< moderator edit: added code tags: [code][/code] >>
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
C++ Syntax (Toggle Plain Text)
# include<iostream.h> # include<stdlib.h> # include<conio.h> # include<string.h> struct ListEntry { char name[10]; int idnumber; struct ListEntry *next; } start,*node,*prev; void add(); void viewdb(); void main() { int choice; do { system("cls"); cout << "Choose option from the menu below:\n\n"; cout << "[1] Insert an ID Number and Name\n"; cout << "[2] View the database\n"; cout << "[3] Exit\n"; cout << "\t\tEnter your choice: "; cin >> choice; switch(choice) { case 1: add(); break; case 2: viewdb(); break; //case 3: // exit(1); } } while (choice != 3); } void add() { char choice; start.next = NULL; node = &start; do { system("cls"); node->next = (struct ListEntry*) malloc(sizeof(struct ListEntry) ); node = node->next; cout << "Enter an ID Number:\t"; cin >> node->idnumber; cout << "Enter a Name:\t"; cin >> node->name; cout << "Record saved. Input another? (Y, N): "; cin >> choice; node->next = NULL; } while (choice == 'Y' || choice == 'y'); return; } void viewdb() { node = start.next; system("cls"); while (node) { cout << node->idnumber << "\t" << node->name << endl; node = node->next; } cout << endl; getch(); return; }
•
•
Join Date: Jun 2005
Posts: 19
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Please some one help me in this code
- Next Thread: Please some one urgent help me in this code
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





