Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,642 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,239 people online right now. Registration is fast and FREE... Join Now!




A runtime error which I did not manage to solve, need help.

 
Reply to this topicStart new topic

A runtime error which I did not manage to solve, need help.

Sepanto
post 7 May, 2008 - 09:22 AM
Post #1


D.I.C Head

Group Icon
Joined: 20 Mar, 2008
Posts: 96



Dream Kudos: 50
My Contributions


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;
}
User is offlineProfile CardPM

Go to the top of the page


skaoth
post 7 May, 2008 - 10:51 AM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 337



Thanked 9 times

Dream Kudos: 100
My Contributions


Your best bet for getting better help is to provide code that will compile. This probably means zipping up your project and attaching it to this thread. IMHO its easier to run through the debugger to see problems than trying to piece together the code segments.

Otherwise from your description of the problem, I would guess that removeEmployee() is failing
User is offlineProfile CardPM

Go to the top of the page

Sepanto
post 7 May, 2008 - 12:20 PM
Post #3


D.I.C Head

Group Icon
Joined: 20 Mar, 2008
Posts: 96



Dream Kudos: 50
My Contributions


Ok lol, but it's a long code lol.


Attached File(s)
Attached File  Shop_driver.zip ( 5.19k ) Number of downloads: 14
User is offlineProfile CardPM

Go to the top of the page

Sepanto
post 7 May, 2008 - 01:02 PM
Post #4


D.I.C Head

Group Icon
Joined: 20 Mar, 2008
Posts: 96



Dream Kudos: 50
My Contributions


NOTE: I found my problem, it was in the loop in removeEmployee, I skipped an index, and as such I got a segmentation fault while calling get_id() with Employees[placeRemoved]
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 06:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month