Question: Programming 332- Project #2 Write a program in C++ which will reserve seats for a concert. There are 5 rows of 8 seats, a random

Programming 332- Project #2

Write a program in C++ which will reserve seats for a concert. There are 5 rows of 8 seats, a random access file must be used to keep track of which seats have been reserved. Each seat will have one byte reserved to indicate whether it is taken or not. The seat s in row r is recorded in byte (8*r+s-1) of the file to indicate that the seat is occupied (record an "O") You may create the random access file prior to running the programs, it simply needs to have zero length. Every request will be followed by a conrimatory response, and the date and time should also be recorded. The protections of the data file (concert.dat) are: $touch concert.dat and $chmod uh+lrw concert.dat

Your program should prompt for a function $ Reserve or Display . D results in the display of occupied seats while R prompts for the row number and then a range of sat numbers. Entering Q quits the program. All file entries should be in raw I/O format (using buffers) with error control and boundary checking.

Run 1:

Reserve seats 2-5 in row 1

Reserve seats 4-7 in row 2

Attempt to reserve seat 4 in row 2 - rejected

Reserve seat 3 in row 1

Display reserved seats:

1 2 3 4 5 6 7 8

X O O O O O O O

X X X O O O X X

X X X X X X X X

X X X X X X X X

X X X X X X X X

Here is the template code:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define NROWS 6 /* Row 0 is not used */

#define NSEATS 8

#define ROWS 5

// Prototype functions

void reserve();

void display();

char * prTime()

{

// Printing the Time

static char buf[80];

time_t timeval;

time(&timeval);

strncpy(buf, ctime(&timeval), 24);

return buf;

}

/* Global Variables */

/* char Occupied = 'O' */

// char Occupied[8] = "OOOOOOOO"; worked in Unix, not in Linux!

char Occupied[8] = {'O','O','O','O','O','O','O','O'};

char empty[8] = {'X','X','X','X','X','X','X','X'}; // free seats

int nb, seat, row;

// Buffers for I/0 only,

char oneRow[NSEATS], Seats[NROWS][NSEATS];

int fd; // File Descripter, or pointer

int main() {

// Local Variales

/* variables for I/O buffers */

char buffer[256]; // to get the entire I/O

char choice[2]; // need only 1 Char, but skip endline

char * cptr;

/* other variables */

int temp = 1234567890;

fd = open("concert.dat", O_RDWR); // open for both read or write

if (fd ==-1)

{

fprintf(stderr, "Error, cannot open the file");

exit (1);

}

/* Extend the size of the file be equal to 8*(ROWS+1) */

if (lseek(fd, 8*(ROWS+1), SEEK_SET) == -1)

{

fprintf(stderr, "Error, cannot open the file");

exit (1);

}

/* Print the menue */

printf(" %s> Welcome to the Concert Hall", prTime());

printf(" Menu Choices are: ");

printf(" \t Reserve (enter R)");

printf(" \t Display (enter D)");

printf(" \t Quit (enter Q)");

printf(" ++++++++++++++++++++++++++++++ ");

/* Enter in a loop, get choices, do the repsonse */

while (1)

{

printf(" Reserve or Display> " );

fflush(stdout);

cptr = fgets(buffer, 256, stdin);

if (!cptr) {

fprintf(stderr, "Data entry error ");

exit(1);

}

nb = sscanf(cptr, "%2s", choice);

if (nb!=1) {

fprintf(stderr, "Data entry error ");

exit(1);

}

printf("%s> You entered the letter %c ", prTime(), choice[0]);

if (choice[0] == 'q' || choice[0] == 'Q')

break;

else if (choice[0] == 'r' || choice[0] == 'R')

reserve();

else if (choice[0] == 'd' || choice[0] == 'D')

display();

else {

fprintf(stderr, "Data entry error ");

exit(1);

}

}

close (fd);

return 0;

}

void reserve()

{

char O1='O', O2[2]={'O','O'};

// O2 ="OO";

// Indide the Reservation

fprintf(stderr, "Good Choice: Tell me how many tickets? ");

// Get the inputs

// Check the inputs

// Make reservation

// Just for demo hw to set file positioning

row = 1; seat = 1;

if (lseek(fd, (8*row+seat-1), SEEK_SET) == -1)

{

fprintf(stderr, "Error, cannot open the file");

exit (1);

}

nb = write(fd, Occupied, 8);

// now your turn to make it work!

return;

}

void display()

{

int i;

// Even for display use lseek call

if (lseek(fd, 8, SEEK_SET) == -1)

{

fprintf(stderr, "Error, cannot open the file");

exit (1);

}

// use a look to read each row and disply the file !

return;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!