//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; }
// 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; }
//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; }
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); } } }
// 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; }
| DaniWeb Message | |
| Cancel Changes | |