In this problem, I have to create a dynamic array and get data from the user, If the user keep entering value, a new array should be created and the data of the old array should be added to new array. and the old array should be deleted. I am not able to print the data from the new array. It is only printing garbage values. can you check where the problem is ?
#include<iostream>
using namespace std;
int main() {
int size = 5;
int* myarrptr = new int[size];
int* newarr = 0;
int count = 0;
int count2 = 0;
int i;
for (i = 0; i <= size; i++) {
if (i == size) {
count = *((myarrptr + i) - 1);
newarr = new int[size*2];
for (i = 0; i < size; i++) {
*(newarr + i) = *(myarrptr+i);
}
size = size + size;
delete[] myarrptr;
myarrptr = newarr;
*(myarrptr + i) = count;
}
cin >> *(myarrptr + i);
if (*(myarrptr+i) == -1) {
break;
}
count2++;
}
delete[] myarrptr;
cout << newarr[3];
}
Here is the code
I am trying to print the data from the newarr but I am getting garbage values.