//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>
usingnamespace 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();
}
elseif(intChoice==2)//Cancel reservation
{
cout<<"Enter seat to be deleted: ";
cin>>position;
seat_delete(position);
}
elseif(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";
}
}
elseif(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;
}
}
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.
usingnamespace 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.
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;
}