-2

my code is not working properly and I don't know why, gets(delhiAgency[i].drvrName); is not waiting for user input, I am stuck here.

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

int main(void) {
    typedef struct busMngr {
        char* drvrName;
        int drvrLicNumb;
        int busNumb;
        char* fullRoute;
        char* route[2];
        int kms;
    } busManager;
    
    int busNo;
    
    printf("Enter number of buses: ");
    scanf("%d", &busNo);
    
    busManager delhiAgency[busNo]; //Derived data type busManager


    for (int i = 0; i < busNo; i++) {
        delhiAgency[i].drvrName = (char *) malloc(15 * sizeof(char));
        delhiAgency[i].fullRoute = (char *) malloc(20 * sizeof(char));
        for (int j = 0; j < 2; j++) {
            delhiAgency[i].route[j] = (char *) malloc(20 * sizeof(char));
            delhiAgency[i].route[j] = (char *) malloc(20 * sizeof(char));
        }
    }

    for (int i = 0; i < busNo; i++) {
        printf("Enter the name of driver: ");
        gets(delhiAgency[i].drvrName);

        printf(" Enter the license number of driver '%s': ", delhiAgency[i].drvrName);
        scanf("%d\n", &delhiAgency[i].drvrLicNumb);

        printf("Enter bus number alloting to driver '%s': ", delhiAgency[i].drvrName); 
        scanf("%d\n", &delhiAgency[i].busNumb);

        printf("Enter the route of bus number %d\n", delhiAgency[i].busNumb);
        gets(delhiAgency[i].fullRoute);

        printf("Enter the starting point of bus number %d: \n", delhiAgency[i].busNumb);
        gets(delhiAgency[i].route[0]);
        printf("Enter the destinatin of bus number %d: \n", delhiAgency[i].busNumb);
        gets(delhiAgency[i].route[1]);
    }
    return 0;
}
Satyam S
  • 17
  • 3
  • First of all *never* use `gets`! It's considered so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it has even been removed from the C language itself. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude May 12 '22 at 11:55
  • Also you [don't have to (and really shouldn't) cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/). Related to this, wjhy do you use `malloc` at all instead of plain arrays? – Some programmer dude May 12 '22 at 11:57
  • Does SO keep good statistics on how often bad-practices are asked about? It would be very interesting to see how often `gets` comes up. – William Pursell May 12 '22 at 12:02
  • @Someprogrammerdude Plain array requires array size and who knows the length of string user gonna enter? – Satyam S May 12 '22 at 12:29
  • @SatyamS Either use a length that is guaranteed to be long enough for all relevant input. Or use dynamic allocation and reallocation (`fgets` makes it easy, as it will insert the ending newline when the full line has been read). – Some programmer dude May 12 '22 at 14:43
  • @Someprogrammerdude yes now i am using it, – Satyam S May 12 '22 at 16:16

0 Answers0