| | |
airplane seating prices
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
Hi, This is my complete code:
yeah, I'm aware that's it's a bit (ok, a lot) messy. But since I'm a beginner, I don't know how to take any other shortcuts.
Can you take a look at the DisplayPrices() and the PurchaseTickets() functions? I post my questions there as well.
thanks a lot,
Karen
C++ Syntax (Toggle Plain Text)
//Airplanes Seating Allocation System //libraries included #include <iostream.h> #include <stdlib.h> #include <iomanip.h> #include <string> char airplane [12] [4]; float ticketPrice [12] = { 0.0 }; float totalsales = 0.0f; int row, seat; //Function Prototypes void DisplaySeats (void); void DisplayPrices (float []); void DisplaySales (); void PurchaseTicket (float[]); void main () { int choice; char response = 'Y'; float seatprices[4] = { 4.0, 5.0, 6.0, 7.0 }; //just some defaults here: ASWL char seattypes[4][12] = { "Aisle", "Regular", "Window", "Leg Room" }; //this makes it possible to do input with a loop cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n"; cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"; //initialize airplane with labels (A, S, W, L) - all seats available for (int y =0; y < 1; y++) for (int q = 0; q <4; q++) airplane [y][q] = 'L'; //Leg Room Seats for (y = 1; y < 12; y++) { for (int z = 0; z < 4; z++) { for (int x = 0; x < 1; x++) airplane [y][x] = 'S'; //Regular Seats for (int v = 1; v < 3; v++) airplane [y][v] = 'A'; //Aisle Seats for (int w = 3; w <4; w++) airplane [y][w] = 'W'; //Window Seats } } //input the Seat prices //did I do this right? for (int i = 0; i < 4; i++) { cout << "Please enter price for " << seattypes[i] << " seats: "; cin >> seatprices[i]; cout << endl; } while (response = 'Y') { //display menu of choices cout << "\n\n\n\tC++ Airplane Seating Allocation System" << endl << endl; cout << "\n\t1. View Availabe Seats"; cout << "\n\t2. View Seating Prices"; cout << "\n\t3. View Ticket Sales"; cout << "\n\t4. Purchase a Ticket"; cout << "\n\t5. Exit the Program\n\n"; cout << "\n\tEnter your choice (1-5): "; cin >> choice; switch (choice) { case 1: cout << "Testing. DisplaySeats() called\n"; DisplaySeats(); break; case 2: DisplayPrices(ticketPrice); break; case 3: DisplaySales(); break; case 4: PurchaseTicket(ticketPrice); break; case 5: exit (0); break; default: exit(0); break; } //end switch }; //end while } //end main //Display a seating chart of the airplane void DisplaySeats (void) { cout << "\n\tSEATS\n"; cout << " 1234"; cout << endl; for (int r = 0; r < 12; r++) { cout << "\nRow " << setw(2) << r + 1 << "\t"; for (int s =0; s < 4; s++) { cout << airplane[r][s]; } } cout << "\n\n\n\tLegend:\t* = Sold"; cout << "\n\t\tA = Aisle Seats (Available)"; cout << "\n\t\tW = Window Seats (Available)"; cout << "\n\t\tS = Regular Seats (Avaiable)"; cout << "\n\t\tL = Leg Room Seats (Available)"; cout << endl; char keyin; cout << endl << endl; cout << "\nPress an alphanumeric key to continue."; cin >> keyin; } //end DisplaySeats void DisplayPrices(float *seatprices[], float *seattypes[]) { cout.precision(2); cout.setf(ios::fixed | ios::right); cout << "\n\tTicket Prices By Seat Types: " << endl << endl; cout << "\tSeat Types Price" << endl; cout << "\t------ -------" << endl << endl; for (int m = 0; m < 4; m++) *seattypes[m]; for (int n = 0; n < 4; n++) *seatprices[n]; // I don't know what to do here! char keyin; cout << endl << endl; cout << "\nPress an alphanumeric key to continue."; cin >> keyin; } void PurchaseTicket(float price[]) { int a, b = 0; char response; cout << "\n\t C++ Airplane Seating Allocation System" << endl; cout << "\t\tTicket Purchase Opportunity" << endl; do { //Right now I can only ask the user to enter the row number, //I don't know how to tell the user to enter the seat type (window, aisle...) and the row number as well do { cout << "\nPlease enter row number (1-12): "; cin >> a; } while (a <1 || a > 12); do { cout << "\nPlease enter seat number (1-4): "; cin >> b; } while ( b < 1 || b > 4); if (airplane [a-1][b-1] == 'A' || airplane [a-1][b-1] == 'L' || airplane [a-1][b-1] == 'W' || airplane [a-1][b-1] == 'S') //if the seat is available { airplane[a-1][b-1] = '*'; //indicate that the seat is taken totalsales +=ticketPrice[a-1]; cout << "\nYour purchase of Seat " << b << "in Row " << a << "is confirmed." << endl; } else //seat has been sold { cout << "\nSorry. That seat has been sold.\n"; } cout << "\nWould you like to purchase another seat?"; cin >> response; } while (toupper(response) == 'Y'); } void DisplaySales () { cout.setf(ios::fixed); cout << "\n\nTotal Sales to Date: $" << setprecision(2) << totalsales << endl; }
yeah, I'm aware that's it's a bit (ok, a lot) messy. But since I'm a beginner, I don't know how to take any other shortcuts.
Can you take a look at the DisplayPrices() and the PurchaseTickets() functions? I post my questions there as well.
thanks a lot,
Karen
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
I looked at your code. It really could benefit from a rewrite. I got this far then had to go out so rest left as exercise for you. see how you do. Post later what you come up with and ill look it over.
You will still need to write the bits I left as an exercise and also add any more functions you deem necessary. Its reasonably simple stuff but some of it you may not have come across yet. Try follow whats been done so far. It should be reasonably easy for you to finish it off.
C++ Syntax (Toggle Plain Text)
// 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<string> #include<limits> #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; } } } // a quick function to output a sorry message void Sorry() { cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl; } // now we can write a function to allocate a seat void AllocSeat(string type) { // left as exercise to reader // you can check strings for equality like // if (type == "legroom") } void Menu() { clrscr(); // exercise for reader } void DisplaySeatMap() { clrscr(); for (int i=0;i<12;++i) for (int j=0;j<4;++j) { if (seatmap[i][j].seattype == "legroom") { cout << ((seatmap[i][j].taken) ? "*\t" : "L\t"); } if (seatmap[i][j].seattype == "window") { cout << ((seatmap[i][j].taken) ? "*\t" : "W\t"); } if (seatmap[i][j].seattype == "aisle") { cout << ((seatmap[i][j].taken) ? "*\t" : "A\t"); } if (j==3) cout<<endl; } } int main() { InitSeatMap(); Menu(); // exercise for reader return 0; }
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
OMG! You're asking me to rewrite my code, which I've spent days (over a week already) doing. Please don't be that cruel. i'm only a beginner and I've slaved over this project so much I can't bear to start over again.
I looked over the code that you're written so far...and to be honest...I can follow it but I can't do that on my own...I have no idea what to do next!
Can you please just help me with my code? with the parts where I got stuck on? Please?
Right now, I've rewritten parts of it. and there is one mistake that I should mention. There are only 3 types of seats: leg room, Window, and aisle seats. There are no "regular seats" because airplanes have windows on both sides *duh*.
My modification: the DisplaySeats() and the DisplayPrices() functions.
My Problem: the PurchaseTicket() and the DisplaySales() functions. Since each type of seat has different prices, I've trying to ask for the user to enter the seattype and then the row and the seat numbers. I'm only able to code the program to ask for the row and seat numbers.
Can you tell me what's wrong with my DisplayPrices() function? Why doesn't display anything when I tested it?
I'm so sorry I'm not any brighter at this. It's really hard to learn this stuff.
Karen
I looked over the code that you're written so far...and to be honest...I can follow it but I can't do that on my own...I have no idea what to do next!
Can you please just help me with my code? with the parts where I got stuck on? Please?
Right now, I've rewritten parts of it. and there is one mistake that I should mention. There are only 3 types of seats: leg room, Window, and aisle seats. There are no "regular seats" because airplanes have windows on both sides *duh*.
My modification: the DisplaySeats() and the DisplayPrices() functions.
My Problem: the PurchaseTicket() and the DisplaySales() functions. Since each type of seat has different prices, I've trying to ask for the user to enter the seattype and then the row and the seat numbers. I'm only able to code the program to ask for the row and seat numbers.
Can you tell me what's wrong with my DisplayPrices() function? Why doesn't display anything when I tested it?
C++ Syntax (Toggle Plain Text)
//Airplanes Seating Allocation System //libraries included #include <iostream> #include <stdlib.h> #include <iomanip.h> #include <string> #include<windows.h> char airplane [12] [4]; float ticketPrice [12] = { 0.0 }; float totalsales = 0.0f; int row, seat; //Function Prototypes void DisplaySeats (void); void DisplayPrices (float *, char [][12]); void DisplaySales (); void PurchaseTicket (float[]); float getSeatPrice(char type); 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); } void main () { int choice; char response = 'Y'; float seatprices[3] = { 4.0, 5.0, 6.0 }; //just some defaults here: AWL char seattypes[3][12] = { "Aisle", "Window", "Leg Room" }; //this makes it possible to do input with a loop cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n"; cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"; //initialize airplane with labels (A, W, L) - all seats available for (int y =0; y < 1; y++) for (int q = 0; q <4; q++) airplane [y][q] = 'L'; //Leg Room Seats for (y = 1; y < 12; y++) { for (int z = 0; z < 4; z++) { for (int x = 0; x < 1; x++) airplane [y][x] = 'W'; //Window Seats for (int v = 1; v < 3; v++) airplane [y][v] = 'A'; //Aisle Seats for (int w = 3; w <4; w++) airplane [y][w] = 'W'; //Window Seats } } //input the Seat prices for (int i = 0; i < 3; i++) { cout << "Please enter price for " << seattypes[i] << " seats: "; cin >> seatprices[i]; cout << endl; } while (response = 'Y') { //display menu of choices clrscr(); cout << "\n\n\n\tC++ Airplane Seating Allocation System" << endl << endl; cout << "\n\t1. View Availabe Seats"; cout << "\n\t2. View Seating Prices"; cout << "\n\t3. View Ticket Sales"; cout << "\n\t4. Purchase a Ticket"; cout << "\n\t5. Exit the Program\n\n"; cout << "\n\tEnter your choice (1-5): "; cin >> choice; switch (choice) { case 1: cout << "Airplane Seating Chart\n"; DisplaySeats(); break; case 2: DisplayPrices(seatprices, seattypes); break; case 3: DisplaySales(); break; case 4: PurchaseTicket(ticketPrice); break; case 5: exit (0); break; default: exit(0); break; } //end switch }; //end while } //end main //Display a seating chart of the airplane void DisplaySeats (void) { clrscr(); cout << "\n\tSEATS\n"; cout << " 1\t2\t3\t4"; cout << endl; for (int r = 0; r < 12; r++) { cout << "\nRow " << setw(2) << r + 1 << "\t"; for (int s =0; s < 4; s++) { cout << airplane[r][s] << "\t"; } } cout << "\n\n\n\tLegend:\t* = Sold"; cout << "\n\t\tA = Aisle Seats (Available)"; cout << "\n\t\tW = Window Seats (Available)"; cout << "\n\t\tL = Leg Room Seats (Available)"; cout << endl; char keyin; cout << endl << endl; cout << "\nPress an alphanumeric key to continue."; cin >> keyin; } //end DisplaySeats void DisplayPrices(float *seatprices, char seattypes[][12]) { clrscr(); cout.precision(2); cout.setf(ios::fixed | ios::right); cout << "\n\tTicket Prices By Seat Types: " << endl << endl; cout << "\tSeat Types Price" << endl; cout << "\t------ -------" << endl << endl; for (int m = 0; m < 3; m++) { cout << "\t" << seattypes[m] << "\t\t\t" << seatprices[m] << endl; cout << endl; } char keyin; cout << endl << endl; cout << "\nPress an alphanumeric key to continue."; cin >> keyin; } void PurchaseTicket(float price[]) { clrscr(); int a, b = 0; char response; cout << "\n\tC++ Airplane Seating Allocation System" << endl << endl; cout << "\t\tTicket Purchase Opportunity" << endl; do { do { cout << "\nPlease enter row number (1-12): "; cin >> a; } while (a <1 || a > 12); do { cout << "\nPlease enter seat number (1-4): "; cin >> b; } while ( b < 1 || b > 4); if (airplane [a-1][b-1] == 'A' || airplane [a-1][b-1] == 'L' || airplane [a-1][b-1] == 'W' || airplane [a-1][b-1] == 'S') //if the seat is available { airplane[a-1][b-1] = '*'; //indicate that the seat is taken totalsales +=ticketPrice[a-1]; cout << "\nYour purchase of Seat " << b << "in Row " << a << "is confirmed." << endl; } else //seat has been sold { cout << "\nSorry. That seat has been sold.\n"; } cout << "\nWould you like to purchase another seat?"; cin >> response; } while (toupper(response) == 'Y'); } void DisplaySales () { cout.setf(ios::fixed); cout << "\n\nTotal Sales to Date: $" << setprecision(2) << totalsales << endl; }
I'm so sorry I'm not any brighter at this. It's really hard to learn this stuff.
Karen
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
You should really try to finish my code. I can help you so dont panic. There was too many errors in your original code to make it worthwhile to fix rather than rewrite. Also your design doesn't lend to code brevity. Notice in mine all the data you need to do any calculations is all stored in an array of structs. I gave in the comments a little info on structs, certainly enough for you to be able to grasp what i had written. Notice how it makes things easy and cuts down in general on the amount of loops you need to write.It enhances code readability and therefore makes it easier for you to understand.If there is any part of my code you dont understand just point it out and ill help. Try first to write the AllocSeat function. You ask for wanted seattype. You can then iterate the array for the first of that seattype whose taken member is also false. Set taken to true or call Sorry() and that ones done. There is a few examples of iterating that array of structs already in the code so that shouldn't phase you. There are also examples of comparing strings and assigning struct members. Most of the code is already written for you. Theres only a few funcs to add and maybe one or two to add a line to.
Dont put yourself down for not being bright. I know quite a few people who describe themselves in that manner and some of those are very good coders. Slow and steady is better than fast and falling flat on your face. C++ is hard to learn, I wont lie to you but its not impossible to grasp the simple constructs I have shown you here. std::strings are a much easier type to work with than char array strings and should be taught before the latter. There is enough information in the code to allow you to use the struct array confidently. loops ifs and function calls are about all you need. If you can follow what I have written then you can finish it. I'll be around for about 4 hrs from time of this post.
Dont put yourself down for not being bright. I know quite a few people who describe themselves in that manner and some of those are very good coders. Slow and steady is better than fast and falling flat on your face. C++ is hard to learn, I wont lie to you but its not impossible to grasp the simple constructs I have shown you here. std::strings are a much easier type to work with than char array strings and should be taught before the latter. There is enough information in the code to allow you to use the struct array confidently. loops ifs and function calls are about all you need. If you can follow what I have written then you can finish it. I'll be around for about 4 hrs from time of this post.
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
OK, I took your advice and gave it a try.
This is my best (miserable) attempt.
C++ Syntax (Toggle Plain Text)
void AllocSeat(string type) { int a, b; do { cout << "What type of seat would you like?\n"; cin >> type; if (type == "legroom") { do { for (int y =0; y < 1; y++) for (int q = 0; q <4; q++) cout << "\nPlease enter seat number (1-4): "; cin >> b; if (string(b, 'L') == 0) { seatmap[y][b] == '*'; } else { Sorry(); } } while ( b < 1 || b > 4); } if (type == "aisle") { do { for (int z = 0; z < 4; z++) { for (int v = 1; v < 3; v++) { cout << "\nPlease enter row number (1-12): "; cin >> a; } while (a <1 || a > 12); do { cout << "\nPlease enter seat number (2-3): "; cin >> b; if (string(b, 'A') ==0) { seatmap[z][b] == '*'; } else { Sorry(); } while ( b < 1 || b > 4); } } if (type == "window") { do { for (int z = 0; z < 4; z++) { for (int x = 0; x < 1; x++) for (int w = 3; w <4; w++) { cout << "\nPlease enter row number (1-12): "; cin >> a; } while (a <1 || a > 12); do { cout << "\nPlease enter seat number (1 or 4): "; cin >> b; if (string(b, 'W') ==0) { seatmap[z][b] == '*'; } else { Sorry(); } } while ( b < 1 || b > 4); } } }
This is my best (miserable) attempt.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
now compare yours with mine. Ive changed a couple of things, displaying now displays row and seatnumbers. Sorry has gone. There are 2 overloads of AllocSeat. one lets you pick a specific seat and another a type of seat. Have you covered overloading of functions yet?
Starting to see how this is coming together?
Starting to see how this is coming together?
C++ Syntax (Toggle Plain Text)
// 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<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 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; return; } cout<<endl<<"Sorry but that particular seat is already taken!!!"<<endl; } void Menu() { clrscr(); // exercise for reader } 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; } } int main() { InitSeatMap(); // exercise for reader return 0; }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
yes you are allowed to do that its called function overloading. If we called it like this...
string s="legroom";
AllocSeat(s);
then the one that takes a string will be called. This will automatically search for the first available seat of that type and allocate it. I should have added a message telling you seatnumber and row, you can do that i am sure.
Alternatively if we call it like so
int row,seatnum;
cout<<"enter row and seatnumber separated by a space"<<endl;
cin>>row>>seatnum;
AllocSeat(row,seatnum);
then that overload will try to take the specific seat pointed at by row/seatnum.
So you should now be able to write a function that asks user whether he would like to pick a seat or auto get one and call the correct overload of AllocSeat.
Again dont panic, I know this is due soon but we will get it done and my hope is I teach you a little something on the way too.
string s="legroom";
AllocSeat(s);
then the one that takes a string will be called. This will automatically search for the first available seat of that type and allocate it. I should have added a message telling you seatnumber and row, you can do that i am sure.
Alternatively if we call it like so
int row,seatnum;
cout<<"enter row and seatnumber separated by a space"<<endl;
cin>>row>>seatnum;
AllocSeat(row,seatnum);
then that overload will try to take the specific seat pointed at by row/seatnum.
So you should now be able to write a function that asks user whether he would like to pick a seat or auto get one and call the correct overload of AllocSeat.
Again dont panic, I know this is due soon but we will get it done and my hope is I teach you a little something on the way too.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Image manipulations(newbie)
- Next Thread: Need hellp with my array
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





