-3

I created a structure of size n & started taking input, but everytime i run it won't take input for name if i use only one gets(). This code only works if I use two gets() both above & below my print statement.

#include <stdio.h>
    
    struct Driver
    {
        char name[50];
        char licence[50];
        char route[50];
        int kms[6];
    };
    
    int main()
    {   
        int n, response;
        printf("WELCOME TO MY DRIVING AGENCY\n");
        printf("\nEnter the no. of data you want to enter - ");
        scanf("%d", &n);
        struct Driver a[n];
        for(int i = 0; i < n; i++) // Take inputs
        {   
            printf("\nEnter the information for Driver no.%d\n", i+1);
            gets(a[i].name); 
            printf("Enter name - "); // WHY WHENEVER I REMOVE ANY OF THE get(a[i].name) IT WON'T TAKE INPUT FOR name
            gets(a[i].name);
            printf("Enter licence no. - ");
            gets(a[i].licence);
            printf("Enter final destination (route) - ");
            gets(a[i].route);
            printf("Enter distance travelled in kms (Enter no. only) - ");
            scanf("%d", &a[i].kms);
        }
        return 0;
    }

Why this code is not working with only one get(a[i].name)

  • Don't use gets never never never & ever ,please read this :https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – MED LDN Dec 19 '20 at 18:33
  • 1
    Solution: read every input with `fgets()` and extract an integer with `sscanf()` or `strtol()` or `atoi()`. But also see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Dec 19 '20 at 18:38
  • You can use 'fgets' function.Example: fgets(name of string,length of string,stdin); – MED LDN Dec 19 '20 at 18:42
  • @WeatherVane "and extract an integer with strtol." Only ever strtol. Not atoi, and not sscanf either. – zwol Dec 19 '20 at 19:05
  • @zwol are they deprecated? – Weather Vane Dec 19 '20 at 19:06
  • @WeatherVane Not officially, but atoi silently ignores syntax errors and sscanf has undefined behavior on input overflow, so they can't be used in a robust parser, and by me that means never use them at all. – zwol Dec 19 '20 at 19:12

1 Answers1

-1

You have a mistake with creating structure variable. You can create fix-length array using constant or dynamic variable.
I changed your code as following. It was working well.

#include <stdio.h>

#define  n 5

struct Driver
{
    char name[50];
    char licence[50];
    char route[50];
    int kms[6];
};


int main()
{   
    int response;
    printf("WELCOME TO MY DRIVING AGENCY\n");
    printf("\nEnter the no. of data you want to enter - ");
    //scanf("%d", &n);
    Driver a[n];
    for(int i = 0; i < n; i++) // Take inputs
    {   
        printf("\nEnter the information for Driver no.%d\n", i+1);
        gets(a[i].name); 
        printf("Enter name - "); // WHY WHENEVER I REMOVE ANY OF THE get(a[i].name) IT WON'T TAKE INPUT FOR name
        gets(a[i].name);
        printf("Enter licence no. - ");
        gets(a[i].licence);
        printf("Enter final destination (route) - ");
        gets(a[i].route);
        printf("Enter distance travelled in kms (Enter no. only) - ");
        scanf("%d", &a[i].kms);
    }
    return 0;

In this code, scanf() acted correctly.

Thanks.

popstar555
  • 16
  • 2