Hi guys, the following code gives me a wierd errror, which I cannot solve : at the first instance of s1->print() it works perfectly well,and at the second my program crashes, I will include the main file and all the called functions inside.
CODE
#include "Interface.h"
#include <iostream>
int main()
{
int x;
Shop *s1 = new Shop (512,"MaranShalita");
s1->addEmployee("Matan Erez",'A',231,15,4213);
s1->addEmployee("Mustafa Dirani",'B',550,57,921);
s1->addEmployee("Avi Rosefneld",'C',21412,77,510);
s1->addEmployee("David",'D',2451,92,214);
s1->addEmployee("Avi Rosenfeld",'C',21415,77,510);
s1->print();
std::cout<<"break 1"<<std::endl;
// i use this as a stop to see the output, this work perfectly.
std::cin>>x;
s1->removeEmployee(2451);
std::cout<<"succesfully removed employee"<<std::endl;
//here i get the runtime error.
s1->print();
// std::cout<<s1->getSumSalaries();
std::cout<<"break 2"<<std::endl;
return 0;
}
the methods used inside here are :
CODE
bool Shop::addEmployee(const char* name,const char type,const int Iid,const
int perce,const int pay)
{
if((type <= 'D')&&(type >= 'A')&&(perce>=1)&&(perce<=100))
{
if(findEmployee(Iid) == -1)
{
int j,save;
Employee** tempo;
Employee* temp;
switch (type)
{
case 'A':temp = new Manager(name,Iid,perce,pay,1);
break;
case 'B':temp = new ExperiencedSeller (name,Iid,perce,pay);
break;
case 'C':temp = new NoviceSeller (name,Iid,perce,pay);
break;
case 'D':temp = new RegisterMan (name,Iid,perce,pay);
break;
default:
break;
}
//both creating the new array AND increasing number_of_employees by one.
tempo = new Employee* [number_of_employees];
tempo=Employees;
save = findEmployeePlace(Iid);
number_of_employees++;
Employees = new Employee* [number_of_employees];
if (!save)//if save is 0
{
Employees[0] = temp;
for (j = 1;j<number_of_employees;j++)
{
Employees[j] = tempo[j];
}
}
else
{
for (j = 0;j<save;j++)
{
Employees[j] = tempo[j];
}
Employees[save] = temp;
for (j=save+1;j<number_of_employees;j++)
{
Employees[j]=tempo[j];
}
}
return true;
}
else
std::cout<<"error not unique"<<std::endl;
return false;
}
else
std::cout<<"input error"<<std::endl;
return false;
}
CODE
void Shop::print()
{
std::cout<<"Number of shop :"<<shop_id<<std::endl;
std::cout<<"The employees :"<<std::endl;
for (int i=0;i<number_of_employees;i++)
std::cout<<Employees[i]->get_id()<<"\n";
if (number_of_deals>0)
{
std::cout<<"Deals done in this shop :";
for (int i=0;i<number_of_deals;i++)
std::cout<<"Deal #"<<i+1<<": "<<deals_done[i]<<std::endl;
if (number_of_cancels>0)
{
std::cout<<"Deals cancelled in this shop :";
for (int i=0;i<number_of_cancels;i++)
std::cout<<"Cancel #"<<i+1<<":"<<deals_canceled[i]<<std::endl;
}
else
std::cout<<"No cancels done"<<std::endl;
}
else
std::cout<<"No deals done.\nNo cancels done"<<std::endl;
}
CODE
bool Shop::removeEmployee(const int Iid)
{int place=findEmployee(Iid);
//I may do this since C++ works with short circuit evaluation of logical
//expressions - that is if place==-1 than it will never get to the checking
//the type of the Employee.
if (place!=-1&&(Employees[place]->get_type()!='C'))
{
//2.5. 3acllocating a new temporary array.
Employee** temp=new Employee* [number_of_employees];
temp=Employees;
number_of_employees--;
Employees=new Employee* [number_of_employees];
for (int i=0;i<place;i++)
{
Employees[i]=temp[i];
}
for (int i=place+1;i<number_of_employees;i++)
Employees[i-1]=temp[i];
return true;
}
else
return false;
}
CODE
Shop::Shop(const int Iid,const char* Name)
{
deals_done = NULL;
deals_canceled=NULL;
Employees = NULL;
network = new char [strlen(Name)+1];
strcpy(network,Name);
shop_id = Iid;
number_of_cancels=0;
number_of_deals = 0;
number_of_employees = 0;
}