Hi to all, I have a homework about a banking system. I try to adding a branch in an array and the size of array dynamically increased. Here is my code but it gives run time error.
#include "BankingSystem.h"
#include "Branch.h"
#include <iostream>
using namespace std;
BankingSystem::BankingSystem(){
size=0;
branches=NULL;
}
void BankingSystem:: addBranch( const int branchId, const string branchName ){
Branch b= Branch(branchId, branchName);
if(size==0){
size++;
branches=new Branch[size];
branches[0]=b;
cout <<"Branch "<< b.getBranchId()<< " has been added."<<endl;
}
else{
for(int i=0;i<size;i++)
if(branches[i].getBranchId()==b.getBranchId()){
cout<<"Branch "<<b.getBranchId()<<" already exists."<<endl;
return;
}
int newsize= size++;
Branch *temp=new Branch[newsize];
for(int i=0;i<size;i++){
temp[i]=branches[i];
}
temp[size]=b;
cout <<"Branch "<< b.getBranchId()<< " has been added."<<endl;
delete[] branches;
branches=temp;
}
}