I have a simple question. I am trying to store a list of numbers into an array using a while loop.
For example, let's say the size of the array is 5.
If I input: 1 2 3 4 5 and press enter, there won't be any problem
However, if the size of the array is 10 and I input: 1 2 3 4 5 6 7 8 9 10 then it doesn't work and skip the lines afterwards.
I searched but can't find any answers. Is it just not possible to enter too long a list of numbers in one line separated by spaces using cin? Do I have to do it like 1 [enter] 2 [enter]...10 [enter]?
Any help is appreciated.
int n=1,key,i;
int arra [n];
cout << "Please enter the size of array (no more than 100): ";
cin >> n;
while (n>100)
{
cout << "Please enter a number no more than 100: ";
cin >> n;
}
cout << "Please enter " << n << " numbers separated by a space and ended with Enter key: \n";
for (i=0;i<n;i++) // store numbers into array
cin >> arra[i];
cout << "Please enter the key number that you want to find out: ";
cin >> key;
if (search(arra,n,key)==1)
cout << "Yes, the key is in the array. \n";
else
cout << "No, the key is not in the array. \n";