READ THISInput just like this, you will see the error
1 2 3 4 5 6 7 8 9 10 11 12 13
Step 1:
+ choose 1: Input your menu: 1
+ Please input number of animal: 1
+ Input option: 1.COW - 2.SHEET: 1
+ Then Input the Information of the Cow. No output this time => check a look Step 2.
+ Input your menu: 1
Step 2:
+ choose 1: Input your menu: 1
+ Please input number of animal: 1
+ Input option: 1.COW - 2.SHEET: 2
+ Then Input the Information of the Sheet.
+ Input your menu: 2 // This time output => the error appear.
You seem to have a few typos; did you actually retype all of the code rather than copy/paste? I don't know what type of animal a "sheet" is. Sheep maybe?
Anyway, check your loop boundary and increment. Array should start at zero with indexes up to num-1. You put the first one in at index "num" and the second at index "num+n", but then try to output by accessing index 0 and index 1.
Also, I do not see where you initialize num to anything other than zero. It needs to be at least number of animals in size or you are attempting to read or write memory outside of the array bounds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int n;
arrayanimal = new CAnimal*[num];
for (int i = num; i < num + n; i++) {
}
num = num + n;
// does not match with output loop
for (int i = 0; i < num; i++) {
arrayanimal[i]->Output();
}
Just because the program didn't crash, doesn't mean it's correct.
You need to allocate space for all the elements in the array. In your case, the array is num + n, so you must allocate that much space. It's a serious error to overwrite the bounds of an array.
So the declaration should be: arrayanimal = new CAnimal*[num + n];