1

I am unable to put the value in the first element of the array. It's always asking to put the value in array second element.

#include<stdio.h>
#include<string.h>

int main(void)
{
    int a, i;
    char names[50][50];
    
    printf("\nEnter the number of names you want :");
    scanf("%d", &a);

    for(i = 0; i < a; i++)
    {
        printf("\n%d name :", i);
        gets(names[i]);
    }
    
    printf("\nThe required name lists :");
    for(int i = 0; i < a; i++)
    {
        printf("\n%d name :", i+1);
        puts(names[i]);
    }
    return 0;
}
Shubham
  • 1,153
  • 8
  • 20
  • 3
    Never *ever* use the `gets` function. It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it even have been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Aug 08 '20 at 11:38
  • 1
    On an unrelated note: Output to `stdout` (which is where `printf` will write) is by default *line buffered*, which means the output will be written to the terminal on newline. Therefore you should make it a habit to only use *trailing* newlines when printing. The leading newline in your code will cause the *previous* line to be written. – Some programmer dude Aug 08 '20 at 11:41

2 Answers2

2

As scanf leaves behind a dangling newline character \n it causes the gets(Use fgets) to not wait for the input from the user. Try flushing the input buffer by using getchar.

Update: Added mechanism to remove the trailing \n registered by the fgets

#include<stdio.h>
#include<string.h>
int main()
{
    int a,i;
    printf("Enter the number of names you want: ");
    scanf("%d",&a);
    //Flush the input buffer
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    char names[50][50];
    for(i=0;i<a;i++)
    {
        printf("%d name: ",i);
        fgets(names[i],50,stdin); //Use fgets instead of gets
        // To remove th \n registed by the fgets
        char *p;
        if ((p = strchr(names[i], '\n')) != NULL)
           *p = '\0';
    }
    printf("The required name lists: \n");
    for(int i=0;i<a;i++)
    {
       printf("%d name: ",i+1);
       puts(names[i]);
  
    }
    return 0;
}

Reference:

  1. Remove newline skipped by scanf
  2. Remove newline registered by fgets
Sourabh Choure
  • 723
  • 4
  • 15
  • 2
    Instead of having that long mechanism, you can use `scanf(" %50s", names[i]);` It'll automatically flush out any trailing whitespace character. In other words, it'll not register the previous `\n` character from input buffer. – Shubham Aug 08 '20 at 15:32
0

Put this after line scanf("%d",&a), as a workaround,

    char c;
    scanf("%c",&c);

Also use fgets(names[i],50,stdin) instead of gets(names[i])

Note: You get warning when you use gets in your code, as it is always assumes a consistent input from user. More explanation over here Why is the gets function so dangerous that it should not be used?

another_CS_guy
  • 682
  • 6
  • 7