Thread: Why this kind of error after normal execution?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    Why this kind of error after normal execution?

    I tried to program a simple data base for employees. Everytime u wanna add a new employee, the dynamic array goes up in size and add one more empty slot for future addition.
    I compiled the code and ran it, after adding 17 employees in the array and showing em on the screen, the next allocation screws up the program!!

    this is the code :

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <string>
    
    
    using namespace std;
    
    class Employee
    {
    public:
    	//Default Constructor 
    	Employee();
    	//Constructor
    	Employee(string name, int age, int salary);
    	//Destructor
    	~Employee();
    
    	//Methods
    	void EditInfo();
    	void EditName();
    	void EditAge();
    	void EditSalary();
    
    	//Accessors
    	string GetName();
    	int	   GetAge();
    	int    GetSalary();
    
    	//Data Members
    private:
    	string m_Name;
    	int	   m_Age;
    	int	   m_Salary;
    };
    
    //Default Constructor
    Employee::Employee()
    {
    }
    
    //Constructor
    Employee::Employee(string name, int age, int salary): m_Name(name), m_Age(age), m_Salary(salary)
    {
    }
    
    //Destructor
    Employee::~Employee()
    {
    }
    
    //Edititing information function 
    void Employee::EditInfo()
    {
    	string sTemp;
    	int    iTemp;
    
    	cout << "Enter the name: "; cin >> sTemp;
    	cout << endl;
    	m_Name = sTemp;
    
    	cout << "Enter the age: "; cin >> iTemp;
    	cout << endl;
    	m_Age = iTemp;
    
    	cout << "Enter the salary: "; cin >> iTemp;
    	m_Salary = iTemp;
    }
    
    //Accessor to m_Salary
    int Employee::GetSalary()
    {
    	return m_Salary;
    }
    
    //Accessor to m_Age 
    int Employee::GetAge()
    {
    	return m_Age;
    }
    
    //Accessor to m_Name
    string Employee::GetName()
    {
    	return m_Name;
    }
    
    //Edit m_Salary
    void Employee::EditSalary()
    {
    	int temp;
    	cout << "Enter the new salary: "; cin >> temp;
    	m_Salary = temp;
    }
    
    //Edit m_Age
    void Employee::EditAge()
    {
    	int temp;
    	cout << "Enter the new age: "; cin >> temp;
    	m_Age = temp;
    }
    
    //Edit m_Name
    void Employee::EditName()
    {
    	string temp;
    	cout << "Enter the new name: "; cin >> temp;
    	m_Name = temp;
    }
    
    
    void IncreaseSlots(int* NumOfEmps);
    void CleanDataBase(Employee** DB, int* NumOfEmps);
    
    
    int main()
    {
    	int NOE = 1;
    	Employee** p_DB = new Employee*[NOE];
    	if(!p_DB[NOE])
    	{
    		cout << "Error: Data Base could not be created!" << endl;
    		cout << "No enough memory for allocation!" << endl;
    		return 0;
    	}
    
    	bool quit = false;
    	while(!quit)
    	{
    		cout << "What do you want to do: " << endl;
    		cout << "1)Add Employee" << endl;
    		cout << "2)Show Data Base" << endl;
    		cout << "3)Clear Data Base" << endl;
    		cout << "4)Quit the program" << endl;
    		int choice;
    		cin >> choice;
    		cout << endl;
    		switch(choice)
    		{
    		case 1:
    			if(NOE == 1)
    			{
    				p_DB[NOE-1] = new Employee();
    				if(!p_DB[NOE-1])
    				{
    					cout << "Error: no enough memory for allocation!";
    					return 0;
    				}
    				p_DB[NOE-1]->EditName();
    				cout << endl;
    				IncreaseSlots(&NOE);
    			}
    			else
    			{
    				p_DB[NOE-1] = new Employee();
    				if(!p_DB[NOE-1])
    				{
    					cout << "Error: no enough memory for allocation!";
    					return 0;
    				}
    				p_DB[NOE-1]->EditName();
    				cout << endl;
    				IncreaseSlots(&NOE);	
    			}
    			break;
    
    		case 2:
    			for(int i = 0; i < (NOE-1); ++i)
    			{
    				string name = p_DB[i]->GetName();
    				cout << name;
    				cout << endl;
    			}
    			cout << NOE;
    			cout << endl;
    			break;
    
    		case 3:
    			
    			cout << "Data Base cleared!" << endl;
    			CleanDataBase(p_DB, &NOE);
    
    		case 4:
    			quit = true;
    		}
    	}
    
    	CleanDataBase(p_DB, &NOE);
    	
    	char q;
    	cin >> q;
    	return 0;
    }
    
    void IncreaseSlots(int* NumOfEmps)
    {
    	*NumOfEmps += 1;
    }
    
    void CleanDataBase(Employee** p_DB, int *NumOfEmps)
    {
    	if(p_DB)
    	{
    		for(int i = 0; i < *(NumOfEmps); ++i)
    			if(p_DB[i])
    			{
    				delete [] p_DB[i];
    				p_DB[i] = 0;
    			}
    			delete p_DB;
    			p_DB = 0;
    	}
    }
    PS: What is wrong with "ClearDataBase" function??
    Last edited by Hussain Hani; 06-30-2007 at 05:22 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run-time execution for recursive functions.
    By Kempelen in forum C Programming
    Replies: 8
    Last Post: 03-26-2009, 04:00 AM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. difference between register int and normal int
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-25-2003, 04:01 PM