This code keeps giving me an error after I input the array size.
I am using Windows btw. Something in the code is stopping the run time,
I cannot see what it is. Thanks !
int size;
cout << "Array Capacity ? "; cin >> size;
int A[size];
You cant do this. If you want this to work. You'd have to allocate memory.
1 2 3
int size;
cout << "Array Capacity ? "; cin >> size;
int* A = newint[size];
Edit: Next time, please provide the errors you get. Fixing this code makes the program run fine on my IDE. Also, dont forget to delete the the array once you're done.
As far as I know. An array size has to be consistant. For example.
1 2
int x = 5;
int y[x];
wont work. It has to be
1 2
constint x = 5;
int y[x];
So thats why, when you try to fill the array size with user input, the input is not const and wont work.
What I did, was simply allocate memory. Which basically means. As far as I know, the program recognizes how big size is and then makes enough space for it in the memory section, which makes it work. Again, I dont know how it works behind the scenes since Im not an expert. But feel free to google it because there are lots of answers there. Its called the new operator, or just google c++ memory allocation. Also, since you allocated memory,you'd have to delete it, otherwise you will get memory leak. I should mention aswell, if you prefer watching videos, there are lots of videos on youtube explaining this aswell.