C Structures Wrong Output

What is wrong with the program in C?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<#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");
		else if(passengerinfo[i].age>=18&&passengerinfo[i].age<60)
		printf("\nFare:=200");
		else if(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.
Topic archived. No new replies allowed.