Assignment 4 Solutions
Assignment 4 Solutions
#include <iostream>
#include <iomanip> //for setw()
#include <string>
using namespace std;
struct travelBooking
{
string travellerName;
string travelClass;
float departureTime;
float arrivalTime;
string seatNo;
};
bool validateTimeOption(int opt)
{
if(opt == 1 || opt == 2 || opt == 3 || opt == 4 || opt == 5)
return true;
else
return false;
}
Open Rubric
if(notBooked == false)
return false;
else
return true;
//called the very first time, for any of the listed times
void displaySeats1()
{
int seatFlag = 0;
int divValue;
char row = 'A';
int col = 1;
divValue = 3;
cout.setf(ios::left);
//displaying the seats
cout << "First Class(";
cout << FLIGHT + FLIGHT * 0.20;
cout << ")" << endl;
cout << "|";
for(int i = 1; i <= 50; i++)
{
if(i == 25)
{
cout << "Economy class(";
cout << FLIGHT;
if(i % divValue == 0)
{
if(seatFlag == 0)
{
cout << "|";
cout << setw(4) << "---- ";
seatFlag = 1;
}
else
{
cout << "|";
cout << endl;
row = row + 1;
seatFlag = 0;
col = 1;
}
}
cout << "|";
}
}
//called from second time onwards, for any particular time
void displaySeats2(travelBooking t[], int timeChoice)
{
int seatFlag = 0;
//int number;
bool notBooked;
int divValue;
char row = 'A';
int col = 1;
divValue = 3;
//float dTime;
string sNo;
cout.setf(ios::left);
//displaying the seats
cout << "First Class(";
cout << FLIGHT + FLIGHT * 0.20;
cout << ")" << endl;
cout << "|";
for(int i = 1; i <= 50; i++)
{
//cout << "Current seat no:" << sNo << endl;
sNo = ""; //very important
notBooked = true;
if(i == 25)
{
cout << "Economy class(";
cout << FLIGHT;
cout << ")" << endl;
cout << "|";
}
sNo += row;
sNo += to_string(col);
//display seats
if(notBooked == false)
{
cout << "**";
col++;
}
else
cout << row << col++;
//arranging the seats for each row
if(i % divValue == 0)
{
if(seatFlag == 0)
{
cout << "|";
cout << setw(4) << "---- ";
seatFlag = 1;
}
else
{
cout << "|";
cout << endl;
row = row + 1;
seatFlag = 0;
col = 1;
}
}
cout << "|";
}
float calcTicketPrice(travelBooking t)
{
float price = 0.0;
return price;
}
void displayTicket(travelBooking t)
{
cout << "\Travel ticket for " << "FLIGHT" << endl;
cout << setw(25) << "*************************" << endl;
cout << setw(15) << "Name" << setw(3) << ":"
<< setw(20) << t.travellerName;
cout << setw(20) << "Travel Ticket class"
<< setw(3) << ":" << t.travelClass << endl;
cout << setw(38) << "" << setw(20) << "Seat No"
<< setw(3) << ":" << t.seatNo << endl;
cout << setw(15) << "Departure" << setw(3) << ":"
<< "Johannesburg";
cout << setw(8) << "" << setw(20) << "Departure Time"
<< setw(3) << ":" << t.departureTime << endl;
//displaying amount
cout << "***********************" << endl;
cout << "Amount:R" << calcTicketPrice(t);
cout << " Thank you for booking with COS1511. "
<< "Your travel agent for queries is "
<< "Annie Mathew" << endl;
cout << "***********************" << endl;
int main()
{
string fullName;
string seatNo;
int depTimeChoice; 16
char answer;
do
{
cout << "Welcome to COS1511 Flight Booking system"
<< endl;
case 3:
bCount = timeCount3;
break;
case 4:
bCount = timeCount4;
break;
case 5:
bCount = timeCount5;
break;
}
if(bCount == 0)
{
displaySeats1(); //first time seat display
cout << "\nPlease key in a seat number "
<< "to choose a seat(eg:A2)" << endl;
}
else
{
//seat display after bookings are made
displaySeats2(traveller, depTimeChoice);
}while(!validateSeat(traveller,seatNo,depTimeChoice));
//display ticket
displayTicket(traveller[i]);
i++;
cout << "Do you want to make another booking(Y/N)?" << endl;
cin >> answer;
cin.get();
//based on time choice increment the counts
if(depTimeChoice == 1)
timeCount1++;
else if(depTimeChoice == 2)
timeCount2++;
else if(depTimeChoice == 3)
timeCount3++;
else if(depTimeChoice == 4)
timeCount4++;
else if(depTimeChoice == 5)
timeCount5++;
}while(toupper(answer) == 'Y');
Output formatting
Give the following marks for each, if the student has demonstrated the use of it fully and correctly.
Marks worth 20:
‐ two‐dimensional array – 2 marks
‐ one‐dimensional array – 2 marks
‐ void function with reference parameter(s) – 2 marks
‐ value‐returning function – 2 marks
‐ void function with value parameter(s) – 2 marks
‐ ‘if’ construct for decision‐making – 2 marks
‐ switch construct for decision – making – 2 marks
‐ while loop construct – 1 marks
‐ do…while loop construct – 2 marks
‐ for loop construct – 2 marks
‐ use of array as function argument ‐ 1