How allocate dinamic an array in real time?

Hi I liked of allocate dinamicing an array in real time, but be giving error! This is my code:

#include <iostream>

using namespace std;

int main()
{
int x, i = 0;
int *a = nullptr;

cout <<"";

while(cin >> x && cin.peek() != '\n'){
a = new int[i];
cout << x << " ";
a[i] = x;
i++;
}
cout<< x <<" \nEND\n" <<endl;

for(int j = 0; j < i; j++){
cout<< a[j] << " ";
}

delete[] a;
a = nullptr;
return 0;
}
Last edited on
There is an 'a' right next to the right bracket that should not be there:

1
2
3
for(int j = 0; j < i; j++){
cout<< a[j] << " ";
}a


Also you are creating an array with zero length.Maybe you need to check this out as well.
Sorry..... I repaired the code
Topic archived. No new replies allowed.