By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

how come i can't view my reservations?? here's my code....

P: 6
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<cctype>
  4.  
  5. using namespace std;
  6.  
  7. struct SEAT {
  8.   bool   occupied;   // initially, false
  9.   string first,last; // passenger name
  10.   int    numBags;    // max of 4
  11.   char   mealType;   // (r)egular, (v)egetaria, (o)ther
  12. };
  13.  
  14.  
  15.  
  16. const int ROWS= 20;
  17. const int COLS= 10;
  18.  
  19. int menuchoice();
  20. void clearPlane(SEAT p[][COLS]);
  21. void showPlane(SEAT p[][COLS]);
  22. bool getSeat(SEAT p[][COLS], int &row, int &col);
  23. void getSeatFromUser(SEAT p[][COLS], int &row, int &col);
  24. void getInfo(SEAT s[][COLS], int, int);
  25. void showInfo(SEAT s[][COLS], int, int);
  26. void makeReservation(SEAT p[][COLS]);
  27. void cancelReservation(SEAT p[][COLS]);
  28. void viewReservations(SEAT p[][COLS]);
  29.  
  30. int main()
  31. {
  32.   int choice;             // user's menu choice
  33.   SEAT plane[ROWS][COLS]; // reservation info for entire plane
  34.  
  35.   clearPlane(plane);
  36.   showPlane(plane); 
  37.   do
  38.   {
  39.     choice= menuchoice();
  40.     switch (choice) {
  41.       case 1: makeReservation(plane); break;
  42.       case 2: cancelReservation(plane); break;
  43.       case 3: viewReservations(plane); break;
  44.       case 4: break;
  45.     } 
  46.   }while(choice !=4);
  47.   return 0;
  48. }
  49.  
  50.  
  51.  
  52. /* menuchoice
  53. **-----------------
  54. ** displays menu and get's user's choice
  55. **--------------------------------------------------------------------*/
  56. int menuchoice()
  57. {
  58.   int choice;
  59.  
  60.   cout << endl << endl;
  61.   cout << "+------------------------+\n";
  62.   cout << "|    P.Metal Airlines    |\n";
  63.   cout << "+------------------------+\n";
  64.   cout << "| [1] Reserve a Seat     |\n";
  65.   cout << "| [2] Cancel Reservation |\n";
  66.   cout << "| [3] List Reservations  |\n";
  67.   cout << "| [4] Exit Program       |\n";
  68.   cout << "+------------------------+\n";
  69.  
  70.  
  71.  do {          
  72.     cout << "Choice: ";
  73.     cin >> choice;
  74.     if(!cin)
  75.         cin.clear();
  76.   } while (choice < 1 || choice > 4);
  77.   return choice;
  78. }
  79.  
  80. //Tabulated Seats
  81. void showPlane(SEAT p[][COLS])
  82. {
  83.   int i,j;
  84.   cout << "                    1                   2\n";
  85.   cout << "  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0\n";
  86.   for (j=0; j<COLS; j++) {
  87.     cout << (char) (j+65);
  88.     for (i=0; i<ROWS; i++)
  89.       if (p[i][j].occupied)
  90.         cout << " x";
  91.       else
  92.         cout << "  ";
  93.     cout << endl;
  94.   }
  95. }
  96.  
  97.  
  98. /* clearPlane
  99. **-----------------
  100. ** empties all reservations in airplane
  101. **--------------------------------------------------------------------*/
  102. void clearPlane(SEAT p[][COLS])
  103. {
  104.   int i,j;
  105.   for (i=0; i<ROWS; i++)
  106.     for (j=0; j<COLS; j++)
  107.       p[i][j].occupied= false;
  108. }
  109.  
  110.  
  111. /* getSeat
  112. **-----------------
  113. ** obtains desired row and column number for a seat; the seat can be
  114. ** selected by the user or can be selected automatically by the
  115. ** computer; the function return true if an open seat was found;
  116. ** false otherwise.
  117. **--------------------------------------------------------------------*/
  118. bool getSeat(SEAT p[][COLS], int &row, int &col)
  119. {
  120.   bool allTaken;   // true if all seats on plane are occupied
  121.   char method;     // (a)utomatic or (m)anual seat selection
  122.  
  123.   allTaken= true;                             // make sure at least one
  124.   for (row=0; row<ROWS && allTaken; row++)    // seat is available
  125.     for (col=0; col<COLS && allTaken; col++)
  126.       if (!p[row][col].occupied)
  127.         allTaken= false;
  128.   if (allTaken) return false;                 // if not return false
  129.  
  130.   do {
  131.     cout << "Would you like to select a seat (a)utomatically or (m)anually: ";
  132.     cin >> method;
  133.   } while (tolower(method)!='a' && tolower(method)!='m');
  134.  
  135.   switch (tolower(method)) {
  136.     case 'a': for (row=0; row<ROWS; row++)   // (a)utomatic seat selection
  137.                 for (col=0; col<COLS; col++)
  138.           if (!p[row][col].occupied)
  139.                     return true;
  140.           break;    // this break is redundant, we hope
  141.     case 'm': getSeatFromUser(p,row,col);    // (m)anual seat selection
  142.               return !p[row][col].occupied;
  143.   }
  144.  
  145.   return false; // we should never get here
  146. }
  147.  
  148.  
  149. /* getSeatFromUser
  150. **-----------------
  151. ** obtains desired row and column number for a seat; this function
  152. ** will be called from getSeat when requesting a seat for making
  153. ** reservations and will be called directly when selecting a
  154. ** seat for cancellation.
  155. **--------------------------------------------------------------------*/
  156. void getSeatFromUser(SEAT p[][COLS], int &row, int &col)
  157. {
  158.   char let;
  159.  
  160.   showPlane(p);
  161.   do {
  162.     cout << "Enter row (1.." << ROWS << "): ";
  163.     cin >> row;
  164.   } while (row < 1 || row > ROWS);
  165.  
  166.   do {
  167.     cout << "Enter col (A.." << (char) (COLS+64) << "): ";
  168.     cin >> let;
  169.     let= toupper(let);
  170.   } while (let < 'A' || let > (COLS+64));
  171.  
  172.   col= let-65;
  173.   row--;
  174. }
  175.  
  176. /*--------------------------------------------------------
  177.  * Accepts the SEAT struct then the user enters in their 
  178.  * information.
  179.  * ------------------------------------------------------*/
  180. void getInfo(SEAT s[][COLS], int row, int col)
  181. {
  182.     cin.ignore(100,'\n');
  183.     char answer;
  184.     do{
  185.  
  186.         cout<<"Enter in this information:\n\n";
  187.  
  188.     //    getSeat(p[][COLS], &row, &col);
  189.  
  190.         cout<<"Enter your first name: ";
  191.  
  192.         cin>>s[row][col].first;
  193.  
  194.         cout<<"Enter your last name: ";
  195.  
  196.         cin>>s[row][col].last;
  197.  
  198.         cout<<"\n";
  199.         do{
  200.             cin.clear();
  201.             cout<<"How many bags will you be bringing?(limit of 4) ";
  202.             cin>>s[row][col].numBags;
  203.             cout<<"\n";
  204.         }while(s[row][col].numBags<0||s[row][col].numBags>4);
  205.  
  206.         do{
  207.             cin.clear();
  208.             cout<<"What type of meal would you like?((c)hicken, (h)am, or (o)ther) ";
  209.             cin>>s[row][col].mealType;
  210.             s[row][col].mealType=tolower(s[row][col].mealType);
  211.             cout<<"\n";
  212.         }while(s[row][col].mealType!='c' && s[row][col].mealType!='h' && s[row][col].mealType!= 'o');
  213.  
  214.     /*    cout<<"Seat: "<<row<<", "<<(char)col+65<<"\n";
  215.         cout<<"Name: "<<SEAT.first<<" "<<SEAT.last<<"\n";
  216.         cout<<"Number of bags: " <<SEAT.numBags<<"\n";
  217.         cout<<"Meal Type: "<<SEAT.mealType<<"\n";*/
  218.  
  219.         showInfo(s, row, col);
  220.         cin.clear();
  221.         cout<<"Is this information correct? ";
  222.         cin>>answer;
  223.  
  224.  
  225.     }while(answer=='n');
  226. }
  227. /*--------------------------------------------------------
  228.  * Displays the information they just entered. 
  229.  * ------------------------------------------------------*/
  230. void showInfo(SEAT s[][COLS], int row, int col)
  231. {
  232.     string chicken="Chicken";
  233.     string ham="Ham";
  234.     string other="Other";
  235.  
  236.     cout<<"Seat: "<<row+1<<", "<<(char)(col+65)<<"\n";
  237.     cout<<"Name: "<<s[row][col].first<<" "<<s[row][col].last<<"\n";
  238.     cout<<"Number of bags: " <<s[row][col].numBags<<"\n";
  239.  
  240.     if(s[row][col].mealType=='c')
  241.         cout<<"Meal Type: "<<chicken<<"\n";
  242.     if(s[row][col].mealType=='h')
  243.         cout<<"Meal Type: "<<ham<<"\n";
  244.     if(s[row][col].mealType=='o')
  245.         cout<<"Meal Type: "<<other<<"\n";
  246. }
  247. /*--------------------------------------------------------
  248.  * Calls showPlane and then will ask what seat they want 
  249.  * and will call getInfo
  250.  * ------------------------------------------------------*/
  251. void makeReservation(SEAT p[][COLS])
  252. {
  253.     int row;
  254.     int col;
  255.     //SEAT person;
  256.     do{
  257.     getSeat(p,row, col);
  258.     }while(p[row][col].occupied==true);
  259.  
  260.     getInfo(p,row,col);    
  261. }
  262. /*--------------------------------------------------------
  263.  * User enters in a seat row and column and if it is empty
  264.  * it will display that. If it is occupied then it will 
  265.  * make the array empty. 
  266.  * ------------------------------------------------------*/
  267. void cancelReservation(SEAT p[][COLS])
  268. {
  269.     int row; 
  270.     int col;
  271.     getSeat(p, row, col);
  272.     if(p[row][col].occupied==false)
  273.         cout<<"The reservation is empty.";
  274.     else
  275.         p[row][col].occupied=false;
  276.  
  277. }
  278. /*--------------------------------------------------------
  279.  * For loop that will go through each seat and if it is 
  280.  * occupied it will display the SEAT struct for that one 
  281.  * then move on.
  282.  * ------------------------------------------------------*/
  283. void viewReservations(SEAT p[][COLS])
  284. {
  285.     int i;
  286.     int n;
  287.     for(i=0; i<10; i++)
  288.                  for(n=0; n<20; n++)
  289.                          if(p[i][n].occupied==true)
  290.                              {
  291.                              cout<<"showInfo(p, i, n)";    
  292.                             }
  293. }
Dec 12 '09 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
My guess is that when you run the program you immediately enter menu option 4 and exit it and it correctly prints nothing.


OK a bit facetious but my point is that you have made no attempt to describe what you entered into the program and what you expected the program to do. All you have done is given the result and that result is actually valid for given program inputs.

Please describe the problem in more detail.



Hmm that seems familiar, that you be because it is exactly what I posted in response here. Nothing has changed since then if you want help you need to describe your problem fully and accurately not just post nearly 300 lines of code and except us to debug it for you. This is pretty much the last friendly warning you will get on this issue, from this point onwards if you do not describe your problems better you are going to get infractions which will ultimately lead to a site ban.
Dec 12 '09 #2

100+
P: 687
You check for p[i][n].occupied==true condition when you print, but never assign .occupied the "true" value.
Dec 12 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.