this is a struct in my code
struct custDetail
{
int reservationID;
char *name;
int year;
int month;
int mday;
int hour;
char *departurePoint;
char *destination;
char *seatType;
char *seat;
float price;
int airlineID;
};
this is one of my function
void purchaseBusiness();
void purchaseBusiness()
{
struct custDetail detail = { 0 };
detail.seatType = "Business";
detail = promptDetail(detail);
//printf("\n%s", detail.name);
//printf("\n%s %d", detail.name, detail.year);
detail = assignSeat(detail);
detail = confirmDetail(detail);
printf("\n%s", detail.name);
printf("\nBooking success");
displayToFile(detail);
return;
}
this is one the function called in function above
struct custDetail promptDetail(struct custDetail);
struct custDetail promptDetail(struct custDetail detail)
{
char name[100];
int flight, departTime;
time_t difference;
printf("\nEnter your name: ");
scanf("%s", name);
detail.name = name;
do
{
printf("\nChoose your flight");
printf("\nPress 1 - KUL to KCH");
printf("\nPress 2 - KCH to KUL");
scanf("%d", &flight);
switch (flight)
{
case 1:
{
detail.departurePoint = "KUL";
detail.destination = "KCH";
printf("\nChoose your departure time");
printf("\nPress 1 - 7:00");
printf("\nPress 2 - 9:00");
scanf("%d", &departTime);
switch (departTime)
{
case 1: detail.hour = 7; break;
case 2: detail.hour = 9; break;
default: printf("\nPlease enter 1 or 2 only"); break;
}
break;
}
case 2:
{
detail.departurePoint = "KCH";
detail.destination = "KUL";
printf("\nChoose your departure time");
printf("\nPress 1 - 19:00");
printf("\nPress 2 - 21:00");
scanf("%d", &departTime);
switch (departTime)
{
case 1: detail.hour = 19; break;
case 2: detail.hour = 21; break;
default: printf("\nPlease enter 1 or 2 only"); break;
}
break;
}
default: printf("\nPlease enter 1 or 2 only");
}
} while ((flight != 1 && flight != 2) || (departTime != 1 && departTime !=
2));
do
{
printf("\nEnter the flight date (dd/mm/yyyy)");
printf("\n30 days and above 10%% discount");
printf("\n90 days and above 15%% discount");
scanf("%d%*c%d%*c%d", &detail.mday, &detail.month, &detail.year);
if (detail.year > 0 && detail.month > 0 && detail.month <= 12 && detail.mday > 0 && detail.mday <= 31)
{
difference = checkDate(detail.year, detail.month, detail.mday, detail.hour);
if (difference < 0)
{
printf("\nPlease enter valid date");
}
}
else
{
printf("\nPlease enter valid date");
difference = -1;
}
} while (difference < 0);
return detail;
}
input for name is: testing
the output at printf("\n%s", detail.name) is
testing
and the output at printf("\n%s %d", detail.name, detail.year) will be garbage value.
why the detail.name returned a garbage value? and at the end comes out with different garbage value when the struct custDetail go on pass through all the function.