It is program for target sum triplets. it is not showing any outputs.please correct the code?

#include
#include
using namespace std;
int main() {
int x,i,n,target;

cin >> n;
int a[n];
if(n>=1 && n<=1000)
   {
        for(i=0; i<n; i++)
        {
            cin>> a[i]; 
        }
        sort(a,a+n);
        cin>> target;
            for(int i=0;i<n-2;i++)
            { x= 0;
                 for(int j=i+1;j<n;j++)
                 {
                    int* beg=&a[j];
                    int* end=&a[n-1];
                    x=a[i]+*beg+*end;
                    while(beg<end)
                        {
                            if(x > target)
                                {
                                    end--;
                                }
                            if(x == target)
                            {   
                                cout<<a[i]<<","<<*beg <<" and "<<*end;
                                cout<<endl;
                                beg++;
                                end--;
                            }
                            else
                                beg++;

                        }
 
                 }
            }
    }
   
return 0;

}