Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. /// Reservation.cpp
  2. // Qian Lao
  3. // 01/15/2019
  4. // CIS-054-101 C/C++ Programming
  5. // Inputs: number of rows, and seats; seat selection
  6. // Outputs: reserve seats, check if seat is already taken, check if all seats are taken
  7.  
  8. #include <iostream>
  9. #include <cctype>
  10. using namespace std;
  11.  
  12. // Function declarations (prototypes)
  13. char **CreateArrayOfSeats (int NumberOfRows, int seats);
  14. void InitializeSeats (char **ArrayOfSeats, int NumberOfRows, int seats);
  15. void DisplayArrayOfSeats (char **ArrayOfSeats, int NumberOfRows, int seats);
  16. void MemoryCleanup (char **ArrayOfSeats, int NumberOfRows, int seats);
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.     char **ArrayOfSeats;
  21.     int NumberOfRows;
  22.     int NumberOfSeats;
  23.     char rowSelection;  // 1 to max NumberOfRows, input from the user
  24.     char seatSelection; // 'A' to max seats, input from the user, convert to a number
  25.     int row;    // index into ArrayOfSeats, 0 to NumberOfRows-1
  26.     int seat;   // index into ArrayOfSeats, 0 to seats-1
  27.     char answer;
  28.        int counter=0;
  29.  
  30.     // get the number of NumberOfRows and seats from the user
  31.     cout << "Enter the number of NumberOfRows: ";
  32.     cin  >> NumberOfRows;
  33.     cout << "Enter the number of seats on each row: ";
  34.     cin  >> NumberOfSeats;
  35.  
  36.     ArrayOfSeats = CreateArrayOfSeats(NumberOfRows, NumberOfSeats);
  37.     InitializeSeats (ArrayOfSeats, NumberOfRows, NumberOfSeats);
  38.     DisplayArrayOfSeats  (ArrayOfSeats, NumberOfRows, NumberOfSeats);
  39.  
  40.     do
  41.     {
  42.  
  43.         cout << endl << "Enter a seat selection" << endl << "  (example 5F -or- 00 to quit): ";
  44.         cin  >> rowSelection;       // get row from the user
  45.         cin  >> seatSelection;      // get the seat from the user
  46.         if (rowSelection=='0')
  47.             continue;               // skip the rest of the loop
  48.  
  49.         seatSelection = toupper(seatSelection); // convert to upper case
  50.         row = rowSelection - '1';     // count from zero to work with the array
  51.         seat = seatSelection - 'A';   // convert 'A' to 0, 'B' to 1, 'C' to 2, etc.
  52.  
  53.         if ((row<0||row>NumberOfRows)||(seat<0||seat>NumberOfSeats))
  54.             cout<<"Invalid input";
  55.  
  56.  
  57.  
  58.         if (ArrayOfSeats [row][seat]=='-')
  59.             cout<<"This seat is taken"<<endl;
  60.         else ArrayOfSeats [row][seat]='-';
  61.  
  62.  
  63.  
  64.         cout<<"Do you want to select another seat? Y/N "<<endl;
  65.         cin>>answer;
  66.  
  67.  
  68.         counter++;
  69.             if (counter==NumberOfRows*NumberOfSeats)
  70.                 {cout<<"All seats are taken"<<endl;
  71.                 DisplayArrayOfSeats  (ArrayOfSeats, NumberOfRows, NumberOfSeats);
  72.                 return 0;}
  73.  
  74.  
  75.  
  76.         DisplayArrayOfSeats (ArrayOfSeats, NumberOfRows, NumberOfSeats);
  77.  
  78.     } while ((rowSelection != '0')&&(answer=='Y'||answer=='y')&&(counter<=NumberOfRows*NumberOfSeats));
  79.  
  80.     MemoryCleanup (ArrayOfSeats, NumberOfRows, NumberOfSeats);   // return the memory
  81.  
  82.     return 0;
  83. }
  84.  
  85. char **CreateArrayOfSeats (int NumberOfRows, int seats)    // ** means pointer to pointers
  86. {
  87.     char **ArrayOfSeats;
  88.     ArrayOfSeats = new char*[NumberOfRows];          // create array of pointers for NumberOfRows
  89.     for(int r = 0; r < NumberOfRows; r++)
  90.         ArrayOfSeats[r] = new char[seats];   // create an array of seats for each row
  91.     return ArrayOfSeats;                     // return pointer to the array back to main program
  92. }
  93.  
  94. void InitializeSeats (char **ArrayOfSeats, int NumberOfRows, int seats)
  95. {
  96.     for (int r=0; r<NumberOfRows; r++)          // initialize the data for each row
  97.     {
  98.         for (int s=0; s<seats; s++)
  99.             ArrayOfSeats[r][s] = 'A' + s;    // put 'A' 'B' 'C' etc in each row
  100.     }
  101. }
  102.  
  103. void DisplayArrayOfSeats (char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
  104. {
  105.     for (int r=0; r<NumberOfRows; r++)              // for each row
  106.     {
  107.         cout.width(2);
  108.         cout << r+1 << ' ';                 // Display row numbers starting from 1
  109.         for (int s=0; s<NumberOfSeats; s++)
  110.             cout << ArrayOfSeats[r][s] << ' ';   // Display info for each seat
  111.         cout << endl;                       // new line after each row
  112.     }
  113. }
  114.  
  115. void MemoryCleanup (char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
  116. {
  117.     for (int r=0; r<NumberOfRows; r++)
  118.         delete [] ArrayOfSeats[r];   // delete each row of seats individually
  119.     delete [] ArrayOfSeats;          // delete the row array
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement