Please support our C++ advertiser: Programming Forums
Views: 5177 | Replies: 25
![]() |
•
•
Join Date: Jul 2005
Location: London
Posts: 164
Reputation:
Rep Power: 4
Solved Threads: 5
here I cleaned it up a bit and added some while smoking a joint. Theres not much left for you to do now. Make sure you understand everything. If you are not sure then ask.
// 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;
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;
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
}
void PurchaseSeat()
{
// offer pick a seat or type of seat.
// call correct AllocSeat
// display cost
}
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<0 || 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;
}
}
}•
•
Join Date: Jul 2005
Location: London
Posts: 164
Reputation:
Rep Power: 4
Solved Threads: 5
gotta go work now so heres how mine looks finished.
Did you manage to make those changes on your own?
Anything you didn't understand?
// 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;
}
}
}Anything you didn't understand?
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
Hi, Stone_Coder;
thank you so much for helping me out.
I don't know if this is hard work for you or not (prob not), but it was certainly was for me. And I'm glad that you help me through it.
Now I'm not going to lie and say I understood your code. I said I can follow it, not nessarily understood everything. Now that the class is over and it's not rushed, I'm going to go through your code slowly. And it might take me a bit of time to digest everything. So if I'm not posting my questions right away yet, please don't think I'm abandoning it because the project's over and done with (plus I don't think you'd appreciate it if I post everything question I have, 'cause it's quite a lot).
When you said "hopefully" you'd teach me something along the way, you don't know how right you are! I certainly learn a lot from doing this, both from my own experience and from your help.
Oh, I forgot. My PurchaseTicket() function, again, did not look like yours at all. I keep having problems with the structure data thing. And I couldn't get it linked to the DisplaySales() functions. But I understand what I did wrong now...I think.
So I know I said it alot already, but Thanks again!
Karen
thank you so much for helping me out.
I don't know if this is hard work for you or not (prob not), but it was certainly was for me. And I'm glad that you help me through it.
Now I'm not going to lie and say I understood your code. I said I can follow it, not nessarily understood everything. Now that the class is over and it's not rushed, I'm going to go through your code slowly. And it might take me a bit of time to digest everything. So if I'm not posting my questions right away yet, please don't think I'm abandoning it because the project's over and done with (plus I don't think you'd appreciate it if I post everything question I have, 'cause it's quite a lot).
When you said "hopefully" you'd teach me something along the way, you don't know how right you are! I certainly learn a lot from doing this, both from my own experience and from your help.
Oh, I forgot. My PurchaseTicket() function, again, did not look like yours at all. I keep having problems with the structure data thing. And I couldn't get it linked to the DisplaySales() functions. But I understand what I did wrong now...I think.
So I know I said it alot already, but Thanks again!
Karen
And for those who weren't aware of the cross-posting, there's this:
http://www.gidforums.com/t-6485.html
Where else this may be I don't know. But it is always nice for the OP to tell others that the question may already be answered elsewhere.
http://www.gidforums.com/t-6485.html
Where else this may be I don't know. But it is always nice for the OP to tell others that the question may already be answered elsewhere.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode