I want to read array of floats from stdin and store them in values array, at the same time calculating how many arrays are there in the input.
Given:
-1.0 2.0 1e-1
3.2 2.1 5.6
-1.0 2.0 3e-2
I should get:
arrays = 3
However, this code to catch \n
is not working and I do not have a clue how to count rows and assign floats simultaneously. I need to know when \n
appears so I could separate arrays apart.
#include <stdio.h>
int main()
{
int i=0;
int arrays=0;
double values[100];
char c;
while(scanf("%lf, %[\n]c", &values[i], &c) != EOF){ // i want to catch \n into c variable
i++;
if(c=="\n"){
arrays++;
break;
}
}
return 0;
}
Now I got to this:
#include <stdio.h>
int main()
{
int count = 0;
char *line;
char *data;
size_t len = 0;
int i=0;
double array[100];
while ((getline(&line, &len, stdin)) != -1){
printf("%c",line[0]);
count++;
while(sscanf(line,"%lf",&array[i])==1){
i++;
}
}
return 0;
}
Do you see the mistake there? It will get one line and end itself.