You're simulating the behavior of std::vector here.
It keeps track of both the size = number of elements used, and the capacity = the number of elements allocated. The max value of size = capacity.
I'd recommend using 2 variables to keep track of this. Trying to use just 1 (int i) may be making things harder.
Also, read the new value into a temp variable before copying it to the array, in case the new value = -1.
int main()
{
int cap = 5;// initial capacity
int sz = 0;// initial size
int* pArr = newint[cap];// allocate for capacity
int n;/ the temp variable for reading into
while( cin >> n && n != -1 )// read in the number and bail if n = -1
{
if( sz == cap )// array is full. Resize
{
cap *= 2;// double the capacity
int* arrayPtr2 = newint[cap];// new array with double capacity
// copy the existing sz elements from the old array to the new one
// delete the old array
// assign pArr to point to the new array
}
// good to go! Assign the new value and increment sz.
}
// other code to use/display the array contents
// free the memory
}
It's all good. I reply to threads because I enjoy it.
It's certainly more interesting than watching tv!
The blank screen may just be the program waiting for input at line 19.
Line 19 is intended to replace the input routine you have on lines 11-16, which actually won't work very well because it's throwing away every value for n except the last one.
So, just remove lines 11-16 from the code. The point of line 19 is that it allows any number of entries (not limited to 5) until a -1 is entered.
Your memcopy call should work, but 2 params are wrong.
How many ints are to be copied? 100? There are only sz ints in the array at that point.
Also, the copy source isn't new int, it's pArr (the array to be copied from).
Same re. line 27. It should be like line 48.
ok i changed everything you said to. thank you so much! it's perfect now. you explained it in a great way because even i could understand it quickly when usually it takes me a long time. have you considered teaching comp sci on the side? or tutoring? i think you'd be great at it. you saved my grade on this homework :)
You're welcome for the help. Thank's for the complement.
I have done some tutoring, but I can't work as a teacher (though I may know enough to teach C++ at the beginner level) because I lack any formal CS education or certificates and I have no employment experience in the field. I'm actually just a lowly construction worker.
Alas, this shall remain just a hobby.
really?! i never would have guessed. well you seem to be really good at your hobby! i feel like college is overrated sometimes. you're clearly a great programmer despite lacking CS education. i sometimes struggle with homework like this for hours when i get stuck. if i get stuck again do you accept private messages on here? you're one of the best tutors on here in my opinion because when others explain i just don't get it. is it okay if the next time i'm struggling i private message you the program and the trouble i'm having?
You may PM if you wish, though I prefer to field problems in the forums.
Most of the regulars here know far more than I do overall, and a fair fraction are actual professionals.
Given your low post count, you probably haven't much feel yet for the replies you'll get here.
They do vary somewhat though, for sure.