0

Here the gets function is not advancing as the i is increasing from 0 to 3. I want to store data for 3 emps, but when the for loop started it takes the gets statement takes the input but as i increases 1,2 the gets function is not taking the input the next line of code is displayed without execution of gets statement.


   #include <stdio.h>
   struct emps
   {
       char name[20];
       char lic_no[10];
   };
   int main()
   {
       struct emps e[3];
       int a;
   
       for (int i = 0; i < 3; i++)
       {
           printf("enter your name\n");
           gets(e[i].name);
           printf("enter the license number\n");
           scanf("%s", &e[i].lic_no);
       }
       return 0;
   }
  • https://stackoverflow.com/questions/22226529/program-doesnt-execute-gets-after-scanf-even-using-fflushstdin etc. – KamilCuk May 10 '21 at 10:09
  • can you plz tell me where I am wrong why the gets function is not executed alon with the loop – Piyush Jaswal May 10 '21 at 10:16
  • 1
    https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer https://stackoverflow.com/questions/2366509/input-in-c-scanf-before-gets-problem https://stackoverflow.com/questions/25629256/in-while-loop-scanf-gets-skipped-second-time https://stackoverflow.com/questions/59994428/scanf-with-the-space-after-d-not-working-before-gets – KamilCuk May 10 '21 at 10:19
  • Don't use `scanf("%s"...` with `gets()`. Best to use `fgets()` for input and then lop off the `'\n'`. – chux - Reinstate Monica May 10 '21 at 12:12
  • 1
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv May 10 '21 at 13:33

0 Answers0