Jake Muller Jake Muller - 1 month ago 11
C++ Question

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

I want to insert multiple integer values less than 10.
Example:


010011


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);
i++;
} while(temp != '\n');

cout<<endl;
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.

Answer

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]);