Hi guys,

Could someone point me in the right direction, please? I'm trying to expand my system to accommodate the reservation of consecutive seat numbers.
The user is prompted for a row number, seat number and then the amount of consecutive seats he would like to book.
For example: The user enters row: 1, seat number: 1 and then 2 to book seats 1 and 2 in row 1. I can reserve a single specified seat, but I'm a little stuck on the consecutive bookings

#include "stdafx.h"
#include <iostream>
using namespace std;

char answer;//made global so promptUser function can access answer

//function to output a visual diagram of the seating plan
//outputs the array with 10 rows and 4 columns
void printDiagram (char seats [][5])
{
	cout<<"\tSEATS"<<endl;//header
	cout<<"\t1 2 3 4"<<endl;//to output header of numbers to table
	
	//looping through the array's rows
	for (int i = 1; i <11; i++)//for loop to output table, starting from row 1
	{	cout<<"Row "<<i<<"\t";
		
		//looping through the columns of the row
		for (int j = 1; j<5; j++)
			cout<<seats[i][j]<<" ";//to ouput the U's and !'s for the rows
			cout<<endl;
	}
}

void bookConsec (char seats[][5])
{
	int row, number, require; //row no, seat no and number of consecutive seats required.
	
	cout<<"Please enter row (1-10),\nthen a seat number (1-4) \nand number of seats required: \n";
			cin>>row;
				while (row<1 || row>10)//while loop to test if row is between 1 and 10
			{
				cout<<"This row doesnt exist, please enter a new row: \t";
				cin>>row;
			}
				cout<<"\n";//used to line up the input for the seat nr
				//cout<< setw(48);
				cin>>number;
					while (number < 1 || number > 4)//while loop to test if seat number is between 1 and 4
			{
				cout<<"This seat doesnt exist, please enter a new seat number: \t";
				cin>>number;
			}
				cout<<"\n";
				cin>>require;
					while (number < 1 || number > 40)//while loop to test if seat number is between 1 and 40
			{
				cout<<"Maximum number of seats allowed for this booking: 40\t";
				cin>>require;
			}
		if ( seats[row][number] == 'U' )//this checks if seat is available, if it is, sets the seat to reserved
			{
				cout<<"Your reservation details: "<<endl<<endl;
				seats[row][number] = '!';//reserved
			}
		else cout<<"Seat already booked. Please select another seat."<<endl<<endl;
}

int main()
{	
	char seats [11][5];		//declare array seats, 10 rows accross, 4 rows down
	char menu;				//variable used to ask if user wants to see the menu again
	int choice;				//variable for menu answer
	
		for (int i = 1; i <11; i++)//initialize all seats to 'U'
			{
				for (int j = 1; j<5; j++)
					seats[i][j] = 'U';
			}
			
	do	//use a do while loop here so user can view many more than one time if necessary
	{
		//Print menu
		cout<<"\n\t\t\tMenu"<<endl;
		cout<<"    ---------------------------------------------"<<endl;
		cout<<"\t1) Reserve consecutive seats"<<endl;
				cin>>choice;
		switch (choice)//switch statement to process user input from the menu above
		{
			case 1:
				{
					bookConsec (seats);//call promptUser function
					printDiagram (seats);//call printDiagram function (prints diagram to screen)
					break;
				}

			case 7:				//function 'main' will end if user enters 7 (return 0)
				return 0;
				break;
			default: return 0;	//Any other numbers entered by the user will end the function
		}
					cout<<endl<<"Do you wish to see the menu again?: (y/n) ";//allows user to view the menu again
					cin>>menu;
					cout<<endl<<endl;
	}
	while (toupper(answer)=='Y');
	return 0;
}

.

So if seats[1][3] ='!', that means that the 4th seat of the 2nd row is taken (adding 1 to array indexes to get row and column)? And that seat is available if seats[1][3] = 'U'? When a seat is reserved, it changes from 'U' to '!'?

So if seats[1][3] ='!', that means that the 4th seat of the 2nd row is taken (adding 1 to array indexes to get row and column)? And that seat is available if seats[1][3] = 'U'? When a seat is reserved, it changes from 'U' to '!'?

Yes, that's spot on. I'm attaching a screendump showing the result of booking the 4th seat in the 2nd row (and requesting 2 consecutive seats)

I believe your problem is in this block of code:

if ( seats[row][number] == 'U' )//this checks if seat is available, if it is, sets the seat to reserved
	{
		cout<<"Your reservation details: "<<endl<<endl;
		seats[row][number] = '!';//reserved
	}

This is where you are assigning seats. The require variable contains the number of seats to be reserved. However you don't use it here. You are only reserving one seat. I would suggest a loop:

for (int i = 0; i < require; i++)
{
     // reserve a seat by changing seats element to '!'
}

Hi VernonDozier,

thank you for your reply. Apologies for yet another question, but I'm still trying to get my head around this. How do I get my variable value and arrays to work together? I've tried the following (among other things) but with no success.

if ( seats[row][number] == '-' )//this checks if seat is available, if it is, sets the seat to reserved
		{
			for (int i = 0; i < require; i++)	
			{
				cout<<"Your reservation details: "<<endl<<endl;
				seats[row][number] +require = '!';//reserved
			}
		}

A few things. One, before assigning ANY consecutive seats, make sure that you've made sure that ALL consecutive seats are available. Don't check them one by one, then assign them as you go. You don't want to assign half of the desired seats, then find out that there are not enough. In that case, you would then have to change the seats you set aside earlier. Don't reserve ANY of the requested seats until you know you can reserve all of them.

As far as your other question, you have at least two cases. One, the simpler one, is where all the desired seats fit into the same row. An example of this is a person asks for three consecutive seats in row 2, starting at seat 2. You'll assign that person row 2, seats 2 through 4. The harder case is if there are not enough seats in the row to accommodate a request. For example, if the person above requested six seats, those seats would spill over to the next row. Let's take the easier example, where they fit in the same row. You'll want to (after checking that all desired seats are available) do something like this:

for (int i = 0; i < require; i++)	
{
	seats[row][number +require] = '!';//reserved
}

So you had it pretty close. Just bring the "require" variable inside the 2nd brackets. Again, this is where is enough space in the row to accommodate the request, but try tackling that first before worrying about row spillover.

Mmwwah! Thanks VernonDozier! The following worked:

for (int i = 0; i < require; i++)	
			{
				cout<<"Your reservation details: "<<endl<<endl;
				seats[row][number+i] = '!';//reserved
			}

Your help is much appreciated. And thanks for the tips on the checking etc that needs to be carried out prior to assigning multiple seats.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.