using sscanf and fgets

I'm trying to use fgets and sscanf to get up to 20 numbers from a user in one line.

I tried doing two things. Using an array of floating points, and using a pointer to that array. The pointer gave me 0's and the array by itself didn't compile. Is there a way to use a for loop for sscanf instead of having to type %f%f%f%f%f%f 20 times for each instance in the array?

Any help/advice appreciated, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
int main()
{
    int i=0;
    float floater1[20];
    float *floatpointer=floater1;
    char line[100];
    printf("Please enter up to 20 numbers in one line\n");
    scanf("%f", floater1);
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%f %f", floatpointer, floatpointer+1);
    printf("%f %f", floatpointer, floatpointer+1);
}

#include <stdio.h>
int main()
{
    int i=0;
    float floater1[20];
    //float *floatpointer=floater1;
    char line[100];
    printf("Please enter up to 20 numbers in one line\n");
    scanf("%f", floater1);
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%f %f", floater1[0], floater[1]);
    printf("%f %f", floater[0], floater[1]);
}
Try:
int num = scanf("%f %f %f %f", &floater[0], &floater[1], &floater[2], &floater[3]);
num should have the number of items.
Read:
http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf
Topic archived. No new replies allowed.