C: Scanf string with field skipper "%*" applied on conversion specifier in while loop -


i have defined structure

typedef struct emp {     char name[100];     int id;     float salary; } emp; 

and use in while-loop input

emp emprecs[3]; int i;  = 0; while (i < 3) {     printf("\nenter name: ");     scanf("%*[\n\t ]%[^\n]s", emprecs[i].name);     printf("\enter id: ");     scanf("%d", &emprecs[i].id);     printf("\nenter salary: ");     scanf("%f", &emprecs[i].salary);     i++; } 

but loop takes first name , skips other input after (it finishes, empty input). example c textbook, problem?

it works better without "%*[\n\t ]" field skip, textbook tells use it.

try this

scanf(" %*[\n\t ]%[^\n]s", emprecs[i].name);       ^^^      white space 

instead of

scanf("%*[\n\t ]%[^\n]s", emprecs[i].name); 

also,

scanf(" %d", &emprecs[i].id);  scanf(" %f", &emprecs[i].salary); 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -