0

So, this was my project:

You manage a travel agency and you want your n drivers to input their following details:

  1. Name
  2. Driving License No
  3. Route
  4. Kms
  5. Your program should be able to take n as input(or you can take n=3 for simplicity) and your drivers will start inputting their details one by one.

Your program should print details of the drivers in a beautiful fashion. User structures.

Here is my solution:

#include <stdio.h>

 struct driver_details
{
    char name[50];
    char licence_no[25];
    char route[20];
    char kms[5];
};

void displayDetails(struct driver_details arr)
{
    printf("\t1. Name of the driver: %s\n",arr.name);
    printf("\t2. Driving License Number: %s\n",arr.licence_no);
    printf("\t3. Route followed: %s\n",arr.route);
    printf("\t4. Total Kilometers travelled: %s\n",arr.kms);
}

int main()
{
    int n;
    printf("Enter the number of drivers:\n");
    scanf("%d",&n);
    struct driver_details arr[n];
   
    for (int i = 0; i < n; ++i)
    {
        printf("Enter the detail of driver number: %d\n",(i+1));

        printf("Enter your name:\n");
        gets(arr[i].name);

        printf("Enter your driving license number:\n");
        gets(arr[i].licence_no);

        printf("Enter your route:\n");
        gets(arr[i].route);

        printf("Enter the kilometers travelled:\n");
        gets(arr[i].kms);

        printf("\n");
    }

    for (int i = 0; i < n; ++i)
    {
        printf("The details of the driver %s:\n",arr[i].name);
        displayDetails(arr[i]);
    }
    return 0;
}

I was expecting it to take the user details one by one and then print them accordingly. But, when I ran my program, this was observed:

Enter the number of drivers:
1
Enter the detail of driver number: 1
Enter your name:
Enter your driving license number:
User
Enter your route:
xyz-abd
Enter the kilometers travelled:
2345

The details of the driver :
        1. Name of the driver:
        2. Driving License Number: User
        3. Route followed: xyz-abd
        4. Total Kilometers travelled: 2345

As shown, it totally skips the name attribute and I have tried various methods but they didn't work. What should I do?

P.S : I am not that well-versed in structures

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • When asking a question here please remove anything not *strictly* necessary to illustrate the problem from your code. Here there's a ton of stuff relating to fields that aren't impacted by your problem. – tadman Apr 11 '23 at 14:59
  • 1
    You should never ever use `gets`. It is considered dangerous and was deprecated and even removed from standard decades ago. Use `fgets` instead. – Gerhardh Apr 11 '23 at 15:05

2 Answers2

1

The function gets is unsafe and is not supported by the C Standard. Do not use it. Instead use standard function fgtes or scanf.

The problem with your code is that after this call of scanf

scanf("%d",&n);

the input buffer contains the new line character '\n' that corresponds to the pressed key Enter.

As a result the next call of gets reads an empty string.

You should remove the new line character from the input buffer.

If to use scanf instead of gets then you can write for example like

scanf("%d",&n);
struct driver_details arr[n];

for (int i = 0; i < n; ++i)
{
    printf("Enter the detail of driver number: %d\n",(i+1));

    printf("Enter your name:\n");
    scanf( " %49[^\n]", arr[i].name);
    //...

and so on using similar calls of scanf.

Pay attention to the leading space in the format string. It allows to skip white space characters in the input buffer.

If to use fgets then you can write

scanf("%d",&n);
struct driver_details arr[n];

while ( getchar() != '\n' );

for (int i = 0; i < n; ++i)
{
    printf("Enter the detail of driver number: %d\n",(i+1));

    printf("Enter your name:\n");
    fgets( arr[i].name, sizeof( arr[i].name ), stdin );
    arr[i].name[ strcspn( arr[i].name, "\n" ) ] = '\0';
    //...

and use similar calls of fgtes.

To use the standard string function strcspn you need to include header <string.h>

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I used scanf but it would just get ignored all at once: `for (int i = 0; i < n; ++i) { printf("Enter the detail of driver number: %d\n",(i+1)); printf("Enter your name:\n"); scanf("%49[^\n]",arr[i].name); printf("Enter your driving license number:\n"); scanf("%24[^\n]",arr[i].licence_no); printf("Enter your route:\n"); scanf("%19[^\n]",arr[i].route); printf("Enter the kilometers travelled:\n"); scanf("%4[^\n]",arr[i].kms); printf("\n"); }` – Snigdha Pandey Apr 11 '23 at 15:32
  • @SnigdhaPandey Reread my answer one more if one time to read it is not enough. – Vlad from Moscow Apr 11 '23 at 15:36
0

Remember scanf does not strip off the trailing newline unless asked, so your name input gets what seems to be a blank line.

tadman
  • 208,517
  • 23
  • 234
  • 262