10 Replies - 253 Views - Last Post: 18 December 2012 - 06:20 PM Rate Topic: -----

#1 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Problem with Departments and Professors...

Posted 18 December 2012 - 06:58 AM

I need help in displaying the total salaries for all the employees listed not just by departments...

else if (in==5)
		{
			//cout<<"Which Department do want?\n1 ,2 ,3\n";
			//int dep;
			//cin>>dep;
			//if (dep==1)These lines not need if I want to only total salary. 				         One+Two+Three.Display_Total_Salary();//???Correct?
			//else if (dep==2)  
				//Two.Display_Total_Salary();
			//else if (dep==3)
				//Three.Display_Total_Salary();
			Sleep(2000);
			system("cls");
		}


This is what I have...
void Department::Display_Total_Salary()
{
	float sum=0;
	if (isEmpty())
		cout<<"The list is empty\n";
	node *curr=List.head;
	while (curr)
	{
		sum=sum+curr->salary;
		curr=curr->next;
	}
	cout<<"The total Salary is "<<sum<<endl;
}



I also need to found out how to show the man_name in the "Display all Employee"


void Department::display()
{
	node *curr=List.head;
	if (isEmpty())
		cout<<"The list is empty\n";
	else
	{
		while (curr)
		{
			cout<<"Department:"<<Deptt_id<<endl;
			cout<<"ID:"<<curr->emp_id<<endl<<"Name:";
			/*int n=strlen(curr->arr);*/
			for (int i=0;i<15;i++)
				cout<<curr->arr[i];
			cout<<"\nSalary:"<<curr->salary<<"\nAge:"<<curr->age<<endl<<endl<<endl;
			curr=curr->next;
		}
	}
}



cout<<"4. Display All Employee Records\n";

Consist of the Professors that are assigned to each departments.



This is my full code.
Header file

#ifndef CLASS_H
#define CLASS_H
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
struct node
{
	
	int emp_id;
	char arr[100];
	float salary;
	int age;
	node *next;
	
};
class Employee
{
public:
	node *head;
	Employee()
	{
		head=new node;
		head->emp_id=0;
		for (int i=0;i<100;i++)
			head->arr[i]='\0';
		head->salary=0;
		head->age=0;
	}
};
class Department
{
private:
	int Deptt_id;
	Employee List;
	string man_name;
	int size;
public:
	Department(int id,string name)
	{
		man_name=name;
		Deptt_id=id;
		List.head=NULL;
		size=0;
	}
	void add_emp(node *emp);
	bool delete_emp(int emp);
	void update_emp(int emp);
	bool search_by_id(int emp);
	node * search_by_name(char arr[100]);
	void sort();
	void display();
	void display_emp(char arr[100]);
	void Display_Total_Salary(); 
	void set_deptt_id(int id);
	bool isEmpty();
	void load_data();
	void save_data();


};
#endif



Class.cpp file

#include <iostream>
#include"Class.h"


void Department::set_deptt_id(int id)
{
	Deptt_id=id;
}
node *Department::search_by_name(char arr[15])
{
	if (isEmpty())
	{
		cout<<"The list is empty\n";
		return NULL;
	}
	else
	{
	node *curr=List.head;
		while(curr)
		{
			if (strcmp(curr->arr,arr)==0)
				return curr;
			curr=curr->next;
		}
	return NULL;
	}
}
void Department::display_emp(char arr[15])
{
	node * emp=search_by_name(arr);
	if (emp!=NULL)
	{
		cout<<"Department:"<<Deptt_id<<endl;
		cout<<"ID:"<<emp->emp_id<<endl<<"Name:";
		int n=strlen(arr);
		for (int i=0;i<n;i++)
			cout<<arr[i];
		cout<<"\nSalary:"<<emp->salary<<"\nAge:"<<emp->age<<endl;
	}
	else
	{
		cout<<"The employee with name";
		int n=strlen(arr);
		for (int i=0;i<n;i++)
			cout<<arr[i];
		cout<<" doesn't exists\n";
	}
}
void Department::add_emp(node *emp)
{
	if (List.head==NULL)
	{	
		List.head=emp;
		List.head->next=NULL;
		size++;
	}
	else
	{
		node *curr=List.head;
		node *prev=NULL;
		while (curr)
		{
			prev=curr;
			curr=curr->next;
		}
		curr=new node;
                cout<<"Department:"<<Deptt_id<<endl;
		curr->emp_id=emp->emp_id;
		curr->salary=emp->salary;
		//int n=strlen(emp->arr);
		for (int i=0;i<15;i++)
			curr->arr[i]='\0';
		for (int i=0;i<15;i++)
			curr->arr[i]=emp->arr[i];
		curr->age=emp->age;
		prev->next=curr;
		curr->next=NULL;
		size++;
	}
}
bool Department::isEmpty()
{
	if (size==0)
		return true;
	else return false;
}
bool Department::delete_emp(int emp)
{
	if (isEmpty())
	{
		cout<<"The list is empty\n";
		return false;
	}


	else
	{
		node *curr=List.head;
		node *prev=NULL;
		while (curr)
		{
			if (curr->emp_id==emp)
			{
				prev->next=curr->next;
				delete curr;
				size--;
				return true;
			}
			prev=curr;
			curr=curr->next;
		}
		return false;
	}
}
void Department::update_emp(int emp)
{
	if (isEmpty())
		cout<<"The list is empty\n";
	node *curr=List.head;
	bool flag=false;
	while (curr)
	{
		if (curr->emp_id==emp)
		{
			flag=true;
                        cout<<"Department:"<<Deptt_id<<endl;
			cout<<"ID:"<<curr->emp_id<<endl<<"Name:";
			int n=strlen(curr->arr);
			for (int i=0;i<n;i++)
				cout<<curr->arr[i];
			cout<<"\nSalary:"<<curr->salary<<"\nAge:"<<curr->age<<endl;
			while(1)
			{
				cout<<"1.Change the name\n2.Change the salary\n3.Change the age\n4.Go Back To main menu\nEnter your choice:";
				int in=0;
				cin>>in;
				if (in==1)
				{
					for (int i=0;i<15;i++)
						curr->arr[i]='\0';
					cout<<"Enter the new Name:";
					cin.ignore();
					cin.getline(curr->arr,15,'\n');
				}
				else if (in==2)
				{
					cout<<"Enter the new Salary:";
					cin.ignore();
					cin>>curr->salary;
				}
				else if (in==3)
				{
					cout<<"Enter the new Age:";
					cin.ignore();
					cin>>curr->age;
				}
				else if (in==4)
					break;
				else
				{
					cout<<"Wrong choice enter again\n";
					Sleep(2000);
					system("cls");
				}
			}
		}
		curr=curr->next;
	}
	if (!flag)
		cout<<"The employee with ID "<<emp<<" not found\n";
}
void Department::display()
{
	node *curr=List.head;
	if (isEmpty())
		cout<<"The list is empty\n";
	else
	{
		while (curr)
		{
			cout<<"Department:"<<Deptt_id<<endl;
			cout<<"ID:"<<curr->emp_id<<endl<<"Name:";
			/*int n=strlen(curr->arr);*/
			for (int i=0;i<15;i++)
				cout<<curr->arr[i];
			cout<<"\nSalary:"<<curr->salary<<"\nAge:"<<curr->age<<endl<<endl<<endl;
			curr=curr->next;
		}
	}
}
void Department::sort()
{
	node *temphead=List.head;
	node *curr1=NULL;
	for (int j=0; j<size; j++)
	{
		int tempid;
		char tempname[15];
		float tempsalary;
		int tempage;
		while (temphead->next)  //iterate through list until next is null
		{
			if (temphead->emp_id > temphead->next->emp_id)
			{
				tempid = temphead->emp_id;
				temphead->emp_id = temphead->next->emp_id;
				temphead->next->emp_id = tempid;


				tempsalary = temphead->salary;
				temphead->salary = temphead->next->salary;
				temphead->next->salary = tempsalary;


				tempage = temphead->age;
				temphead->age = temphead->next->age;
				temphead->next->age = tempage;


				//int n=strlen(temphead->arr);
				for (int i=0;i<15;i++)
					tempname[i] = temphead->arr[i];
				for (int i=0;i<15;i++)
					temphead->arr[i] = temphead->next->arr[i];
				for (int i=0;i<15;i++)
					temphead->next->arr[i] = tempname[i];
					temphead = temphead->next;//increment node
			}
			else 
				temphead = temphead->next;//increment node
		}	
		temphead = List.head;//reset temphead
	}


}
void Department::Display_Total_Salary()
{
	float sum=0;
	if (isEmpty())
		cout<<"The list is empty\n";
	node *curr=List.head;
	while (curr)
	{
		sum=sum+curr->salary;
		curr=curr->next;
	}
	cout<<"The total Salary is "<<sum<<endl;
}
void Department::load_data()
{
	ifstream file;
	if (Deptt_id==1)
	file.open("Dept 1.txt");
	else if (Deptt_id==2)
		file.open("Dept 2.txt");
	else if (Deptt_id==3)
		file.open("Dept 3.txt");
	int i=0;
	if (file.fail())
		cout<<"File didn't open\n";
	else
	{
		while (!file.eof())
		{
			int id;
			char name[15];
			float salary;
			int age;
			if (i==0)
			{
				file.ignore();
				i++;
			}
			file>>id;
			file.ignore();
			for(int i=0;i<15;i++)
				name[i]='\0';
			file.getline(name,15,'\n');
			file>>salary;
			file>>age;
			node *newnode=new node;
			newnode->age=age;
			for (int i=0;i<15;i++)
				newnode->arr[i]=name[i];
			newnode->emp_id=id;
			newnode->salary=salary;
			add_emp(newnode);
			sort();
		}
	}
	file.close();
}
void Department::save_data()
{
	ofstream file;
	if (Deptt_id==1)
	file.open("Dept 1.txt");
	else if (Deptt_id==2)
		file.open("Dept 2.txt");
	else if (Deptt_id==3)
		file.open("Dept 3.txt");
	node *curr=List.head;
	if (!isEmpty())
	{
		while (curr)
		{
			file<<curr->emp_id<<endl;
			int n=strlen(curr->arr);
			for (int i=0;i<n;i++)
				file<<curr->arr[i];
			if (curr->next)
			file<<endl<<curr->salary<<endl<<curr->age<<endl;
			else
				file<<endl<<curr->salary<<endl<<curr->age;
			curr=curr->next;
		}
	}
	file.close();
}


Main File
#include "Class.h"
void main()
{
	int id=1;
	Department One(1,"Professor XSSSX");
	Department Two(2,"Professor WEEE");
	Department Three(3,"Professor XXX");
	One.load_data();
	Two.load_data();
	Three.load_data();
	int in;
	while (1)
	{
		cout<<"                     Main Menu\n";
		
		
		cout<<"1. Add an Employee Record\n";
		cout<<"2. Delete an Employee Record\n";
		cout<<"3. Update an Employee Record\n";
		cout<<"4. Display All Employee Records\n";
		cout<<"5. Display Total Salary for All Employees\n"; 
		cout<<"6. Search Employee by Name and Display his/her record.\n";
		cout<<"7. Save and Exit\n";
		cout<<"\nEnter your choice:"; 
		cin>>in;
		if (in==1)
		{    
            cout<<"Which Department do want?\n1 ,2 ,3\n";
			int dep;
			cin>>dep;
			node * newnode=new node;
			cout<<"Enter the ID of Employee:";
			cin>>newnode->emp_id;
			cout<<"Enter the Name of Employee:";
			//int n=strlen(newnode->arr);
			for (int i=0;i<15;i++)
				newnode->arr[i]='\0';
			cin.ignore();
			cin.getline(newnode->arr,15,'\n');
			cout<<"Enter the Salary of Employee:";
			cin>>newnode->salary;
			cout<<"Enter the Age of Employee:";
			cin>>newnode->age;
			if (dep==1)
			{
				One.add_emp(newnode);
				One.sort();
			}
			else if (dep==2)
			{
				Two.add_emp(newnode);
				Two.sort();
			}
			else if (dep==3)
			{
				Three.add_emp(newnode);
				Three.sort();
			}
			Sleep(2000);
			system("cls");
		}
		else if (in==2)
		{
			cout<<"Which Department do want?\n1 ,2 ,3\n";
			int dep;
			cin>>dep;
			cout<<"Enter the ID of employee to be deleted:";
			int id;
			cin>>id;
			bool flag;
			if (dep==1)
				flag=One.delete_emp(id);
			else if (dep==2)
				flag=Two.delete_emp(id);
			else if (dep==3)
				flag=Three.delete_emp(id);
			if (!flag)
				cout<<"The Employee	with id "<<id<<" not found in the list\n";
			else
				cout<<"The Employee	with id "<<id<<" deleted from the list\n";
			Sleep(2000);
			system("cls");
		}
		else if (in==3)
		{
			cout<<"Which Department do want?\n1 ,2 ,3\n";
			int dep;
			cin>>dep;
			cout<<"Enter the ID of Employee to be updated:";
			int n;
			cin>>n;
			if (dep==1)
			One.update_emp(n);
			else if (dep==2)
				Two.update_emp(n);
			else if (dep==3)
				Three.update_emp(n);
			Sleep(2000);
			system("cls");
		}
		else if (in==4)
		{
			cout<<"Which Department do want?\n1 ,2 ,3\n";
			int dep;
			cin>>dep;
			if (dep==1)
				One.display();
			else if (dep==2)
				Two.display();
			else if (dep==3)
				Three.display();
			Sleep(5000);
			system("cls");
		}
		else if (in==5)
		{
			//cout<<"Which Department do want?\n1 ,2 ,3\n";
			//int dep;
			//cin>>dep;
			//if (dep==1)
				One+Two+Three.Display_Total_Salary();
			//else if (dep==2)
				//Two.Display_Total_Salary();
			//else if (dep==3)
				//Three.Display_Total_Salary();
			Sleep(2000);
			system("cls");
		}
		else if (in==6)
		{
			
			cout<<"Enter the name of Employee:";
			char name[15];
			cin.ignore();
			cin.getline(name,15,'\n');
			//if (dep==1)...not needed
				One.display_emp(name);
			//else if (dep==2)
				Two.display_emp(name);
			//else if (dep==3)
				Three.display_emp(name);
			Sleep(2000);
			system("cls");
		}
		else if (in==7)
		{
			//if (dep==1)  **not needed..
				One.save_data();
			//else if (dep==2)
				Two.save_data();
			//else if (dep==3)
				Three.save_data();
			break;
			Sleep(2000);
			system("cls");
		}
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Departments and Professors...

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 07:53 AM

This would be much easier if you were using a vector, or an array of your Department class.

Please explain what you are trying to accomplish with this line:
               One+Two+Three.Display_Total_Salary();


Jim
Was This Post Helpful? 0
  • +
  • -

#3 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 08:11 AM

View Postjimblumberg, on 18 December 2012 - 07:53 AM, said:

This would be much easier if you were using a vector, or an array of your Department class.

Please explain what you are trying to accomplish with this line:
               One+Two+Three.Display_Total_Salary();


Jim



I am trying to add the departments' employees' salaries together...vs. having to ask which department you want to see the salary for.
In the menu I had for in==5
cout<<"5. Display Total Salary for All Employees\n";

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 08:24 AM

You must call the function for every instance that you want to total. This is really where a vector/array and a loop would help a lot.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 09:01 AM

How about this? where would I put this?

double totalsalary()
  {
    double sum=0;
    while(curr)
    node*curr =List.head;
    sum=sum+curr->salary;
    curr=curr->next;
  }
  return sum;




I was thinking of replacing the void Department::Display_Total_Salary()

with

void Department::totalSalary(double)


else if(in==5)
{
cout<<"Total Salary of all departments listed would be "<<One.totalSalary()+Two.totalSalary()+Three.totalSalary()<<endl;
}

This post has been edited by amyceres: 18 December 2012 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 09:29 AM

I recommend that you modify this function: void Department::Display_Total_Salary() to return a value, the total, then just call this function for each of your Department instances to compute the totals.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 09:40 AM

void Department::Display_Total_Salary()
{
	float sum=0;
	if (isEmpty())
		cout<<"The list is empty\n";
	while (curr)
	{
		node *curr=List.head;
                sum=sum+curr->salary;
		curr=curr->next;
	}
	return sum;
}




else if(in==5)
{
cout<<"Total Salary of all departments listed would be "<<One.Display_Total_Salary()+Two.Display_Total_Salary()+Three.Display_Total_Salary()<<endl;
}



I think this should work....hopefully it's correct me if I am wrong...

This post has been edited by amyceres: 18 December 2012 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 09:43 AM

Quote

I think this should work....hopefully it's correct me if I am wrong...

Nope, you can't return something from a function returning void.
void Department::Display_Total_Salary()


Also you should probably skip the rest of the function if there is nothing in the list.

I still recommend using a vector/array for your Departments.

Jim

This post has been edited by jimblumberg: 18 December 2012 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 10:09 AM

I going to have to do a separate function for this...I think
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 11:13 AM

Why? Can't you just change the return type from void to float then return the sum?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: Problem with Departments and Professors...

Posted 18 December 2012 - 06:20 PM

View Postjimblumberg, on 18 December 2012 - 11:13 AM, said:

Why? Can't you just change the return type from void to float then return the sum?

Jim

Yup I did ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1