i keep getting garbage value on one of the indexes in the dynamic array when i try to remove a value which was entered by user from a list of elements in the dynamic array.
used pointer as function parameters and replaced the value to be removed with 0 and by using a counter and for loop tried to skip all the 0s but in place of zero theres a garbage value.
#include<iostream>
#include<fstream>
using namespace std;
int size = 0;
int final = 0;
int* read(ifstream& a){
    int temp;
    a.open("data(1).txt");
    while (!a.eof()){
        a >> temp;
        size++;
    }
    a.close();
    a.open("data(1).txt");
    int* arr = new int[size];
    for (int i = 0; i < size; i++)
        a >> arr[i];
    return arr;
}
int* remove(int* a,int search){
    for (int i = 0; i < size; i++){
        if (a[i] == search)
            a[i] = 0;
        else final++;
    }
    int* change = new int[final+1];
    for (int i = 0; i < size; i++){
        if (a[i] > 0){
            change[i] = a[i];
        }
        else continue;
    }
    delete[] a;
    a = nullptr;
    return change;
}
int main(){
    int* ptr = nullptr;
    int num;
    cout << "please enter the number to remove: ";
    cin >> num;
    ifstream in;
    ptr=read(in);
    ptr=remove(ptr, num);
    for (int i = 0; i < final; i++)
        cout << ptr[i] << " ";
    cout<<endl;
    system("pause");
    return 0;
}
 
    