Hi guys,
I've previously posted a question re a booking system. After major changes I'm glad to say that I've made some progress (Thanks, Mohammad Rastkar)
I'm using a 2D array. When the user books the 4th seat of the 3rd row (adding 1 to array indexes) then seats[2][3] ='U' changes to seats[2][3] ='!' I'm trying to include an option to reserve successive seats. For example, the user enters the row number, seat number and then the number of successive seats required.
#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 rowfor (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
{
for (int i = 0; i < require; i++)
{
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 menucout<<"\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;
}