Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <string>
using namespace std;

void displayRules()
{
	cout<<"  Sensei's Airline'  "<<endl;
	cout<<"--------------------------"<<endl;
	cout<<"How to reserve a ticket: -"<<endl;
	cout<<"FC for First Class       -"<<endl;
	cout<<"BC for Business Class    -"<<endl;
	cout<<"EC for Economy Class     -"<<endl;
	cout<<"--------------------------"<<endl;
	cout<<" "<<endl;
}

void displaySeat(string seatingChar[11][5],string classType);
void seating(string seatingChart[11][5],int rowNum,int seatNum);
void getSeat(string seatingChart[11][5]);
string getClassType();



int rowNum,seatNum;
string classType;

void displaySeat(string seatingChart[11][5],string classType) {
	cout << "Here is our seating chart, you may select from the (" << classType << ") seats." <<endl;
    cout<<"--------------------------"<<endl;
	cout<<"      * = available      -"<<endl;
	cout<<"     X = not available   -"<<endl;
	cout<<"--------------------------"<<endl;
	cout<<" "<<endl;

    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 5; j++) {
            cout << seatingChart[i][j]<< "\t";
        }
        cout << '\n';
    }
    cout << endl;

}
void seating(string seatingChart[11][5],int rowNum,int seatNum) {

    bool seatSelect = true;

    while (seatSelect) {
        if (classType == "FC") {
            if (rowNum > 2) {
                cout << "You have First Class, please select from the 'First Class' seats." << endl;
                getSeat(seatingChart);
                seatSelect = false;
            }
        } else if (classType == "BC") {
            if (rowNum <= 2 || rowNum > 7) {
                cout << "You have Business Class, please select from the 'Business Class' seats." << endl;
                getSeat(seatingChart);
                seatSelect = false;
            }
        } else if (classType == "EC") {
            if (rowNum < 8) {
                cout << "You have Economy Class, please select from the 'EC' seats." << endl;
                getSeat(seatingChart);
                seatSelect = false;
            }
        }
        if (seatingChart[rowNum][seatNum] == "X") {
            cout << "That seat is unavailable, please select again: " << endl;
            getSeat(seatingChart);
            seatSelect = false;
        } else if (seatingChart[rowNum][seatNum] == "*") {
            seatSelect = false;
        }
    }

}

void getSeat(string seatingChart[11][5]) {

    int row;
    char aisle;
    cout << "Please select seat using this format example( '1 A' ): ";
    cin >> row >> aisle;
    rowNum = row;
    seatNum = static_cast<int>(aisle)-125;
    seating(seatingChart,rowNum,seatNum);

}

string getClassType() {
	
	displayRules();
    cout << "Welcome, please enter your ticket type (First Class, Business Class or Economy Class): ";
    getline(cin,classType);

    if (classType == "FC") {
        classType = "First Class";
    } else if (classType == "BC") {
        classType = "Business Class";
    } else {
        classType = "EC";
    }
    return classType;
}

int main()

{

    string seatingChart[11][5] = {
            {"          " ,  " A"," B"," C"," D"},
            {"(FC)Row  1", " *"," *"," X"," *"},
            {"(FC)Row  2", " *"," X"," *"," X"},
            {"(BC)Row  3", " *"," *"," X"," X"},
            {"(BC)Row  4", " X"," *"," X"," *"},
            {"(BC)Row  5", " *"," X"," *"," X"}, //22 available, 18 not available
            {"(BC)Row  6", " *"," X"," *"," *"},
            {"(BC)Row  7", " X"," *"," *"," *"},

            {"(EC)Row  8", " *"," X"," *"," X"},
            {"(EC)Row  9", " X"," *"," X"," X"},
            {"(EC)Row 10", " *"," X"," *"," X"},
    };

    displaySeat(seatingChart,getClassType());

    getSeat(seatingChart);

    cout << "That seat is available, Safe Travels!" << endl;
}


What I have tried:

The static_cast<int>(aisle)-??

i dont know what to set on the minus can someone help me and explain it to me on how. the problem is when I input the seat I want it doesn't send that I occupy that instead the code is freezing.
Posted
Updated 17-May-22 18:41pm

Start by looking at what you are doing:
seatNum = static_cast<int>(aisle)-125;
Why are you subtracting 125? If that was an attempt to convert 'A' to an array index, then it's wrong because the char value for 125 is '}':
ASCII Table: Printable Reference & Guide - αlphαrithms[^]
Use (int) (aisle -'A') instead:
seatNum = (int)(aisle - 'A');
And check the resulting value is between 0 and 4, or your code will break ...

Then it's time to fix the rest of your code, and compiling successfully does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Seeing the literal values 11 and 5 all over the place makes me cringe. That is not a good thing to do. At the least, they should be constants defined at the top of the program. Imagine what would happen if the assignment was changed to have six seats per row and twenty-two rows.

I think one of your problems is this line :
C++
seatNum = static_cast<int>(aisle)-125;
You should look closely at the values you get for aisle because I think they will always be less than 125 so that will result in a negative seatNum. If I understand this correctly, seat numbers can range from 1 to 4 so if A corresponds to 1 then you should try the following :
C++
seatNum = static_cast<int>(aisle) - 'A' + 1;
If I were writing this I would define a class to represent a row of seats. Something like this :
C++
class SeatRows
{
public:
    static const int SeatCount = 4;
    std::string description;
    std::string seats[ SeatCount ];
};
Then using the seating chart could look like this :
C++
const int RowCount = 11;

void displaySeats( SeatRows seatingChart[], string classType )
{
}

int main()
{
   SeatRows chart[ RowCount ];
   std::string classType = getClassType();
   displaySeats( chart, classType );
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900