You are taking the input upto 5 that’s the problem there it could be anything 1,2 even100000. You
have to take input untill you don’t find a stopping point .
More precisely You have to take input of array without knowing the array size .You should read this to know more about this
You are taking user input continuously and then checking for the conditions, but the statement is to check condition for every input as soon as it is entered. while(true){ cin>>x; if(....) break; else cout<<x;