Hi, I'm a beginner at programming and I'm having a bit of trouble trying to delete elements in a dynamic array of objects. I tried to delete elements by shifting that particular element and the ones that follow over one element and then assigning the last one to NULL. Can you guys give me some pointers on what I should change. I would really appreciate it. Thanks.
You are not using the loop variable j correctly while assignment inside the inner for but using k in place of it. Also I believe you should use i in the expression of the upper for loop instead of k and in many places inside the for. The whole appearance of k seems to be redundant, what does it represent to you? You should first move the elements one place lower in the array and after that set the last element to null, moreover do that even though the input was not the last element i.e. do it in every case. Also you are printing invalid entry if the input is not the id of the first records[ 0 ] and again if it is not the id of records[ 1 ] and so on - invalidity check should be done after the whole checking and only if we found out that the id was not the id of any record I believe. You do not want to decrease amount of records when moving the elements but only when removing one. There is no point in breaking out from the loop after every input check - the second break is wrong as well, it is not inside the else without braces. Specify the point of the while and the second cin >> input, are you exactly trying to loop there asking new record to be removed again and again?
Line 18 doesn't belong. You don't want to decrease the number of records for each record moved.
The break on line 24 is not governed by the else on line 22, as your indentation suggests it is. It is executed every iteration of the loop (which means you don't actually have a loop there.) Of course, you also break inside the if block, so I suppose you wouldn't have a loop there regardless.
Thank you for the feedback. I took a break and looked at it again and realized much of it didn't make sense. The only reason I had the if(input == records[numberOfRecords-1].id) statement was because if the user picks the last element, I can't use the (records[k] = records[k+1]) shifting method for that.