#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Scanf and fgets issue!


    Hello guys, I'm a begginer in C...

    My program needs to read the name than 2 values for each name...

    When I use fgets it just read 1 name then it keep reading the numbers like that:

    john
    12
    35
    24
    22


    what I need is:

    John
    24
    21
    Maria
    56
    43
    Peter
    22
    89


    The result I need comes when I use the scanf, but I need to do it with the fgets...

    Here is the code:

    #include <stdio.h>

    int main(){
    int i=0;


    typedef struct funcionario {
    char nome[100];
    float s1;
    float s2;
    } funcionario;

    funcionario func[100];

    for (i=0; i<100; i++){
    fgets(func[i].nome,100,stdin);
    if((func[i].nome[0]=='F') && (func[i].nome[1]=='I') && (func[i].nome[2]=='M')){
    break;
    }
    scanf("%f", &func[i].s1);
    scanf("%f", &func[i].s2);
    }

    printf("%f\n", func[0].s1);
    printf("%f\n", func[0].s2);
    printf("%f\n", func[1].s1);
    printf("%f\n", func[1].s2);
    printf("%f\n", func[2].s1);
    printf("%f\n", func[2].s2);

    system("pause");
    return 0;
    }



    The printfs are just to test the processing.

    helpppp *-*
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    26
    Rep Power
    0
    First put tag around your code to retain format so you can easily read it
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i=0;
        typedef struct funcionario 
        {
        char nome[100];
        float s1;
        float s2;
        }funcionario;
    
        funcionario func[100];
    
        for (i=0; i<100; i++){
        fgets(func[i].nome,100,stdin);
        if((func[i].nome[0]=='F') && (func[i].nome[1]=='I') && (func[i].nome[2]=='M')){
        break;
        }
        scanf("%f", &func[i].s1);
        scanf("%f", &func[i].s2);
        }
    
        printf("%f\n", func[0].s1);
        printf("%f\n", func[0].s2);
        printf("%f\n", func[1].s1);
        printf("%f\n", func[1].s2);
        printf("%f\n", func[2].s1);
        printf("%f\n", func[2].s2);
    
        system("pause");
        return 0;	
    }
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,385
    Rep Power
    1871
    The easiest thing to do is to use fgets for everything.
    Mixing scanf and fgets on the same stream just leads to confusion.

    So for example, do something like
    Code:
    char temp[100];
    fgets(temp,sizeof(temp),stdin);
    sscanf(temp,"%d", &func[i].s1);
    // ditto for s2
    It might look long winded, but by the time you made scanf bomb-proof, you would end up with pretty much the same amount of code either way.

    Separating input (fgets) from conversion (sscanf) allows you to treat errors at either stage in a meaningful way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo