Error in getting input in structure using gets()

12,709

Solution 1

Use this:

getchar();

before

 gets(th[i].Name);

to consume leading whitespaces and newlines, which were left in the buffer due to previous statements.

Also, I would recommend fgets, which is safer than gets as:

fgets(th[i].Name,30,stdin);

and

fgets(th[i].Qualifications,20,stdin);

Why gets is dangerous

Solution 2

First a remind for good and bad pratices :

  • thou shall not use gets - only fgets is good
  • thou shall not mix [f]gets and [f]scanf - only stick to one input mode

Now let's be serious.

gets tries to put in your input array as many characters as are present in input stream : it used to be the cause of countless memory violation. Never use it and only use fgets in you want to deal with line mode input (but do not forget that the input of fgets keeps the terminating \n)

[f]scanf is used for blank delimited fields. That is it reads a field until next blank character (at least space, tab, \r or \n) and leaves it in input stream. You can try to skip over that using a fgets and discarding what was read, or reading character by character until a \n. Avoid it because you got a number of bad answers advising to use a single getchar(). That's always the first try, and it works until you get an input with (unvisible) spaces before the newline - just try it and you will understand why it is a wrong solution.

So what should you do ? If input is line oriented, only use fgets, and then decode the input values with sscanf :

void teacher()
{    
 int t,i;
 char line[16];
 printf("Enter how many teachers are in department\n");
 fgets(line, sizeof(line), stdin);
 sscanf(line, "%d",&t);
 for(i=1;i<=t;i++)
 {       
   printf("Enter name of teacher : ");
   fgets(th[i].Name, 30, stdin);
   printf("Enter qualification of teacher : ");
   fgets(th[i].Qualifications, 30, stdin);
   printf("Enter experience_year of teacher : ");
   fgets(line, sizeof(line), stdin);
   sscanf(line, "%d",&th[i].experience_year);
 }
 ...

And in really good practices (in fact everything that could be used in real workd) you should always test the result of input functions (left as an exercise for the reader in above code :-) )

Solution 3

When you enter the number of teachers that is t you enter a \n after the integer which stays in your buffer and is accepted as input in gets statement. Therefore use a getchar after entering integer to solve the problem.

int t,i;
printf("Enter how many teachers are in department\n");
scanf("%d",&t);
getchar();

and add another getchar after the scanf in the for loop.

scanf("%d",&th[i].experience_year);
getchar();

Solution 4

Use:

#include < stdio.h > 
#include < conio.h >
void main()
{
    char name[3][100], add[3][100];
    int i;
    printf("ENTER YOUR DETAIl:");
    for (i = 0; i < 3; i++) {
        fflush(stdin);
        printf("ENTER NAME:  ");
        gets(name[i]);
        printf("ENTER ADDRESS: ");
        gets(add[i]);
    }
    for (i = 0; i < 3; i++) {
        printf("\n___________________\n");
        printf("\nNAME:%s", name[i]);
        printf("\Address:%s", add[i]);
        printf("\n___________________\n");
    }

    getch();
}

You can use this way in c programming. If your program does not take a string with spaces as input for multiple string type of input.

fflush(stdin);

This function will clear your input buffer.

Share:
12,709
udit043
Author by

udit043

Software Developer at Google Summer of Code (Debian) [April’16 - August’16] Love Free Style Codîng, like to work with OpenCV and love to automate everything (like Iron Man - Jarvis :p). Suggestions/Ideas are welcome : Github, Blog, Youtube, Facebook

Updated on June 14, 2022

Comments

  • udit043
    udit043 almost 2 years

    I am making a program to take input in teacher structure but there is unknown run time error , here is the code -

    #include <stdio.h>
    #include <conio.h>
    
    struct Teacher
    {
     char Name[30];
     char Qualifications[20];
     int experience_year;
    }th[10];
    
    void teacher()
    {    
     int t,i;
     printf("Enter how many teachers are in department\n");
     scanf("%d",&t);
     for(i=1;i<=t;i++)
     {       
       printf("Enter name of teacher : ");
       gets(th[i].Name);
       printf("Enter qualification of teacher : ");
       gets(th[i].Qualifications);
       printf("Enter experience_year of teacher : ");
       scanf("%d",&th[i].experience_year);
     }
     for(i=1;i<=t;i++)
     {
       printf("Details of %d teacher\n",i);
       printf(th[i].Name);
       printf(" ");
       printf(th[i].Qualifications);
       printf(" ");
       printf("%d",th[i].experience_year);
       printf("\n");
     }
    }   
    int main()
     {
       teacher();
       return 0;
     }
    

    Output is -

    Enter number of teacher
    1
    Enter teachers name : Enter qualification of teacher :
    

    How to get teacher's name(input).. and what is the error ?