Hey guys,
I've been having a bit of trouble with my c++ program i'm trying to create for an assignment. I'm still learning, but array's seem to give me the most trouble. I've been reading and searching google alot to try and find examples to help my situation, but i'm not quite sure what i've done so far is correct especially with the array side of things. The program compiles correctly but when i go to test adding a passanger name it gives and unhandled exception error. The requirements for the assignment are:
"You are hired to develop a program to be used by airport staff to assign seats for a commercial airplane.
The airplane has 12 rows. Rows 1 and 2 are first class, rows 3 through 5 are business class, and rows 6 through 12 are economy class."
- You must define and implement a class Passenger to model passenger objects and you must use the class in your program.
- The data structure that is used to store Passenger objects must be array.
- The system must be a 2008 or 2010 Microsoft Visual Studio .NET Win32 console application implemented in C++.
- You may add more classes, if you wish.
- You may use any C++ standard libraries, if you wish.
And my code so far is (sorry i haven't commented it yet aswell) :
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
class Passenger
{
string **FirstArray;
static const int firstClassRow = 2;
static const int firstClassColumn = 2;
string **BusArray;
static const int busClassRow = 3;
static const int busClassColumn = 4;
string **EconArray;
static const int econClassRow = 7;
static const int econClassColumn = 6;
static const char Available = '*';
static const char Occupied = '+';
public:
Passenger()
{
FirstArray = new string*[firstClassRow];
for (int i = 0; i < firstClassRow; i++)
FirstArray[i] = new string[firstClassColumn];
BusArray = new string*[busClassRow];
for (int i = 0; i < busClassRow; i++)
BusArray[i] = new string[busClassColumn];
EconArray = new string*[econClassRow];
for (int i = 0; i < econClassRow; i++)
EconArray[i] = new string[econClassColumn];
memset(FirstArray, Available, sizeof FirstArray);
memset(BusArray, Available, sizeof BusArray);
memset(EconArray, Available, sizeof EconArray);
void addPassenger(int rowNumber, int columnNumber);
string getSeat(int rowNumber, int columnNumber);
};
~Passenger() {
for (int i = 0; i < firstClassRow; i++)
delete [] FirstArray[i];
delete [] FirstArray;
for (int i = 0; i < busClassRow; i++)
delete [] BusArray[i];
delete [] BusArray;
for (int i = 0; i < econClassRow; i++)
delete [] EconArray[i];
delete [] EconArray;
}
void Passenger::addPassenger(int rowNumber, int columnNumber) {
if (rowNumber <= 2) {
FirstArray[rowNumber][columnNumber]= "test";
} else if (rowNumber > 2 && rowNumber <= 5) {
BusArray[rowNumber][columnNumber]= "test";
} else {
EconArray[rowNumber][columnNumber]= "test";
}
}
string Passenger::getSeat(int rowNumber, int columnNumber) {
string seat;
if (rowNumber <= 2) {
seat = FirstArray[rowNumber][columnNumber];
} else if (rowNumber > 2 && rowNumber <= 5) {
seat = BusArray[rowNumber][columnNumber];
} else {
seat = EconArray[rowNumber][columnNumber];
}
return seat;
}
void showSeating() {
}
void changePassenger() {
}
void cancelPassenger() {
}
void showReport() {
}
};
void displayMenu();
void reserveSeat();
char columnMaxCheck(int row);
char getColumnNumber(char maxColumn, int row);
int getRowNumber();
Passenger airline;
int main()
{
const int Reserve = 1;
const int Change = 2;
const int Cancel = 3;
const int Report = 4;
const int Quit = 5;
int menuSelection =0;
bool exitApplication = false;
string input = "";
while (!exitApplication) {
displayMenu();
while (true) {
cin >> input;
stringstream myStream(input);
if (myStream >> menuSelection)
break;
cout << "\n *Invalid input, please try again!*"
<< "\n\nPlease select an option (1-5): "<< endl;
}
static_cast<int>(menuSelection);
switch (menuSelection) {
case Reserve:
reserveSeat();
break;
case Change:
break;
case Cancel:
break;
case Report:
break;
case Quit:
cout << "\n * Thanks for using Prestige Flights seat allocation system *";
exitApplication = true;
cin.get();
break;
default:
cout << " *Please input a valid menu option, type only 1, 2, 3, 4 or 5*\n";
cin.get();
}
}
}
void displayMenu()
{
cout << "---------------------------------------------------"
<< "------------------"
<< "\n\n\n\n"
<< "\n *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
<< "\n"
<< "\n Welcome to Prestige Flight Services"
<< "\n Please follow the instructions"
<< "\n for seat allocation"
<< "\n"
<< "\n *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
<< "\n"
<< "\n Seat Allocation Menu"
<< "\n ======================="
<< "\n 1. Reserve a Seat"
<< "\n"
<< "\n 2. Change a Seat"
<< "\n"
<< "\n 3. Cancel a Seat"
<< "\n"
<< "\n 4. Dispaly Seat Report"
<< "\n"
<< "\n 5. Exit"
<< "\n\n\n"
<< "\nPlease select an option (1-5): ";
}
void reserveSeat()
{
int rowNumber;
int columnNumber;
char columnNumberChar;
char columns;
cout << "\n\n The airplane contains 12 rows.\n"
<< " First Class - Rows 1 and 2\n"
<< " Business Class - Rows 3 to 5\n"
<< " Economy Class - Rows 6 to 12\n";
rowNumber = getRowNumber();
columns = columnMaxCheck(rowNumber);
columnNumberChar = getColumnNumber(columns, rowNumber);
if (columnNumberChar == 'A') {
columnNumber = 0;
} else if (columnNumberChar == 'B') {
columnNumber = 1;
} else if (columnNumberChar == 'C') {
columnNumber = 2;
} else if (columnNumberChar == 'D') {
columnNumber = 3;
} else if (columnNumberChar == 'E') {
columnNumber = 4;
} else {
columnNumber = 5;
}
airline.addPassenger((rowNumber-1),columnNumber);
}
char columnMaxCheck(int row) {
char columns;
if (row <= 2) {
columns = 'B';
}
else if (row > 2 && row <= 5) {
columns = 'D';
}
else if (row > 5 && row <= 12) {
columns = 'F';
}
return columns;
}
char getColumnNumber(char maxColumn, int row) {
char columnNumber;
cout << "\nPlease enter your desired seat number (A - " << maxColumn << "):" << endl;
cin >> columnNumber;
columnNumber = static_cast<char>(toupper(columnNumber));
if (row <= 2) {
while(columnNumber != 'A' && columnNumber != 'B')
{
cout << "\n * Invalid seat number, seat must be between (A - " << maxColumn << ") *";
cout << "\n\nPlease enter your desired seat: " << endl;
cin >> columnNumber;
columnNumber = static_cast<char>(toupper(columnNumber));
}
} else if (row > 2 && row <= 5) {
while(columnNumber != 'A' && columnNumber != 'B' && columnNumber != 'C'
&& columnNumber != 'D')
{
cout << "\n * Invalid seat number, seat must be between (A - " << maxColumn << ") *";
cout << "\n\nPlease enter your desired seat: " << endl;
cin >> columnNumber;
columnNumber = static_cast<char>(toupper(columnNumber));
}
} else if (row > 5 && row <= 12) {
while(columnNumber != 'A' && columnNumber != 'B' && columnNumber != 'C'
&& columnNumber != 'D' && columnNumber != 'E' && columnNumber != 'F')
{
cout << "\n * Invalid seat number, seat must be between (A - " << maxColumn << ") *";
cout << "\n\nPlease enter your desired seat: " << endl;
cin >> columnNumber;
columnNumber = static_cast<char>(toupper(columnNumber));
}
}
return columnNumber;
}
int getRowNumber() {
int rowNumber = 0;
const int firstRow = 1;
const int lastRow = 12;
string input = "";
bool notValidRowOption = true;
while (notValidRowOption == true) {
while (true) {
cout << "\nPlease enter your desired row: " << endl;
cin >> input;
stringstream myStream(input);
if (myStream >> rowNumber)
break;
cout << "\n * Invalid input, please enter a number! *" << endl;
}
if (rowNumber < firstRow || rowNumber > lastRow) {
cout << "\n * Invalid input, number must be between 1-12! *\n";
} else {
notValidRowOption = false;
}
}
return rowNumber;
}
Any help with this would be greatly appreciated.
Thanks so much guys!
Luke