Home > Mobile >  Why doesn't fgets get the first input?
Why doesn't fgets get the first input?

Time:12-05

I am creating a few dynamic strings, using a function that uses fgets for getting input from the user. But the first time when I am trying to get the name of the airport, it's just "skip" the fgets automatically put \n in temp and continue the function. all the other inputs are right instead of the first one.

Please enter name of Airport:

Here it's just skipping the input process and straight printing "enter the address":

----------Please enter the address-------------

Please enter name of country:

Please enter name of city:
int initAirport(Airport* airportP)
{
    airportP->nameOfAirPort = createDynamicString("Please enter name of Airport:\n");
    printf("----------Please enter address-------------\n");
    airportP->country = createDynamicString("Please enter name of country:\n");
    airportP->city = createDynamicString("Please enter name of city:\n");
    airportP->address = createDynamicString(" Please enter name of Address:\n");
    printf("Please enter house number:\n");
    scanf("%d",&(airportP->houseNumber));
    return 1;
}
char* createDynamicString(const char* msg)
{
    char* str;
    char temp[254];
    printf(msg);
    fgets(temp,254,stdin);
    str = (char*)malloc((strlen(temp) 1) * sizeof(char));
    if (!str)
        return NULL;
    strcpy(str, temp);
    //str[strlen(str) - 1] = 0;
    return str;
}
#pragma once
#define MAX 254

typedef struct
{
    char* nameOfAirPort;
    char* country;
    char* city;
    char* address;
    int houseNumber;

} Airport;

int initAirport(Airport* pNameOfAirport);
void addNumberSignToString(char* stringOfNumberSign);
void printNameOfAirport(const Airport* pNameOfAirport);
int isSameAirport(Airport* airport1P, Airport* airport2P);
int isAirportName(Airport* airportP, char* airportName);
void freeAirport(Airport* airportP);

CodePudding user response:

scanf() leaves the newline character in the buffer after which fgets() picks it up and quits before the user has a chance to input anything.

You'll have to clear the buffer after scanf()

void clearInputBuffer(void)
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

CodePudding user response:

Check this problem: scanf() leaves the newline character in the buffer

After you use scanf() function, scanf() leaves a newline character ('\n') in your buffer. Then after, the fgets() in your initAirport function appears, it takes that newline character and you can't enter input. But that happens only once, since the next time you call createDynamicString function, you use fgets() function, instead of scanf().

You can enter this sample of code just after your first call of scanf() function, the one you have said exists and asks the user if he wanna add an airport then call another function that calls to initAirport():

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

This basically takes existing letters in your buffer until it hits newline character. In this case this will filter out the newline character that scanf() left in the buffer.

  • Related