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);