Jake Muller Jake Muller - 5 months ago 29
C++ Question

C++ - Insert multiple integers without space separated in one line

I want to insert multiple integer values less than 10.


Each digit of above integer inputs is treated as single integer value.

I've tried this:

int i=0, j=0;
int arr[10000];
char temp;
do {
scanf("%d%c", &arr[i], &temp);
} while(temp != '\n');

for(j=0; j<i; j++) {
printf("%d ", arr[j]);

But this code won't accept multiple integers in one line if it's initiated with 0(zero). How to solve this? Thank you.


You can specify a (maximum) length for a scan code. To read a single-digit int you can use the following:

scanf("%1d", &arr[i]);