Answer to Question #290200 in C++ for Chera

Question #290200

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.


1
Expert's answer
2022-01-24T08:03:47-0500
#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);
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment