Thread: Reservation Code: Verify that a valid row and seat were entered

  1. #1
    Registered User
    Join Date
    Jun 2014
    Location
    California
    Posts
    9

    Question Reservation Code: Verify that a valid row and seat were entered

    So I may or may not spoon feed but I need to Verify that a valid row and seat were entered to See if all the seats are taken.
    My hint is keep a count each time a seat is taken and compare to the maximum number of seats (NumberOfRows*seats), See if the seat selection is already taken (see if it has an '-'), if it has an '-', display a message that the seat is taken. else, put an '-' in that location
    So my question is if anyone can help assist me in this missing piece, I've managed to make it this far, but need more ideas to adjust this validation. it's towards the center where I have written myself some hints, it probably dumb but I need a quick aid. (:
    Code:
     #include <iostream>
    #include <cctype>
    using namespace std;
    
    
    // Declaration of each function (prototypes) //
    char ** CreateArrayOfSeats (int NumberOfRows, int SeatsOnRow);
    void InitializeSeats (char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
    void DisplayArrayOfSeats (char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
    void MemoryCleanup (char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
    
    
    int main(int argc, char* argv[])
    {
        char **ArrayOfSeats;
        int NumberOfRows;
        int NumberOfSeats;
        char answer;    // user input for "Do you want another seat?" //
        
        // Get the number of NumberOfRows and SeatsOnRow from the user //
        
        cout << "Enter the number of NumberOfRows: ";
        cin  >> NumberOfRows;
        cout << "Enter the number of seats on each row: ";
        cin  >> NumberOfSeats;
        
        ArrayOfSeats = CreateArrayOfSeats(NumberOfRows, NumberOfSeats);
        InitializeSeats (ArrayOfSeats, NumberOfRows, NumberOfSeats);
        DisplayArrayOfSeats  (ArrayOfSeats, NumberOfRows, NumberOfSeats);
        
        do
        {
            int  rowSelection;  // 1 to max NumberOfRows, input from the user //
            char seatSelection; // 'A' to max seats, input from the user, convert to a number //
            int row;    // index into ArrayOfSeats, 0 to NumberOfRows-1 //
            int seat;   // index into ArrayOfSeats, 0 to seats-1 //
            
            cout << "Enter a seat selection (example 4E): ";
            cin  >> rowSelection;       // get row from the user
            cin  >> seatSelection;      // get the seat from the user
            seatSelection = toupper(seatSelection); // convert to upper case
            row = rowSelection - 1;     // count from zero to work with the array
            seat = seatSelection - 'A'; // convert 'A' to 0, 'B' to 1, 'C' to 2, etc.
            
            // Verify that a valid row and seat were entered
            
            // See if all the seats are taken
            //   hint, keep a count each time a seat is taken
            //   and compare to the maximum number of seats (NumberOfRows*seats)
            // See if the seat selection is already taken (see if it has an '-')
            //   if it has an '-', display a message that the seat is taken
            //   else, put an '-' in that location
            
            
            DisplayArrayOfSeats (ArrayOfSeats, NumberOfRows, NumberOfSeats);
            cout << "Do you want to select another seat (y/n)? ";
            cin  >> answer;
            answer = toupper(answer);
        } while (answer == 'Y');
        
        MemoryCleanup (ArrayOfSeats, NumberOfRows, NumberOfSeats);   // return the memory that was allocated //
        
        cout << "Press the ENTER key to continue...";
        char buff[100];
        cin.getline (buff, 100);
        return 0;
    }
    
    
    char **CreateArrayOfSeats (int NumberOfRows, int SeatsOnRow)    // ** displays pointer to pointers //
    {
        char **ArrayOfSeats;
        ArrayOfSeats = new char*[NumberOfRows];               // Create the array of pointers for the Seats //NumberOfRows
        for(int r = 0; r < NumberOfRows; r++)
            ArrayOfSeats[r] = new char[SeatsOnRow];   // Create an array of SeatsOnRow for each row //
        return ArrayOfSeats;                     // Return pointer to the array back to the main program //
    }
    
    
    void InitializeSeats (char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow)
    {
        for (int r=0; r<NumberOfRows; r++)          // Initialize the data for each row //
        {
            for (int s=0; s<SeatsOnRow; s++)
                ArrayOfSeats[r][s] = 'A' + s;    // put 'A', 'B', 'C', etc.. in each row //
        }
    }
    
    
    void DisplayArrayOfSeats (char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
    {
        for (int r=0; r<NumberOfRows; r++)              // For each row //
        {
            cout.width(2);
            cout << r+1 << ' ';                 // Display row numbers from 1 to the next //
            for (int s=0; s<NumberOfSeats; s++)
                cout << ArrayOfSeats[r][s] << ' ';   // Display info for each seat //
            cout << endl;                       // New line after each row //
        }
    }
    
    
    void MemoryCleanup (char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
    {
        for (int r=0; r<NumberOfRows; r++)
            delete [] ArrayOfSeats[r];   // delete each row of seats individually
        delete [] ArrayOfSeats;          // delete the row array
    }
    Last edited by Jose Vega; 07-15-2014 at 02:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check entered address range is valid
    By mrapoc in forum C Programming
    Replies: 4
    Last Post: 08-04-2012, 07:10 AM
  2. Verify the effectiveness of this code.
    By Tigerbeetweenie in forum C++ Programming
    Replies: 0
    Last Post: 10-10-2010, 01:22 PM
  3. Can someone verify this code?
    By Hunter2 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-29-2004, 10:28 PM
  4. Ignoring data after valid entered
    By wpr101 in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2003, 02:55 PM
  5. Preludes: verify int code
    By RyeDunn in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 01:54 PM