Airline Reservation System( Can't get only available seats to load)

This is my code, however I can't get my program to print only AVAILABLE SEATS; instead it prints Both Available and Taken.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//Program allows you to delete and add seats to the Airplane. Also to upload seat settings as a text file. 
#include <iostream>
#include <fstream> //file stream for text file
#include <string>
using namespace std;

bool Myseats[150]; //the size of the array for type bool
int AvSeats[150];//size of array for int
int position; //index of the array 
void Store(int); //function:Stores the item in the airplane  
void New_Seat(); //function prints if person wants to change seat because its taken. 
void seat_delete(int); //function: deleteing an item in the arra 

void print_seats(); //function:show the seat that are taken. 

bool is_inArray(int); //check if the item already exisit

char chSeat= 'n'; //if want to change seat 
int intChoice; //choosing the option on the menu 

void main()
{

for (int a =0; a<=150; a++) //loops until seats are filled 
{
	Myseats[a]==false;//initialize the seats as false as empty seats 
}
do //start of DO while
{

cout<<endl<<"\t\t\tAirline Reservation System"<<endl;
cout<<"Make your selection\n\nPurchase tickets(1)\nCancel Reservation(2)\nWrite seats to be saved in a text file(3)\nUpload seats from a text file(4)\n";
cin>>intChoice;

if (intChoice==1) //purchase ticket 
{
	cout<<"Enter seat number: ";
	cin>>position;
	New_Seat();

}

else if(intChoice==2)//Cancel reservation 
{
cout<<"Enter seat to be deleted: ";
cin>>position;
seat_delete(position);
}

else if(intChoice==3)
{
ofstream outFile ("sample.txt");//sample.text was creaTED AUTOMATICALLy DUE TO OFSTREAM
//Checks for errors in file
if(!outFile.is_open())
{cout<<"Unable to open the file"<<endl;
}
else
{
for(int a =0; a<150;a++) //printing out the seat arrangments as a text file
{
	outFile<<Myseats[a]<<endl;
}

outFile.close(); //to close text and if not used .close to file will have errors
cout<<"Sucessfully written to the file, Open File\n\n";
}

}
else if(intChoice==4)
{
	ifstream infile ("Airplane1.txt");
	if(!infile.is_open())//Checks for errors in file
	{cout<<"Unable to open the file"<<endl;
}
else
{
for(int a =0; a<150;a++) //printing out the seat arrangments as a text file
{
	infile>>Myseats[a];
}


	infile.close();//to close text and if not used .close to file will have errors
	cout<<"Sucessfully read to the file, Open File\n\n";
}
}
print_seats();

}
while (position<=150);//end of Do while
}

//===========================================Print=========================================================
void print_seats()//Shows the seats that are taken. 
{	cout<<"Seat Status: Taken-1-  Avaliable-0-\n";
int intposition(0);    
for(int a = (0); a<150; a++)//A normal for statement can be used to output the elements of any array
	{
		if(Myseats[a]==0)
		{
		if(intposition%4==0)
	{cout<<endl;
	}
		cout<<"Seat Number "<<a+1<<" :"<<Myseats[a]<<" ";// note that Myseats[a] is used the same way as a normal variable
	intposition++;

	}
	}
	cout<<endl;
}

//===========================================Check if the Seat is taken =========================================================	 
bool is_inArray(int intVal)/*This function searches throught the entire array to check if an item (intval) is stored within int*/
{
	bool result=false;/*First declare and initialize a result varaible to false
						 This automatically makes the assumption that the value is not within the array*/
	for(int a=0; a<150;a++)
	{
		if(intVal==AvSeats[a])
		{	
			result = true;
			break;
			
		}
	}
	return result;
}
//==========================================================If Seats are full pick another seat===========================
void New_Seat()
{
	if (is_inArray(position)==true)
	{
		cout<<"\nSeat is full. Press 'y' to choose a different seat: ";
		cin>>chSeat; 
		if(chSeat=='y')
		{print_seats();

		do
		{
		int takeaway;
		cout<<"New Seat Number: ";
		cin>>position;
		if (is_inArray(position)==false)
		{Store(position);
		break;
		}
		else {cout<<"Seat Taken\n";}
		position=position-1;
		}
		while(is_inArray(AvSeats [position])==true);
		
		}
	}
		else 

		{
			Store(position);
		}		
}
//===========================================Assign a Seat=========================================================	
	void Store (int intVal)
	{
		AvSeats[intVal]=intVal;
		intVal=position-1;
		Myseats[intVal]=true;
	}

//===========================================Delete a Seat=========================================================
void seat_delete(int intVal)
{

{AvSeats[intVal]=intVal;
intVal=position-1;
Myseats[intVal]=false;
}

}


Last edited on
Updated.
I don't mean to sound rude or anything but you need to work on your indentation, it is hurting my eyes trying to read.

using namespace std; You should use std:: before anything in the std namespace to avoid naming conflicts.

1
2
3
bool Myseats[150]; //the size of the array for type bool
int AvSeats[150];//size of array for int
int position; //index of the array  
you should avoid global variables and pass them as parameters to your functions. You should also avoid magic numbers like 150 that I see throughout your program. Make them constants.

void main this should be int main


In your print function you are printing the "myseats" instead of "avseats." Is myseats supposed to be all the available and taken seats? I assumed "avseats" was the available seats.

I don't mean to sound rude or anything but you need to work on your indentation


I was holding my tongue till I could figure it out. (but it does make it so hard when the formatting isn't consistent. Spaces, newlines, indentation are all inconsistent)

I would suggest making one array for available seats and one array for taken seats. Or just use the values 0, 1 to indicate which are taken or free, instead of having a bool array just to indicate that.

1
2
bool Myseats[150]; //the size of the array for type bool
int AvSeats[150];//size of array for int 


Also try to think of more descriptive comments. As anyone can see the type and size just by looking at the code.
Last edited on
My teacher wanted this type of format. So i had to follow his guidelines. However, I've noticed my problem in my previous code. I just had to change
If(Myseats[a]==0)
to the one below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void print_seats()//Shows the seats that are taken. 
{	cout<<"\n\t\t\tAvaliable Seats-0-\n";
int intposition(0);    
for(int a = (0); a<150; a++)//A normal for statement can be used to output the elements of any array
	{
		if(Myseats[a]==false)
		{
		if(intposition%4==0)
	{cout<<endl;
	}
		cout<<"Seat Number "<<a+1<<" :"<<Myseats[a]<<"  ";// note that Myseats[a] is used the same way as a normal variable
	intposition++;

	}
	}
	cout<<endl;
}
== 0 and == false is the same. 0 == false and anything else == true.
Topic archived. No new replies allowed.