hai..
i juz started learning C++ language & started to try creating a program.
its a simple contacts list.
here's the code:
yes,its not completed nor izit a good program..
i juz wana learn,but somehow this prog doesn't seem to function prperly..
can anyone help me debug? or give instructions on how to improve it?
such knowledge is crucial to me.
thanx guys
:)
i juz started learning C++ language & started to try creating a program.
its a simple contacts list.
here's the code:
Code:
========================================================= #include <iostream> using namespace std; #include <string> struct node { string name; string job; string hp; string bday; node *nxt; }; node *start_ptr = NULL; node *current; int option = 0; void add_contact() { node *temp, *temp2; temp = new node; cout << "Please enter the name: "; getline(cin,temp->name); cout << "Please enter the birthday : "; getline(cin,temp->bday); cout << "Please enter the job : "; getline(cin,temp->job); cout << "Please enter the handphone number : "; getline(cin,temp->hp); temp->nxt = NULL; if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp2 = start_ptr; while (temp2->nxt != NULL) { temp2 = temp2->nxt; } temp2->nxt = temp; } } void display_list() { node *temp; temp = start_ptr; cout << endl; if (temp == NULL) cout << "The list is empty!" << endl; else { while (temp != NULL) { cout << "Name : " << temp->name << "\n"; cout << "Birthday : " << temp->bday << "\n"; cout << "Job : " << temp->job<< "\n"; cout << "handphone : " << temp->hp<< "\n"; if (temp == current) cout << " ------------^------------ Current highlighted contact"; cout << endl; temp = temp->nxt; } cout << "End of list!" << endl; } } void delete_first_contact() { node *temp; temp = start_ptr; start_ptr = start_ptr->nxt; delete temp; } void delete_last_contact() { node *temp1, *temp2; if (start_ptr == NULL) cout << "The list is empty!" << endl; else { temp1 = start_ptr; if (temp1->nxt == NULL) { delete temp1; start_ptr = NULL; } else { while (temp1->nxt != NULL) { temp2 = temp1; temp1 = temp1->nxt; } delete temp1; temp2->nxt = NULL; } } } void move_forward () { if (current->nxt == NULL) cout << "You are at the end of the list." << endl; else current = current->nxt; } void move_backward () { if (current == start_ptr) cout << "You are at the start of the list" << endl; else { node *previous; previous = start_ptr; while (previous->nxt != current) { previous = previous->nxt; } current = previous; } } void main() { start_ptr = NULL; do { display_list(); cout << endl; cout << "Please select an option : " << endl; cout << "0. Exit the program." << endl; cout << "1. Add a contact to the list." << endl; cout << "2. Delete the 1st contact from the list." << endl; cout << "3. Delete the last contact from the list." << endl; cout << "4. Move forward." << endl; cout << "5. Move backward" << endl; cout << endl << " >> "; cin >> option; switch (option) { case 1 : add_contact(); break; case 2 : delete_first_contact(); break; case 3 : delete_last_contact(); break; case 4 : move_forward(); break; case 5 : move_backward(); } } while (option != 0); } ======================================================
i juz wana learn,but somehow this prog doesn't seem to function prperly..
can anyone help me debug? or give instructions on how to improve it?
such knowledge is crucial to me.
thanx guys
:)
Comment