Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   function cause error (http://www.go4expert.com/forums/function-cause-error-t19680/)

low1988 6Oct2009 16:07

function cause error
 
Code:

  1. #include <iostream>

  2. #include <conio.h>

  3. using namespace std;

  4. struct employeeInfo

  5. {

  6.       string name;

  7.       int id;

  8.       string department;

  9.       employeeInfo *next;

  10. };

  11. class List {

  12.         public:

  13.                 List(void) { head = NULL; } // constructor

  14.                 ~List(void); // destructor

  15.                 bool IsEmpty() { return head == NULL; }

  16.                 employeeInfo* InsertEmployee(string ,int ,string );

  17.                 void DisplayList(void);

  18.                 void numEmployee();

  19.                 int DeleteNode(int x);

  20.                 int FindNode(int y);

  21.                 void ValNode(int,string);

  22.           private:

  23.                 employeeInfo* head;

  24. };

  25. List::~List(void) {

  26.         employeeInfo* currNode = head, *nextNode = NULL;

  27.         while (currNode != NULL) {

  28.                 nextNode = currNode->next;

  29.                 // destroy the current node

  30.                 delete currNode;

  31.                 currNode = nextNode;

  32.         }

  33. }

  34. employeeInfo* List::InsertEmployee(string nama,int number,string affiliate) {

  35.         int currIndex = 0;

  36.         employeeInfo* currNode = head;

  37.         employeeInfo* prevNode = NULL;

  38.         while (currNode) {

  39.                 prevNode = currNode;

  40.       currNode = currNode->next;

  41.                 currIndex++;

  42.         }

  43.   employeeInfo* newNode = new employeeInfo;

  44.         newNode->name = nama;

  45.         newNode->id=number;

  46.         newNode->department= affiliate;

  47.         if (currIndex == 0) {

  48.                 newNode->next = head;

  49.                 head = newNode;

  50.           } else {

  51.                 newNode->next = prevNode->next;

  52.                 prevNode->next = newNode;

  53.           }

  54. return newNode;

  55. }

  56. void List::DisplayList()

  57. {

  58.         int num = 0;

  59.         employeeInfo* currNode = head;

  60.         while(currNode!=NULL)

  61.         {

  62.                 cout << "Employee Name :"<<currNode->name <<endl;

  63.                 cout << "Employee ID  :"<<currNode->id<<endl;;

  64.                 cout << "Age          :"<<currNode->department<<endl;

  65.                 currNode = currNode->next;

  66.                 num++;

  67. }

  68. }

  69. int List::DeleteNode(int x) {

  70.         employeeInfo* prevNode = NULL;

  71.         employeeInfo* currNode = head;

  72.         int currIndex = 1;

  73.         while (currNode && currNode->id != x) {

  74.                 prevNode = currNode;

  75.                 currNode = currNode->next;

  76.                 currIndex++;

  77.         }

  78.         if (currNode) {

  79.                 if (prevNode) {

  80.                         prevNode->next = currNode->next;

  81.                         delete currNode;

  82.                 } else {

  83.                         head = currNode->next;

  84.                         delete currNode;

  85.                 }

  86.                 return currIndex;

  87.           }

  88.         return 0;

  89. }

  90. void List::numEmployee()

  91. {

  92.     int numa=0;

  93.     int totald = 0;

  94.     int totalp = 0;

  95.     int totalm = 0;

  96.     string name ,department;

  97.     int id;

  98.     employeeInfo* currNode = head;

  99.     while(currNode!=NULL)

  100.     {

  101.     if(currNode->department =="design")

  102.     {

  103.     totald++;

  104.     }

  105.     if(currNode->department =="production")

  106.     {

  107.     totalp++;

  108.     }

  109.       if(currNode->department =="management")

  110.     {

  111.     totalm++;

  112.     }

  113.       currNode = currNode->next;

  114.       }

  115.       cout <<"Total employee in design department is :"<<totald<<endl;

  116.       cout <<"Total employee in production department is :"<<totalp<<endl;

  117.         cout <<"Total employee in management department is :"<<totalm<<endl;

  118.         cout <<"Total employees of all department is :"<<totald + totalp + totalm<<endl;

  119.     }

  120.     int List::FindNode(int y) {

  121.         employeeInfo* currNode = head;

  122.         int currIndex = 1;

  123.         while (currNode && currNode->id!= y) {

  124.                 currNode = currNode->next;

  125.                 currIndex++;

  126.         }

  127.         if (currNode)

  128.         {        cout << "Employee Name :"<<currNode->name <<endl;

  129.                 cout << "Employee ID  :"<<currNode->id<<endl;;

  130.                 cout << "Age          :"<<currNode->department<<endl;}

  131.         else{

  132.             cout <<"Employee not found"<<endl;

  133.                 return 0;

  134. }}

  135. void List::ValNode(int z,string dep) {

  136.         employeeInfo* currNode = head;

  137.         int currIndex = 1;

  138.         while (currNode && currNode->id!= z) {

  139.                 currNode = currNode->next;

  140.                 currIndex++;

  141.         }

  142.         if (currNode->department == dep)

  143.         {

  144.     cout <<"id incorrect";

  145.     }

  146.     else

  147.     {cout <<"Id correct";}

  148. }

  149. int main()

  150. {

  151.     int choice,logchc,id,dselect;

  152.     int numemploy = 0;

  153.     int select ;

  154.     int count = 0;

  155.     char name[20];

  156.     int eid;

  157.     string departmentl;

  158.     string Bpassword = "1234";

  159.     string Hpassword = "5678";

  160.     string password,department,decide;

  161.     List test;

  162.     test.InsertEmployee("Michael",1213,"design");

  163.     test.InsertEmployee("Steven",2345,"management");

  164.     test.InsertEmployee("Fernando",3456,"design");

  165.     test.InsertEmployee("Michelle",1213,"production");

  166.     test.InsertEmployee("Fenelix",1213,"design");

  167.     test.InsertEmployee("Michael",1213,"production");

  168.     label:

  169.     cout <<" 1.Log in as Board Director"<<endl;

  170.     cout <<" 2.Log in as Head of Department"<<endl;

  171.     cout <<"\nWhich priviledges do you wish to log in:"<<endl;

  172.     cin >>logchc;

  173.     if (logchc == 1)

  174.     {

  175.               cout <<"You had choose to login as Board of Director"<<endl;

  176.               cout <<"Please enter your password:";

  177.               while(password != Bpassword){

  178.               cin >> password;

  179.               if(password != Bpassword)

  180.               cout <<"Incorrect password ,please enter again :";

  181.               }

  182.                           system("cls");

  183.                           cout <<"Log in successfully"<<endl;

  184.                           test.numEmployee();

  185.                           cout <<"How many employees do you wish to layoff :";

  186.                           cin >> numemploy;

  187.                           cout <<"From which department:";

  188.                           cin >> departmentl;

  189.                           cout <<"You had decide to sack "<<numemploy<<" people from "<<departmentl<<" department."<<endl;

  190.                           cout <<"Information would further to Head of Department.In order to take action Head of department must be take action"<<endl;

  191.                           system("PAUSE");

  192.                           system("cls");

  193.                           goto label;

  194.                           }

  195.     if(logchc == 2)

  196.     {

  197.               cout <<"You had choose to login as Head of Department"<<endl;

  198.               cout <<"Please enter your password";

  199.               cin >> password;

  200.               while(password != Hpassword){

  201.               cin >> password;

  202.               if(password != Hpassword)

  203.               cout <<"Incorrect password ,please enter again :";

  204.               }

  205.               system("cls");

  206.               cout <<"Log in successfully"<<endl;

  207.               cout <<"\n";

  208.               labelb:

  209.               cout <<"1.Employ new employee"<<endl;

  210.               cout <<"2.Layoff employee"<<endl;

  211.               cout <<"3.Find employee"<<endl;

  212.               cout <<"4.View all Information of employee"<<endl;

  213.               cout <<"5.Enter your selection :"<<endl;

  214.               cin >> select;

  215.               if(select == 2)

  216.                         {

  217.                         cout <<"The Board of Director had decide to fire "<<numemploy<<" people from "<<departmentl<<" department"<<endl;

  218.                         cout <<"Insert the employee ID you wish to layoff :";

  219.                         cin >> eid;

  220.                         test.ValNode(eid,departmentl);

  221.                         cout <<"id from another department,please insert employee from "<<departmentl<<"department."<<endl;

  222.                           }}

  223.     getch();

  224.     return 0;

  225. }


The codes above shows a process of the employee management system.First
the Board of Director decide how many employees to layoff then from
which department(design,production,management).

After then log in the Head of department section and layoff the number
of employee as the decision of the Board of Director .But if the
employees from another department mistaken to be layoff by head of
department the mesage error would prompt out and forbid the
operation.So the test.ValNode(int ,string) function is the segment to
validate the employees to be layoff is matching with the Board of
Director decision,but it seems to cause error why ?

xpi0t0s 6Oct2009 16:50

Re: function cause error
 
What error does it cause, and on what line does the error occur?

low1988 6Oct2009 18:41

Re: function cause error
 
from line 162 to 167 I had initialise few employee in different department .If the board director choose to sack 2 people from design department , the head of department must enter the ids of 2 employees from design department in order to perform the fire operation.But if the id entered is not exist those employee that is preset from line 162 to 167 an error would prompt out during runtime

xpi0t0s 6Oct2009 19:35

Re: function cause error
 
So you've answered my question by pointing out lines that do not error and by not telling me what the error is.

So here's my answer. The problem with the code is not on line 107 and it's not the ++ operator that is causing the problem.


All times are GMT +5.5. The time now is 01:09.