Jake Muller Jake Muller - 1 year ago 82
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.

Answer Source

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]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download