unable to take input of strings from user

  Kiến thức lập trình

I have to make an array of structure for cricketers presenting their name, age etc. and take input of each value from user . I am trying to get value of name from the user but my code editor keeps on skipping that line i have tried gets , scanf with %s , scanf with %[^n]s and fgets nothing works the whole line gets skipped . I want to take full name as input.

#include<stdio.h>
#include<string.h>
#define name_len 20

int main (){
    printf("enter no of players :");
    int n;
    scanf("%d",&n);
    typedef struct cricketers {
        char name[name_len];
        int age;
        int testMatches;
        float avgRuns;
    } players;
    players arr[n];

    for (int i = 0; i < n; i++)
    {
        printf("enter name of the player indexed %d",i);
        fgets(arr[i].name,name_len,stdin);
        printf("enter age of the player indexed %d",i);
        scanf("%d",&arr[i].age);
        printf("enter no of test matches played by the player indexed %d",i);
        scanf("%d",&arr[i].testMatches);
        printf("enter avg runs in each test math of the player indexed %d",i);
        scanf("%f",&arr[i].avgRuns);
    }
    

    return 0;
}

pliz help ????????

i already tried this and that fgets func too

gets(arr[i].name);
scanf("%s",arr[i].name);
scanf("%[^n]s",arr[i].name);
scanf("%[^n]",arr[i].name);

New contributor

Harshit Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT