| | |
c++ 2 dimentional array- airplane seating
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 20
Reputation:
Solved Threads: 0
After user choose ticket type(first class, business, or economy) and desired seat, for example row 2 A, the program will output the form:
A B C D E F
row 1 * * * * * *
row 2 X * * * * * // now user chooses row 2 A
... * * * * * *
row 13 * * * * * *
* indicates the seat is available, X indicates the seat is occupied.
my problem is after the program works the first time, then the user wants to do it again( which I use a while loop) , for example , choose row 1 B, the form will show X at row 1 B, but not the row 2 A ( which the first time the user chose) any more.
A B C D E F
row 1 * X * * * *//user this time chooses row 1 B
row 2 * * * * * *//row 2 A that user has chosen isn't showing now
... * * * * * *
row 13 * * * * * *
Can anyone give me some suggestion to fix the problem? Thank you very much.
A B C D E F
row 1 * * * * * *
row 2 X * * * * * // now user chooses row 2 A
... * * * * * *
row 13 * * * * * *
* indicates the seat is available, X indicates the seat is occupied.
my problem is after the program works the first time, then the user wants to do it again( which I use a while loop) , for example , choose row 1 B, the form will show X at row 1 B, but not the row 2 A ( which the first time the user chose) any more.
A B C D E F
row 1 * X * * * *//user this time chooses row 1 B
row 2 * * * * * *//row 2 A that user has chosen isn't showing now
... * * * * * *
row 13 * * * * * *
Can anyone give me some suggestion to fix the problem? Thank you very much.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cctype> #include<iomanip> using namespace std; void getData(char& ticketType, int& row, char& column); void printForm(char form[][6], int row, char column); int main() { char ch, ticketType, column; int row; char form[13][6]; cout << "This program assigns seats for a plane.\n" << "Do you want to start now? Y/y for yes, N/n for no." << endl; cin >> ch; ch = static_cast<char>(toupper(ch)); while(ch == 'Y') { getData(ticketType, row, column); printForm(form, row, column); cout << "This program assigns seats for a plane.\n" << "Do you want to start now? Y/y for yes, N/n for no." << endl; cin >> ch; ch = static_cast<char>(toupper(ch)); if(ch == 'N') return 0; }// end while system("PAUSE"); return 0; } void getData(char& ticketType, int& row, char& column) { cout << "The airplane has 13 rows, with six seats in each row. " << endl; cout << "Enter ticket type,\n" << "F for first class, \n" << "B for business class,\n" << "E for economy class:" << endl; cin >> ticketType; ticketType = static_cast<char>(toupper(ticketType)); while(ticketType != 'F' && ticketType != 'B' && ticketType != 'E') { cout << "Invalid ticket type." << endl; cout << "Enter ticket type,\n" << "F/f for first class, \n" << "B/b for business class,\n" << "E/e for economy class:" << endl; cin >> ticketType; ticketType = static_cast<char>(toupper(ticketType)); } switch(ticketType) { case 'F': cout << "Row 1 and 2 are first class,\n" ; break; case 'B': cout << "row 3 throuh 7 are business class,\n"; break; case 'E': cout << "row 8 through 13 are economy class." << endl; break; }// end switch cout << "Enter the row number you want to sit: " << endl ; cin >> row; cout << "Enter the seat number (from A to F). " << endl; cin >> column; column = static_cast<char>(toupper(column)); } void printForm(char form[][6], int row, char column) { int i, j; cout << "* indicates that the seat is available; " << endl; cout << "X indicates that the seat is occupied. " << endl; cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C" << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl; for(i = 0; i < 13; i++) { cout << left << setw(3) << "Row " << setw(2)<< i+1 ; for(j = 0; j < 6; j++) { if(i == row - 1 && j == static_cast<int>(column)-65) cout << right << setw(6) << "X"; else cout << right << setw(6) << "*"; } cout << endl; } }
•
•
Join Date: Jul 2005
Posts: 1,749
Reputation:
Solved Threads: 282
Try sending form() to getData() and pass row by copy rather than by reference. Within getData() assign the actual seat number by converting input row number and column char to appropriate index numbers for the array. No need to cast anything that I can think of. It will complicate the getData() function a little, but simplify the print function.
Klatu Barada Nikto
•
•
Join Date: Oct 2008
Posts: 45
Reputation:
Solved Threads: 11
•
•
•
•
..problem is after the program works the first time, then the user wants to do it again( which I use a while loop) , for example , choose row 1 B, the form will show X at row 1 B, but not the row 2 A ( which the first time the user chose) any more.
..
C++ Syntax (Toggle Plain Text)
void printForm(char form[][6], int row, char column)
C++ Syntax (Toggle Plain Text)
if(i == row - 1 && j == static_cast<int>(column)-65)
C++ Syntax (Toggle Plain Text)
char form[13][6];
Suppose in the beginning, you assume that all the seats are empty and fill a '*' in all the elements of the 2D form[][].You call this function in the beginning of your program itself, something like this:
C++ Syntax (Toggle Plain Text)
void initialize( char form[][6]) { for(int i=0 ;i < 13 ;i++) for(int j=0 ;j<6 ;j++) form[i][j]='*'; } //and call it in the main() . . . . int main() { . . char form[13][6]; initialize( form) ; //empty seat assignment cout << "This program assigns seats for a plane.\n" << "Do you want to start now? Y/y for yes, N/n for no." << endl; . . }
Next you modify the function
C++ Syntax (Toggle Plain Text)
void printForm(char form[][6], int row, char column) { //Assign the seat to the OP //You can check it seat is already filled //by: //if ( [static_cast<int>(column)-65]== 'X') //{ cout <<"Seat already assigned .Choose another seat"; // return ;} //Here i make crude assumption that the user enters //correct data ,I //mean rows and columns which are possible ones. //You don't check that. //else you assign it form[ row-1 ] [static_cast<int>(column)-65]= 'X'; int i, j; cout << "* indicates that the seat is available; " << endl; cout << "X indicates that the seat is occupied. " << endl; cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C" << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl; //Now you display your matrix for(i = 0; i < 13; i++) { cout << left << setw(3) << "Row " << setw(2)<< i+1 ; for(j = 0; j < 6; j++) { cout << right << setw(6) << form [i][j]; } cout << endl; } }
•
•
Join Date: Mar 2009
Posts: 20
Reputation:
Solved Threads: 0
Here is my updated program which works fine now. I hope it's helpful to others.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cctype> #include<iomanip> using namespace std; void initialize( char form[][6]); void getData(char& ticketType, int& row, char& column); void printForm(char form[][6], int row, char column); int main() { char ch, ticketType, column; int row; char form[13][6]; initialize( form) ; cout << "This program assigns seats for a plane.\n" << "Do you want to start now? Y/y for yes, N/n for no." << endl; cin >> ch; ch = static_cast<char>(toupper(ch)); while(ch == 'Y') { getData(ticketType, row, column); printForm(form, row, column); cout << "This program assigns seats for a plane.\n" << "Do you want to start now? Y/y for yes, N/n for no." << endl; cin >> ch; ch = static_cast<char>(toupper(ch)); if(ch == 'N') return 0; }// end while system("PAUSE"); return 0; } void initialize( char form[][6]) { for(int i=0 ;i < 13 ;i++) for(int j=0 ;j<6 ;j++) form[i][j]='*'; } void getData(char& ticketType, int& row, char& column) { cout << "The airplane has 13 rows, with six seats in each row. " << endl; cout << "Enter ticket type,\n" << "F for first class, \n" << "B for business class,\n" << "E for economy class:" << endl; cin >> ticketType; ticketType = static_cast<char>(toupper(ticketType)); while(ticketType != 'F' && ticketType != 'B' && ticketType && ticketType != 'E') { cout << "Invalid ticket type." << endl; cout << "Enter ticket type,\n" << "F for first class, \n" << "B for business class,\n" << "E for economy class:" << endl; cin >> ticketType; ticketType = static_cast<char>(toupper(ticketType)); } switch(ticketType) { case 'F': cout << "Row 1 and 2 are first class,\n" ; break; case 'B': cout << "row 3 throuh 7 are business class,\n"; break; case 'E': cout << "row 8 through 13 are economy class." << endl; break; }// end switch cout << "Enter the row number you want to sit: " << endl ; cin >> row; cout << "Enter the seat number (from A to F). " << endl; cin >> column; column = static_cast<char>(toupper(column)); } void printForm(char form[][6], int row, char column) { int i, j; if(form[row-1][static_cast<int>(column-65)]=='X') { cout << "This seat already assigned. Choose another seat: " << endl; cin >> column; column = static_cast<char>(toupper(column)); } form[ row-1 ] [static_cast<int>(column)-65]= 'X'; cout << "* indicates that the seat is available; " << endl; cout << "X indicates that the seat is occupied. " << endl; cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C" << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl; for(i = 0; i < 13; i++) { cout << left << setw(3) << "Row " << setw(2)<< i+1 ; for(j = 0; j < 6; j++) { cout << right << setw(6) << form [i][j]; } cout << endl; } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ instanceof
- Next Thread: Conveting file into binary mode
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






