-
fgets && sscanf
code:
struct Base
{
float Tocke;
struct Base *next;
};
// this code is in the function
float Points1;
printf("Enter points: ");
fgets(Points1, strlen(Points1), stdin);
sscanf(Points1, "%f", (*NEW)->Points);
Could anyone tell me why this code is not working?
-
fgest reads a string so you need a tempary character array
Code:
char temp[MAX]; /* MAX size of input */
float Points1;
printf("Enter points: ");
fgets(temp, sizeof(temp)-1, stdin);
sscanf(temp, "%f", &Points);
you also need to read sizeof - 1 as fgets leaves the newline on the string and adds the NULL to the end, so you array needs to have an extra element
-
This is working. But if I do this with pointers than it is not working. How to do it with pointers?
-
When you use the structure pointer in the sscanf you have to use the & in front of it.
Code:
sscanf(temp, "%f", &struct_PTR->Points);
That should work, it does for me.
:)