Write a simple bus seat reservation program. The bus has 10 rows, with 4 seats in each row. The program should perform the following: Step 1. The program should ask the user to enter what row and seat number to reserve. Indicate an ‘X’ mark if the seat is reserved. Step 2. Repeat Step 1; the program will stop until the user enters a negative number.
#include<iostream>
#include<vector>
using namespace std;
class Bus
{
vector< vector<char> >busSeats;
public:
Bus():busSeats(10,vector<char>(4))
{
for(int i=0;i<10;i++)
{
for(int j=0;j<4;j++)
{
busSeats[i][j]='0';
}
}
}
int ReservePlace()
{
int i,j;
cout<<"\nPlease, enter the number of row (0 or negative is exit): ";
cin>>i;
if(i<=0)
return -1;
cout<<"Please, enter the number of seat (0 or negative is exit): ";
cin>>j;
if(j<=0)
return -1;
if(i>10||j>4)
cout<<"Incorrect seat!";
else if(busSeats[i-1][j-1]=='X')
cout<<"Seat has been already reserved!";
else
busSeats[i-1][j-1]='X';
return 0;
}
void Display()
{
cout<<"\nSeating plan in the bus:\n";
cout<<"Seat: 1 2 3 4\n";
for(int i=0;i<10;i++)
{
if(i!=9)
cout<<"Row "<<i+1<<": ";
else
cout<<"Row "<<i+1<<": ";
for(int j=0;j<4;j++)
{
cout<<busSeats[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Bus b;
int ext;
cout<<"***You are in bus seat reservation program***";
do
{
b.Display();
ext=b.ReservePlace();
}while(ext==0);
}
Comments
Leave a comment