I am a novice and for my school programming exercise I need to make integer array from specified input. Input look like this:
1000: { 250, 500, 750 } (enter)
My very basic code is capable to scan only numbers separated with whitespace.
#include <stdio.h>
#include <stdlib.h>
#define LEN 10
long int array[LEN];
int main()
{
long int i;
for (i =0; i < LEN; i++)
{
scanf("%li", &array[i]);
}
return 0;
}
I have a static array and I need to fill it with numbers in {} brackets. The number before ":" symbol (1000 in this case) I could scan as a single variable or as a 0th element of array. Sould I use some modified scanf? But I think the way here is some while cycle with scanf. Sometimes is array bigger than amout of given numbers so I need to end cycle with "}" symbol. Thanks for ideas.
Consider using
fgets
to read a line. Parse the line withsscanf
. The%n
specifier will report the number of characters processed by the scan. Accumulating those values will allow for iterating through the line.