c++ 2 dimentional array- airplane seating

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 20
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

c++ 2 dimentional array- airplane seating

 
0
  #1
Mar 30th, 2009
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.

  1. #include<iostream>
  2. #include<cctype>
  3. #include<iomanip>
  4.  
  5. using namespace std;
  6.  
  7. void getData(char& ticketType, int& row,
  8. char& column);
  9. void printForm(char form[][6], int row, char column);
  10.  
  11. int main()
  12. {
  13. char ch, ticketType, column;
  14. int row;
  15. char form[13][6];
  16.  
  17. cout << "This program assigns seats for a plane.\n"
  18. << "Do you want to start now? Y/y for yes, N/n for no." << endl;
  19. cin >> ch;
  20.  
  21. ch = static_cast<char>(toupper(ch));
  22. while(ch == 'Y')
  23. {
  24.  
  25. getData(ticketType, row, column);
  26. printForm(form, row, column);
  27.  
  28. cout << "This program assigns seats for a plane.\n"
  29. << "Do you want to start now? Y/y for yes, N/n for no." << endl;
  30. cin >> ch;
  31. ch = static_cast<char>(toupper(ch));
  32. if(ch == 'N')
  33. return 0;
  34. }// end while
  35. system("PAUSE");
  36. return 0;
  37. }
  38.  
  39. void getData(char& ticketType, int& row, char& column)
  40. {
  41.  
  42. cout << "The airplane has 13 rows, with six seats in each row. " << endl;
  43.  
  44. cout << "Enter ticket type,\n"
  45. << "F for first class, \n"
  46. << "B for business class,\n"
  47. << "E for economy class:" << endl;
  48. cin >> ticketType;
  49. ticketType = static_cast<char>(toupper(ticketType));
  50. while(ticketType != 'F' && ticketType != 'B' && ticketType != 'E')
  51. {
  52. cout << "Invalid ticket type." << endl;
  53. cout << "Enter ticket type,\n"
  54. << "F/f for first class, \n"
  55. << "B/b for business class,\n"
  56. << "E/e for economy class:" << endl;
  57. cin >> ticketType;
  58. ticketType = static_cast<char>(toupper(ticketType));
  59. }
  60. switch(ticketType)
  61. {
  62. case 'F':
  63. cout << "Row 1 and 2 are first class,\n" ;
  64. break;
  65. case 'B':
  66. cout << "row 3 throuh 7 are business class,\n";
  67. break;
  68. case 'E':
  69. cout << "row 8 through 13 are economy class." << endl;
  70. break;
  71. }// end switch
  72.  
  73. cout << "Enter the row number you want to sit: " << endl ;
  74. cin >> row;
  75.  
  76. cout << "Enter the seat number (from A to F). " << endl;
  77. cin >> column;
  78. column = static_cast<char>(toupper(column));
  79. }
  80.  
  81. void printForm(char form[][6], int row, char column)
  82. {
  83. int i, j;
  84.  
  85. cout << "* indicates that the seat is available; " << endl;
  86. cout << "X indicates that the seat is occupied. " << endl;
  87. cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
  88. << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl;
  89.  
  90. for(i = 0; i < 13; i++)
  91. {
  92. cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
  93. for(j = 0; j < 6; j++)
  94. {
  95. if(i == row - 1 && j == static_cast<int>(column)-65)
  96. cout << right << setw(6) << "X";
  97. else
  98. cout << right << setw(6) << "*";
  99.  
  100. }
  101. cout << endl;
  102. }
  103. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,749
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 282
Lerner Lerner is offline Offline
Posting Virtuoso

Re: c++ 2 dimentional array- airplane seating

 
0
  #2
Mar 30th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: c++ 2 dimentional array- airplane seating

 
0
  #3
Mar 30th, 2009
..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.
..
that is because int the function
  1. void printForm(char form[][6], int row, char column)
you get into the block only once
  1. if(i == row - 1 && j == static_cast<int>(column)-65)
You don't make the actual change in the
  1. 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:

  1. void initialize( char form[][6])
  2. {
  3. for(int i=0 ;i < 13 ;i++)
  4. for(int j=0 ;j<6 ;j++)
  5. form[i][j]='*';
  6. }
  7. //and call it in the main()
  8. .
  9. .
  10. .
  11. .
  12. int main()
  13. {
  14. .
  15. .
  16. char form[13][6];
  17. initialize( form) ; //empty seat assignment
  18. cout << "This program assigns seats for a plane.\n"
  19. << "Do you want to start now? Y/y for yes, N/n for no." << endl;
  20. .
  21. .
  22.  
  23. }

Next you modify the function
  1. void printForm(char form[][6], int row, char column)
  2. {
  3. //Assign the seat to the OP
  4. //You can check it seat is already filled
  5. //by:
  6. //if ( [static_cast<int>(column)-65]== 'X')
  7. //{ cout <<"Seat already assigned .Choose another seat";
  8. // return ;}
  9.  
  10. //Here i make crude assumption that the user enters
  11. //correct data ,I
  12. //mean rows and columns which are possible ones.
  13. //You don't check that.
  14.  
  15.  
  16. //else you assign it
  17. form[ row-1 ] [static_cast<int>(column)-65]= 'X';
  18. int i, j;
  19.  
  20. cout << "* indicates that the seat is available; " << endl;
  21. cout << "X indicates that the seat is occupied. " << endl;
  22. cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
  23. << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl;
  24. //Now you display your matrix
  25. for(i = 0; i < 13; i++)
  26. {
  27. cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
  28. for(j = 0; j < 6; j++)
  29. {
  30. cout << right << setw(6) << form [i][j];
  31. }
  32. cout << endl;
  33. }
  34. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 20
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

Re: c++ 2 dimentional array- airplane seating

 
0
  #4
Mar 30th, 2009
Thanks zalezog. I have changed my program according to your suggestion and it works perfectly now. : )
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 20
Reputation: YingKang is an unknown quantity at this point 
Solved Threads: 0
YingKang YingKang is offline Offline
Newbie Poster

Re: c++ 2 dimentional array- airplane seating

 
0
  #5
Mar 31st, 2009
Here is my updated program which works fine now. I hope it's helpful to others.
  1. #include<iostream>
  2. #include<cctype>
  3. #include<iomanip>
  4.  
  5. using namespace std;
  6.  
  7. void initialize( char form[][6]);
  8. void getData(char& ticketType, int& row,
  9. char& column);
  10. void printForm(char form[][6], int row, char column);
  11.  
  12. int main()
  13. {
  14. char ch, ticketType, column;
  15. int row;
  16. char form[13][6];
  17.  
  18. initialize( form) ;
  19. cout << "This program assigns seats for a plane.\n"
  20. << "Do you want to start now? Y/y for yes, N/n for no." << endl;
  21. cin >> ch;
  22.  
  23. ch = static_cast<char>(toupper(ch));
  24. while(ch == 'Y')
  25. {
  26.  
  27. getData(ticketType, row, column);
  28. printForm(form, row, column);
  29.  
  30. cout << "This program assigns seats for a plane.\n"
  31. << "Do you want to start now? Y/y for yes, N/n for no." << endl;
  32. cin >> ch;
  33. ch = static_cast<char>(toupper(ch));
  34. if(ch == 'N')
  35. return 0;
  36. }// end while
  37.  
  38. system("PAUSE");
  39. return 0;
  40. }
  41.  
  42. void initialize( char form[][6])
  43. {
  44. for(int i=0 ;i < 13 ;i++)
  45. for(int j=0 ;j<6 ;j++)
  46. form[i][j]='*';
  47. }
  48.  
  49.  
  50.  
  51. void getData(char& ticketType, int& row, char& column)
  52. {
  53. cout << "The airplane has 13 rows, with six seats in each row. " << endl;
  54.  
  55. cout << "Enter ticket type,\n"
  56. << "F for first class, \n"
  57. << "B for business class,\n"
  58. << "E for economy class:" << endl;
  59. cin >> ticketType;
  60. ticketType = static_cast<char>(toupper(ticketType));
  61. while(ticketType != 'F' && ticketType != 'B'
  62. && ticketType && ticketType != 'E')
  63. {
  64. cout << "Invalid ticket type." << endl;
  65. cout << "Enter ticket type,\n"
  66. << "F for first class, \n"
  67. << "B for business class,\n"
  68. << "E for economy class:" << endl;
  69. cin >> ticketType;
  70. ticketType = static_cast<char>(toupper(ticketType));
  71. }
  72. switch(ticketType)
  73. {
  74. case 'F':
  75. cout << "Row 1 and 2 are first class,\n" ;
  76. break;
  77. case 'B':
  78. cout << "row 3 throuh 7 are business class,\n";
  79. break;
  80. case 'E':
  81. cout << "row 8 through 13 are economy class." << endl;
  82. break;
  83. }// end switch
  84.  
  85. cout << "Enter the row number you want to sit: " << endl ;
  86. cin >> row;
  87.  
  88. cout << "Enter the seat number (from A to F). " << endl;
  89. cin >> column;
  90. column = static_cast<char>(toupper(column));
  91.  
  92. }
  93.  
  94. void printForm(char form[][6], int row, char column)
  95. {
  96. int i, j;
  97.  
  98. if(form[row-1][static_cast<int>(column-65)]=='X')
  99. {
  100. cout << "This seat already assigned. Choose another seat: " << endl;
  101. cin >> column;
  102. column = static_cast<char>(toupper(column));
  103. }
  104. form[ row-1 ] [static_cast<int>(column)-65]= 'X';
  105.  
  106. cout << "* indicates that the seat is available; " << endl;
  107. cout << "X indicates that the seat is occupied. " << endl;
  108. cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
  109. << setw(6) << "D" << setw(6) << "E" << setw(6) << "F" << endl;
  110.  
  111. for(i = 0; i < 13; i++)
  112. {
  113. cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
  114. for(j = 0; j < 6; j++)
  115. {
  116. cout << right << setw(6) << form [i][j];
  117. }
  118. cout << endl;
  119. }
  120.  
  121. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC