> C++ Programming?

C++ Programming?

Posted at: 2014-12-18 
I suggested a different category (other than Air Travel) for this question when you asked it earlier. You are far more likely to get a useful answer from the Programming and Design forum.

So what is your question exactly? What do you have trouble understanding? Its hard to answer your question when all you did is post up your assignment.

This is a solution, slightly modified from the assignment with respect to feedback of the program, which random seat was reserved (booked). I hope it helps.

#include

#include

using namespace std;

int get_number(const string& strPrompt, const int& iMin, const int& iMax)

{

int iRet = 0;

string strBuffer;

do {

cout << strPrompt << "> ";

getline(cin, strBuffer);

stringstream ss(strBuffer);

if (ss >> iRet){

if (iRet < iMin || iRet > iMax) {

cout << "Range: " << iMin << " <= x <= " << iMax << endl;

}

else

break;

}

} while(true);

return iRet;

}



bool vacant_spot(int iAr[], int iStart, int iEnd)

{

for (int i = iStart; i < iEnd; i++) {

if (!iAr[i])

return true;

}

return false;

}



char get_yesno(const string& cstrPrompt)

{

string strBuffer;

do {

cout << cstrPrompt << "(y/n)> ";

getline(cin, strBuffer);

}while(::toupper(strBuffer[0]) != 'Y' && ::toupper(strBuffer[0]) != 'N');

return ::tolower(strBuffer[0]);

}



int get_free(int iAr[], int iEnd, int iAdd = 1)

{

int iSeat = -1;

if (vacant_spot(iAr, iAdd, iEnd + iAdd)) {

do {

iSeat = rand() % iEnd + iAdd;

}while(iAr[iSeat]);

}

return iSeat;

}

void menu()

{

int iSelect = 0;

const string strMenu = "Please type 1 for First class\n"

"Please type 2 for Economy class (3 quits)";

int iSeats[11] = {0};

int iSeat = -1;

do {

do {

iSelect = get_number(strMenu, 1, 3);

iSeat = -1;

if (iSelect < 3) {

if (iSelect == 1) {

if ((iSeat = get_free(iSeats, 4, 1)) == -1)

if (vacant_spot(iSeats, 5, 10)) {

if (get_yesno("Is Economy class acceptable") == 'y') {

iSeat = get_free(iSeats, 6, 5);

}

}

}

else {

if ((iSeat = get_free(iSeats, 6, 5)) == -1) {

if (vacant_spot(iSeats, 1, 4)) {

if (get_yesno("Is First class acceptable") == 'y') {

iSeat = get_free(iSeats, 4, 1);

}

}

}

}

}

if (iSeat != -1) {

iSeats[iSeat] = 1;

cout << "You booked seat #" << iSeat << "!" << endl;

}

else {

cout << "next flight leaves in 3 hours!" << endl;

break;

}

}while(!iSeats[iSeat]);

}while(iSelect != 3);

}



int main()

{

menu();

return 0;

}

Write a program for an airline to simulate the reservation system. Your program assigns the seats on each flight of the airline’s only plane (capacity 10 seats). Your program should display the following menu of alternatives:

Please type 1 for First class

Please type 2 for Economy class

If the person types 1, then your program should assign a seat in the First class (1-4). If passenger types 2, then your program should assign a seat in the economy class, seats (5-10.) Seats are assigned randomly, and one seat of course will never assign to two passengers. If the section is full, your program should ask the person if it is acceptable to be placed in the other section, if no, then show a message “next flight leaves in 3 hours.” Your program should then show a boarding pass for a passenger.

Use functions, and array to represent the seating chart of the plane. Initialize all the elements of the array to zero to indicate that the seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate the seat is no longer available.

Boarding pass sample:

Passenger Name Seat Number Flight Number Class