I want to have a user enter numbers separated by a space and then store each value as an element of an array. Currently I have:
while ((c = getchar()) != \'\
Small change to your code: only increment i
when you read the space:
while ((c = getchar()) != '\n')
{
if (c != ' ')
arr[i] = arr[i] * 10 + c - '0';
else
i++;
}
Of course, it's better to use scanf
:
while (scanf("%d", &a[i++]) == 1);
providing that you have enough space in the array. Also, be careful that the while
above ends with ;
, everything is done inside the loop condition.
As a matter of fact, every return value should be checked.