1

Hello senior programmers! I am new in programming. I am having trouble making my program run efficiently. My code works perfectly fine when I do not give white space. But when I give white space while writing the name or route it doesn't work.

Example: Driver's Name: ALI ( work perfectly )

Example: Driver's Name: ALI ALI ( not working )

I want to write all the char with white space. I have already tried every possible way. I have commanded all the ways which I have tried. I am a beginner. It is my college assignment. Kindly, please help me, How can I fix this?

/*
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
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.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Max_Size 1000
#define max 1000
struct Drivers
{
    char Name[150];
    char Driving_License_No[150];
    char Route[150];
    char Kms[50];
    int positionNo;
} drivers[Max_Size];
int main()
{
    int totalDrivers;
    char name[200];
    printf("Enter the total numbers of drivers\n");
    scanf("%d", &totalDrivers);
    for (int i = 0; i < totalDrivers; i++)
    {
        drivers[i].positionNo=i+1;
        printf("\nFor Driver Id no. %d,\n", drivers[i].positionNo);
        printf("Enter the full name: ");
        scanf("%s", &drivers[i].Name);
        // scanf("%[^\n]",drivers->Name);
        // scanf("%[^\n]s",drivers->Name);
        // fgets(drivers.Name, 150, stdin);
        // gets(Name);
        // gets(driver.Name);

        printf("Enter the Driving License no : ");
        scanf("%s", &drivers[i].Driving_License_No);
        printf("Enter the Driving Route  : ");
        scanf("%s", &drivers[i].Route);
        printf("Enter the Driving KM's : ");
        scanf("%s", &drivers[i].Kms);
    }
    printf("The Information of all drivers are given below\n");
    for (int i = 1; i < totalDrivers; i++)
    {
        printf("\nDriver Id no.: %d\n", i + 1);
        printf("Driver's Name: ");
        puts(drivers[i].Name);
        printf("Driving License no: %s", drivers[i].Driving_License_No);
        printf("\n");
        printf("Driving Route: %s", drivers[i].Route);
        printf("\n");
        printf("Driving KM's: %s", drivers[i].Kms);
        printf("\n");
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    See https://stackoverflow.com/q/1247989/10622916 Use `fgets` instead of `scanf`. – Bodo Dec 29 '20 at 20:35
  • Does this answer your question? [Scanning until new line](https://stackoverflow.com/questions/27677063/scanning-until-new-line) – user14063792468 Dec 29 '20 at 20:50
  • Please, dear novice, explain what does it mean **it doesn't work**, because we don't understand what is happening in your computer. Tell us what do you expect the code to do, and what is actually doing, so we can guess where to look. When you bring your car for repair, you tell the mechanic you hear some noise somewhere in the motor, and this makes him easier to find where to look. Do you go to the repair box and say _the car doesn't work_? – Luis Colorado Dec 31 '20 at 04:52

2 Answers2

1

There are multiple issues:

  • parsing a string with embedded white space can be done with %[^\n] in scanf().
  • you should tell printf the maximum number of characters to store into the destination arrays to avoid potential undefined behavior on invalid input.
  • &drivers[i].Name is not a char *, you should pass drivers[i].Name.
  • when printing the details, you start at 1 instead of 0.
  • to input multiple strings with embedded spaces, which is likely required here, you should use " %[^\n]" with an initial space to skip the pending newline and any initial spaces.

Here is a modified version:

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

#define Max_Size 1000
#define max 1000

struct Drivers {
    char Name[150];
    char Driving_License_No[150];
    char Route[150];
    char Kms[50];
    int positionNo;
} drivers[Max_Size];

int main() {
    int totalDrivers;
    char name[200];

    printf("Enter the total numbers of drivers\n");
    if (scanf("%d", &totalDrivers) != 1)
        return 1;

    if (totalDrivers > Max_Size)  // avoid overflowing the drivers array;
        totalDrivers = Max_Size;

    for (int i = 0; i < totalDrivers; i++) {
        drivers[i].positionNo = i + 1;
        printf("\nFor Driver Id no. %d,\n", drivers[i].positionNo);
        printf("Enter the full name: ");
        scanf(" %149[^\n]", drivers[i].Name);
        printf("Enter the Driving License no: ");
        scanf(" %149s", drivers[i].Driving_License_No);
        printf("Enter the Driving Route: ");
        scanf(" %149s", drivers[i].Route);
        printf("Enter the Driving KM's: ");
        scanf(" %49s", drivers[i].Kms);
    }

    printf("The Information of all drivers are given below\n");
    for (int i = 0; i < totalDrivers; i++) {
        printf("\n");
        printf("Driver Id no.:      %d\n", i + 1);
        printf("Driver's Name:      %s\n", drivers[i].Name);
        printf("Driving License no: %s\n", drivers[i].Driving_License_No);
        printf("Driving Route:      %s\n", drivers[i].Route);
        printf("Driving KM's:       %s\n", drivers[i].Kms);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

See instead of using %s as format specifier you should use %[^\n] as the format specifier while taking input in scanf to take the input with white spaces.

    printf("Enter the full name: ");
    scanf("%[^\n]", drivers[i].Name);

In case of any query please feel free to ask.

Yukti Kumari
  • 23
  • 1
  • 7