i want to read space separated integer into an array and when i press enter it should stop reading at any point of time, how to implement loop for this prog
Method 1: Use the return value of scanf() and enter a character once done(Your question doesn't need this as reading till newline will not work but this is one of the ways)
int d;
while(scanf("%d",&d) == 1)
{
arr[i] = d;
i++;
}
Method 2: Use fgets()
the read the line and parse the line using strok()
and atoi()
as shown
char arr[100];
fgets(arr,sizeof(arr),stdin);
char *p =strtok(arr, " ");
while(p != NULL)
{
int d = atoi(p);
arr[i++] = d;
p = strtok(NULL," ");
}