. I increase the array size by making the size a variable. This is an example
Code:
//Making a dynamic array based on a variable
int size = 0;
int* iArray = new int[size]
//Since the array size is based on the variable, we can change variable to resize the array
//can we not?
if(//the user wanna add new employee)
size += 1;
this is the new code i forgot to post
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 = 0;
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:
IncreaseSlots(&NOE);
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;
break;
case 2:
for(int i = 0; i < NOE; ++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;
}
}