// You have shown enough effort for me to help you rewrite this
// We will use standard headers and not the deprecated ones you were using.
// I am assuming you are running under a windows OS so we can use a few
// facilities provided by the OS to do things like clearing the screen.
#include<iostream>
#include<iomanip>
#include<string>
#include<limits>
#include<cstdlib>
#include<windows.h>
// windows.h defines a max macro that will make our life hell so we undef it
#undef max
// the functions and objects in the standard headers are all declared within namespace
// std. We will bring them all into the global scope so we dont have to prefix everything
// with std:: with this simple line.
using namespace std;
// here is how to clear the screen on a windows console. You do not need to
// necessarily understand this to use it.Suffice to say that a call to this
// will clear the screen and reset the cursor pos to the top left corner.
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
// Lets introduce a struct to represent our seats. A struct is an aggregate type made up
// of smaller types.
// You declare one like so :-
// struct STRUCTNAME { members };
// you make an object of your struct like declaring an int.
// STRUCTNAME obj;
// You access the members with the dot operator like so...
// struct mystruct{inta;intb;};
// mystruct astruct;
// astruct.a = 0;astruct.b = 0;
struct Seat
{
string seattype;
float seatprice;
int row;
int seatnumber;
bool taken; // a bool has 2 states true and false. When this is true the seat is taken
};
// Now we will make a global array of seats to represent the plane.
// We will use this all over the place so global as a convenience.
// There are 12 rows of 4 seats so...
Seat seatmap[12][4];
// Lets write a function to get the prices of seats
float GetInput(string s)
{
cout<<s;
float input;
while (!(cin>>input))
{
cerr<<endl<<"error! re-enter"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
return input;
}
// armed with that we can write a function to initalise the seatmap
void InitSeatMap()
{
float Legroomprice = GetInput(string("Enter cost of a leg-room seat ? "));
float Windowprice = GetInput(string("Enter cost of a window seat ? "));
float Aisleprice = GetInput(string("Enter cost of an aisle seat ? "));
for (int i=0;i<12;++i)
for(int j=0;j<4;++j)
{
seatmap[i][j].row = i+1;
seatmap[i][j].seatnumber = j+1;
seatmap[i][j].taken = false; // no seat is taken yet
if (i==0) // legroom seats
{
seatmap[i][j].seattype = "legroom";
seatmap[i][j].seatprice = Legroomprice;
}
if((i != 0) && (j==0 || j==3)) // we have a windowseat
{
seatmap[i][j].seattype = "window";
seatmap[i][j].seatprice = Windowprice;
}
if((i != 0) && (j==1 || j==2)) // we have an aisle seat
{
seatmap[i][j].seattype = "aisle";
seatmap[i][j].seatprice = Aisleprice;
}
}
}
// now we can write a function to allocate a seat
void AllocSeat(string type)
{
for(int i=0;i<12;++i)
for(int j=0;j<4;++j)
{
if (seatmap[i][j].seattype == type && !seatmap[i][j].taken)
{
seatmap[i][j].taken = true; // seats free so allocate it taken
cout<<endl<<"Seat allocated. Row :- "<<seatmap[i][j].row<< " Seatnumber :- "<<seatmap[i][j].seatnumber<<endl;
cout<<"Cost of seat is $"<<seatmap[i][j].seatprice<<endl;
return;
}
}
cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl;
}
// an overload to take a specific seat
void AllocSeat(int row, int seatnum)
{
if (!seatmap[row-1][seatnum-1].taken)
{
seatmap[row-1][seatnum-1].taken = true;
cout<<endl<<"Seat allocated."<<endl;
cout<<"Cost of seat is $"<<seatmap[row-1][seatnum-1].seatprice<<endl;
return;
}
cout<<endl<<"Sorry but that particular seat is already taken!!!"<<endl;
}
void Menu()
{
clrscr();
cout<<"1 - View available seats"<<endl
<<"2 - View seating prices"<<endl
<<"3 - View ticket sales"<<endl
<<"4 - Purchase a ticket"<<endl
<<"5 - Quit"<<endl;
}
void DisplaySeatMap()
{
clrscr();
for (int i=0;i<12;++i)
for (int j=0;j<4;++j)
{
if (!i && !j)
{
cout<<" "<<setw(3)<<1<<setw(3)<<2<<setw(3)<<3<<setw(3)<<4<<endl;
}
if (j==0)
{
cout<<setw(4)<<left<<seatmap[i][j].row;
}
if (seatmap[i][j].seattype == "legroom")
{
cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "L");
}
if (seatmap[i][j].seattype == "window")
{
cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "W");
}
if (seatmap[i][j].seattype == "aisle")
{
cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "A");
}
if (j==3)
cout<<endl;
}
system("PAUSE"); // lousy but just what we need
}
bool Valid(char c)
{
c = tolower(c);
if( c == 'a' || c == 'w' || c == 'l')
return true;
else
return false;
}
void PurchaseSeat()
{
clrscr();
cout<<"Do you wish to pick a particular seat(Y/N) ? ";
char input;
while(!(cin>>input))
{
cerr<<endl<<"error! re-enter"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
if (tolower(input) == 'y')
{
int row,seatnum;
cout<<"Enter row followed by a space then seat number :- ";
while (!(cin>>row>>seatnum) || row<1 || row>12 || seatnum<1 || seatnum>4)
{
cerr<<endl<<"error! re-enter"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
AllocSeat(row,seatnum);
}
else
{
cout<<endl<<"What type of seat would you like (W,L,A) ? ";
while(!(cin>>input) || !Valid(input))
{
cerr<<endl<<"error! re-enter"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
string type;
if(tolower(input) == 'l')
{
type = "legroom";
}
else if(tolower(input) == 'w')
{
type = "window";
}
else
{
type = "aisle";
}
AllocSeat(type);
system("PAUSE");
}
}
void DisplaySales()
{
int sales[3]={0};
for (int i=0;i<12;++i)
for (int j=0;j<4;++j)
{
if (seatmap[i][j].seattype == "legroom" && seatmap[i][j].taken)
{
++sales[0];
}
if (seatmap[i][j].seattype == "window" && seatmap[i][j].taken)
{
++sales[1];
}
if (seatmap[i][j].seattype == "aisle" && seatmap[i][j].taken)
{
++sales[2];
}
}
clrscr();
cout<<"Legroom seats sold :- "<<sales[0]<<" at $"<<seatmap[0][0].seatprice<<endl
<<"Window seats sold :- "<<sales[1]<<" at $"<<seatmap[1][0].seatprice<<endl
<<"Aisle seats sold :- "<<sales[2]<<" at $"<<seatmap[1][1].seatprice<<endl;
double revenue = (sales[0] * seatmap[0][0].seatprice) + (sales[1] * seatmap[1][0].seatprice)
+ (sales[2] * seatmap[1][1].seatprice);
cout<<"Total revenues for this flight are $"<<revenue<<endl;
system("PAUSE");
}
void DisplayPricing()
{
clrscr();
cout << "Legroom seat :- $"<<seatmap[0][0].seatprice<<endl
<<"Window seat :- $"<<seatmap[1][0].seatprice<<endl
<<"Aisle seat :- $"<<seatmap[1][1].seatprice<<endl;
system("PAUSE"); // lousy but does the job
}
int main()
{
InitSeatMap();
while(1) // infinite loop
{
Menu();
cout<<"Enter choice :- ";
int input;
while (!(cin>>input) || input<1 || input>5)
{
cerr<<endl<<"error! re-enter"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
switch(input)
{
case 1: DisplaySeatMap();
break;
case 2: DisplayPricing();
break;
case 3: DisplaySales();
break;
case 4: PurchaseSeat();
break;
case 5: cout<<"Exiting.........Goodbye!!!"<<endl;
Sleep(2000); // a small wait of 2 secs
return 0;
}
}
}