function cause error

Discussion in 'C++' started by low1988, Oct 6, 2009.

  1. low1988

    low1988 New Member

    Joined:
    Sep 11, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    
    [LIST=1]
    [*]#include <iostream>
    [*]#include <conio.h>
    [*] 
    [*] 
    [*]using namespace std;
    [*] 
    [*]struct employeeInfo
    [*]{
    [*]       string name;
    [*]       int id;
    [*]       string department;
    [*]       employeeInfo *next;
    [*]};
    [*] 
    [*] 
    [*]class List {
    [*]	public:
    [*]		List(void) { head = NULL; } // constructor
    [*]		~List(void); // destructor
    [*]		bool IsEmpty() { return head == NULL; }
    [*] 
    [*]		employeeInfo* InsertEmployee(string ,int ,string );
    [*]		void DisplayList(void);
    [*]		void numEmployee();
    [*]		int DeleteNode(int x);
    [*]		int FindNode(int y);
    [*]		void ValNode(int,string);
    [*] 
    [*]  	 private:
    [*]		employeeInfo* head;
    [*]};
    [*] 
    [*]List::~List(void) {
    [*]	employeeInfo* currNode = head, *nextNode = NULL;
    [*]	while (currNode != NULL) {
    [*]		nextNode = currNode->next;
    [*]		// destroy the current node
    [*]		delete currNode;
    [*]		currNode = nextNode;
    [*]	}
    [*]}
    [*] 
    [*]employeeInfo* List::InsertEmployee(string nama,int number,string affiliate) {
    [*]	int currIndex = 0;
    [*]	employeeInfo* currNode = head;
    [*]	employeeInfo* prevNode = NULL;
    [*]	while (currNode) {
    [*]		prevNode = currNode;
    [*]      currNode = currNode->next;
    [*]		currIndex++;
    [*]	}
    [*] 
    [*]   employeeInfo* newNode = new employeeInfo;
    [*]	newNode->name = nama;
    [*]	newNode->id=number;
    [*]	newNode->department= affiliate;
    [*]	if (currIndex == 0) {
    [*]		newNode->next = head;
    [*]		head = newNode;
    [*]  	 } else {
    [*]		newNode->next = prevNode->next;
    [*]		prevNode->next = newNode;
    [*]   	}
    [*] return newNode;
    [*]}
    [*] 
    [*] void List::DisplayList()
    [*]{
    [*]	int num = 0;
    [*]	employeeInfo* currNode = head;
    [*]	while(currNode!=NULL)
    [*]	{
    [*]		cout << "Employee Name :"<<currNode->name <<endl;
    [*]		cout << "Employee ID   :"<<currNode->id<<endl;;
    [*]		cout << "Age           :"<<currNode->department<<endl;
    [*]		currNode = currNode->next;
    [*]		num++;
    [*]}   
    [*]}
    [*] 
    [*]int List::DeleteNode(int x) {
    [*]	employeeInfo* prevNode = NULL;
    [*]	employeeInfo* currNode = head;
    [*]	int currIndex = 1;
    [*]	while (currNode && currNode->id != x) {
    [*]		prevNode = currNode;
    [*]		currNode = currNode->next;
    [*]		currIndex++;
    [*]	}
    [*]	if (currNode) {
    [*]		if (prevNode) {
    [*]			prevNode->next = currNode->next;
    [*]			delete currNode;
    [*]		} else {
    [*]			head = currNode->next;
    [*]			delete currNode;
    [*]		}
    [*]		return currIndex;
    [*]   	}
    [*]	return 0;
    [*]}
    [*]void List::numEmployee()
    [*]{
    [*]    int numa=0;
    [*]    int totald = 0;
    [*]    int totalp = 0;
    [*]    int totalm = 0;
    [*]    string name ,department;
    [*]    int id;
    [*]    employeeInfo* currNode = head;
    [*]    while(currNode!=NULL)
    [*]    {
    [*] 
    [*]    if(currNode->department =="design")
    [*]    {
    [*]     totald++;
    [*]     }
    [*] 
    [*]    if(currNode->department =="production")
    [*]    {
    [*]     totalp++;
    [*]     }
    [*]      if(currNode->department =="management")
    [*]    {
    [*]     totalm++;
    [*]     }
    [*]      currNode = currNode->next;
    [*]      }
    [*]      cout <<"Total employee in design department is :"<<totald<<endl;
    [*]       cout <<"Total employee in production department is :"<<totalp<<endl;
    [*]        cout <<"Total employee in management department is :"<<totalm<<endl;
    [*]        cout <<"Total employees of all department is :"<<totald + totalp + totalm<<endl;
    [*] 
    [*]     }
    [*] 
    [*]     int List::FindNode(int y) {
    [*]	employeeInfo* currNode = head;
    [*]	int currIndex = 1;
    [*]	while (currNode && currNode->id!= y) {
    [*]		currNode = currNode->next;
    [*]		currIndex++;
    [*]	}
    [*]	if (currNode)
    [*]	{	cout << "Employee Name :"<<currNode->name <<endl;
    [*]		cout << "Employee ID   :"<<currNode->id<<endl;;
    [*]		cout << "Age           :"<<currNode->department<<endl;}
    [*]	else{
    [*]	    cout <<"Employee not found"<<endl;
    [*]		return 0;
    [*]}}
    [*] 
    [*]void List::ValNode(int z,string dep) {
    [*]	employeeInfo* currNode = head;
    [*]	int currIndex = 1;
    [*]	while (currNode && currNode->id!= z) {
    [*]		currNode = currNode->next;
    [*]		currIndex++;
    [*]	}
    [*]	if (currNode->department == dep)
    [*]	{
    [*]    cout <<"id incorrect";
    [*]    }
    [*]    else 
    [*]    {cout <<"Id correct";}
    [*]}
    [*] 
    [*]int main()
    [*]{
    [*]    int choice,logchc,id,dselect;
    [*]    int numemploy = 0;
    [*]    int select ;
    [*]    int count = 0;
    [*]    char name[20];
    [*]    int eid;
    [*]    string departmentl;
    [*] 
    [*]    string Bpassword = "1234";
    [*]    string Hpassword = "5678";
    [*]    string password,department,decide;
    [*] 
    [*]    List test;
    [*]    test.InsertEmployee("Michael",1213,"design");
    [*]    test.InsertEmployee("Steven",2345,"management");
    [*]    test.InsertEmployee("Fernando",3456,"design");
    [*]    test.InsertEmployee("Michelle",1213,"production");
    [*]    test.InsertEmployee("Fenelix",1213,"design");
    [*]    test.InsertEmployee("Michael",1213,"production");
    [*] 
    [*]    label: 
    [*]    cout <<" 1.Log in as Board Director"<<endl;
    [*]    cout <<" 2.Log in as Head of Department"<<endl;
    [*] 
    [*]    cout <<"\nWhich priviledges do you wish to log in:"<<endl;
    [*]    cin >>logchc;
    [*]    if (logchc == 1)
    [*]    {
    [*]               cout <<"You had choose to login as Board of Director"<<endl;
    [*]               cout <<"Please enter your password:";
    [*]               while(password != Bpassword){
    [*]               cin >> password;
    [*]               if(password != Bpassword)
    [*]               cout <<"Incorrect password ,please enter again :";
    [*]               }
    [*]                           system("cls");
    [*]                           cout <<"Log in successfully"<<endl;
    [*]                           test.numEmployee();
    [*]                           cout <<"How many employees do you wish to layoff :";
    [*]                           cin >> numemploy;
    [*]                           cout <<"From which department:";
    [*]                           cin >> departmentl;
    [*]                           cout <<"You had decide to sack "<<numemploy<<" people from "<<departmentl<<" department."<<endl;
    [*]                           cout <<"Information would further to Head of Department.In order to take action Head of department must be take action"<<endl;
    [*] 
    [*]                           system("PAUSE");
    [*]                           system("cls");
    [*]                           goto label;
    [*]                          }
    [*] 
    [*]     if(logchc == 2)
    [*]     {
    [*]               cout <<"You had choose to login as Head of Department"<<endl;
    [*]               cout <<"Please enter your password";
    [*]               cin >> password;
    [*]               while(password != Hpassword){
    [*]               cin >> password;
    [*]               if(password != Hpassword)
    [*]               cout <<"Incorrect password ,please enter again :";
    [*]               }
    [*]               system("cls");
    [*]               cout <<"Log in successfully"<<endl;
    [*]               cout <<"\n";
    [*]               labelb:
    [*]               cout <<"1.Employ new employee"<<endl;
    [*]               cout <<"2.Layoff employee"<<endl;
    [*]               cout <<"3.Find employee"<<endl;
    [*]               cout <<"4.View all Information of employee"<<endl;
    [*]               cout <<"5.Enter your selection :"<<endl;
    [*]               cin >> select; 
    [*] 
    [*]              if(select == 2)
    [*]                         {
    [*]                         cout <<"The Board of Director had decide to fire "<<numemploy<<" people from "<<departmentl<<" department"<<endl;
    [*] 
    [*] 
    [*]                         cout <<"Insert the employee ID you wish to layoff :";
    [*] 
    [*]                         cin >> eid;
    [*]                        test.ValNode(eid,departmentl);
    [*]                        cout <<"id from another department,please insert employee from "<<departmentl<<"department."<<endl;
    [*] 
    [*]                          }}
    [*] 
    [*] 
    [*]    getch(); 
    [*]    return 0;
    [*]}
    [*]
    [/LIST]
    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 ?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What error does it cause, and on what line does the error occur?
     
  3. low1988

    low1988 New Member

    Joined:
    Sep 11, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice