Please help in rectifying the error in C++ code


#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int count=0;
struct airline
{
int ano;
char fname[50];
char lname[50];
int day;
int month;
int year;
char address[100];
char depcity[100];
char destin[100];
float fare;
};
void reservation();
void flightstatus();
void details();
void cancel();
int main()
{ airline res;
int choice;
cout<<"\n*****************WELCOME TO AIRLINE RESERVATION SYSTEM*********************";
do
{
cout<<"\n\tMain menu";
cout<<"\n1.Reservation";
cout<<"\n2.Flight details";
cout<<"\n3.Check your flight status";
cout<<"\n4.Cancel Reservation";
cout<<"\n5.Exit";
cout<<"\n\nEnter choice:";
cin>>choice;
switch(choice)
{
case 1:
// clrsr();
reservation();
break;
case 2:
//clrsr();
if(count==0)
{
cout<<"\nAirline not yet booked.";
cout<<"Please select choice 1 first and complete the procedure.";
break;
}
details();
break;
case 3:
//clrsr();
if(count==0)
{
cout<<"\nAirline not yet booked.";
cout<<"Please select choice 1 first and complete the procedure.";
break;
}
flightstatus();
break;
case 4:
// clrsr();
if(count==0)
{
cout<<"\nAirline not yet booked.";
cout<<"Please select choice 1 first and complete the procedure.";
break;
}
cancel();
break;
default:cout<<"Invalid Choice!!...Please input a valid choice or EXIT!";
}
cout<<"Press any key";
}while(choice!=5);
}
void reservation()

{ airline res;
int n;
cout<<"\n\tWELCOME GUEST!!";
cout<<"Enter how many passengers?";
cin>>n;
for(int i=0;i<n;i++)
{
//res.ano=0;
cout<<"Enter passengers[i+1] first name:";
gets(res[i].fname);
cout<<"Enter passengers last name:";
gets(res[i].lname);
cout<<"Enter address of passenger:";
gets(res[i].address);
cout<<"Enter passenger date of birth(MM/DD/YYYY):";
gets(res[i].date);
gets(res[i].month);
gets(res[i].year);
cout<<"Check date of birth again!";
cout<<res[i].date<<"/"<<res[i].month<<"/"<<res[i].year;
cout<<"Enter destination city:";
gets(res[i].depcity);
cout<<"Enter departure city:";
gets(res[i].destin);

}
}

I am writing a school project program for airline reservation system but im getting the following error during compilation:

[i]error: no match for 'operator[]' in 'res[i]'


Im getting these errors in the gets part of the function.

By the way the program is incomplete.
I am using code blocks.

Last edited on
You declare your variable res to be of the type airline (a structure type). Later on you try to use the array subscript operator (operator[]) on this structure. Since res isn't an array (or a class implementing the operator), the compiler will complain that you cannot use that operator on your variable. An example of what you might have intended:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    /* Our variables */
    airline* res; //We will store our values in here later on
    int n; //The number of passengers

    /* Get some value into n (like you did in your original code) */
    ...

    /* Allocate enough airline structures for all passengers */
    res = new airline[n]; //Allocate n airline structures
    
    /* Use our values (like you did in your original code) */
    ...
    
    /* Free the allocated airline structures */
    delete[] res;
}


Don't forget to check for an allocation failure when using new, and don't forget to call delete when you're done with your values.

Another detail, try not to use #include <conio.h> . It's a non portable header file. Clearing the screen isn't necessary in most cases, and if it really is necessary (think about console games like snake or pacman), then implement it yourself. Using clrscr reduces portability and the function, meaning quite a few people on this forum wouldn't be able to compile your code.

Also, try to use code tags from now on, it greatly improves readability and thus your chances at getting an answer to your topic.
Topic archived. No new replies allowed.