For taking input in arrays until Enter is pressed

I am writing program in c++, the program is working with the first code when i use scanf instead of cin as below in first code, but it is not working when i use cin as in my second code.
First code (Working):-
#include
#include<stdio.h>
using namespace std;
int main()
{
int x;
cout<<“Enter element to be searched”<<endl;
cin>>x;
cout<<“Enter array”<<endl;
int arr[20],i;
char c;
for(int i=0;i<20;i++)
{
scanf("%d%c",&arr[i],&c);
if(c==’\n’)
break;
}
int n=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<n;i++)
{
if(arr[i]==x)
{
cout<<"Element is present at index "<<i;
break;
}
}
return 0;
}

Second code (Not working) :-
#include
#include<stdio.h>
using namespace std;
int main()
{
int x;
cout<<“Enter element to be searched”<<endl;
cin>>x;
cout<<“Enter array”<<endl;
int arr[20],i;
char c;
for(int i=0;i<20;i++)
{
cin>>arr[i]>>c;
if(c==’\n’)
break;
}
int n=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<n;i++)
{
if(arr[i]==x)
{
cout<<"Element is present at index "<<i;
break;
}
}
return 0;
}

please let me know, what mistake i am making.

cin skips blank spaces and endline characters. That’s why you are not getting the result you are looking for.

Also Google gives answer of every question. Check out this link :
c++ - tell cin to stop reading at newline - Stack Overflow