<#include<stdio.h>
#include<conio.h>
struct boat
{ int age;
char name[30];
}passengerinfo[2];
void main()
{ clrscr();
int a;
printf("\nEnter the number of tickets:");
scanf("%d",&a);
for(int i=0;i<a;i++)
{ printf("\nEnter name[%d]:",i+1);
scanf("%s",&passengerinfo[i].name);
printf("\nEnter age[%d]:",i+1);
scanf("%d",&passengerinfo[i].age);
}
printf("\nThe boarders of the boats:");
for(i=0;i<a;i++)
{ printf("\nName:%s",passengerinfo[i].name);
printf("\tAge:%d",passengerinfo[i].age);
}
for(i=0;i<a;i++)
{
if(passengerinfo[i].age>0&&passengerinfo[i].age<18)
printf("\nFare=:100");
elseif(passengerinfo[i].age>=18&&passengerinfo[i].age<60)
printf("\nFare:=200");
elseif(passengerinfo[i].age==0||passengerinfo[i].age>=60)
printf("\nFare=:300");
else
printf("\nIncorrect age");
}
getch();
}>
I am able to type the name and age for 4 people. For numbers greater than 4(for ex: for 5 tickets, 6tickets,etc.) I do not get any displays but I can type. Please help me to rectify the display. Thanks.
The problem is that you've only allocated space for 2 passengers. Change line 6 to have the maximum number that you expect. Alternatively, allocate the passenger array on the heap after the user enters the number at line 11.