-2

So I'm making an airplane seating program and I want my user to enter a number to choose a row and enter a letter for to choose one of the six choices in a row (A to F). I also want the user to keep entering seats as long as they input the letter "C". I have most of the code written but I can't make it output for some reason the seats are not outputting after initializing and displaying them for the first time. The program just stops. I'm trying to output the result before correcting any mistakes I've made in the logic.

//Any unused includes are part of the default code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cassert>

using namespace std;

const int rowsz = 5;
const int colsz = 6;

int main()
{
    char seating[rowsz][colsz];
    char y = ' '; //Row
    char x = ' '; //Col
    char taken = 'X'; // This letter is for marking booked seats
    char letter = ' '; // This letter initializes the seat positions.
    char let = ' '; // Choice for a seat
    char choice = 'c'; // This let's the user quit or continue booking seats
    int rownum = 1; // Row number
    for(int row = 1; row <= rowsz; row++)
    {
        letter = 'A';
        cout << rownum;
        rownum++;
        for(int col = 0; col < colsz; col++)
        {
            seating[row][col] = letter;
            letter++;
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Would you like to get a seat? Press C. TO quit, press Q: "; cin >> choice;
    while(toupper(choice) == 'C')
    {
        cout << "Enter a row. Ex: 1,2,3... ";cin >> y;
        cout << "Enter a Letter for a seat: "; cin >> x;

        if(toupper(x) == 'A')
            x = 0;
        if(toupper(x) == 'B')
            x = 1;
        if(toupper(x) == 'C')
            x = 2;
        if(toupper(x) == 'D')
            x = 3;
        if(toupper(x) == 'E')
            x = 4;
        if(toupper(x) == 'F')
            x = 5;

        seating[y][x] = taken;

        for(int row = 1; row <= rowsz; row++)
        {
            for(int col = 0; col < colsz; col++)
            {
                cout << seating[row][col];
            }
            cout << endl;
        }
        cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;
    }
    if(toupper(choice) != 'C')
    {
        cout << "Thank you for using this program! " << endl;
    }

    return 0;
}

If anyone is still reading this old post, I managed to get it working a long time ago:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cassert>

using namespace std;

//const int 5 = 5;
//rconst int 6 = 6;

int main()
{
    char seating[5][6];
    int y = 0; //Row
    int x = 0; //Col
    char letter = ' '; // This letter initializes the seat positions.
    char let = ' '; // Choice for a seat
    char choice = 'c'; // This let's the user quit or continue booking seats
    int rownum = 1; // Row number

    for(int row = 0; row < 5; row++)
    {
        letter = 'A';
        cout << rownum;
        rownum++;
        for(int col = 0; col < 6; col++)
        {
            seating[row][col] = letter;
            letter++;
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Would you like to get a seat? Press C. TO quit, press Q: "; cin >> choice;
    while(toupper(choice) == 'C')
    {
        cout << "Enter a row. Ex: 1,2,3... ";cin >> x;

        if(x <= 0 || x > 5){
            cout << "Try again: "; cin >> x;
        }

        cout << "Enter a Letter for a seat: "; cin >> let;


        if(toupper(let) == 'A'){
            y = 0;
        }
        else if(toupper(let) == 'B'){
            y = 1;
        }
        else if(toupper(let) == 'C'){
            y = 2;
        }
        else if(toupper(let) == 'D'){
            y = 3;
        }
        else if(toupper(let) == 'E'){
            y = 4;
        }
        else if(toupper(let) == 'F'){
            y = 5;
        }
        else{
            cout << "Wrong Input: " << endl;
            cin >> let;
        }

        while(seating[x][y] == 'X')
        {
            cout << "Try again? Row:" << endl;
            cin >> x;
            cout <<" and letter: " << endl;
            cin >> let;
        }

        seating[x - 1][y] = 'X';

        rownum = 1;
        for(int row = 0; row < 5; row++)
        {
            cout << rownum;
            rownum++;
            for(int col = 0; col < 6; col++)
            {
                cout << seating[row][col];
            }
            cout << endl;
        }
        cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;
    }
    if(toupper(choice) != 'C')
    {
        cout << "Thank you for using this program! " << endl;
    }

    return 0;
}
Rob
  • 43
  • 6
  • 1
    An array `T array[N];` has valid indexes from `0` to `N - 1`. `for(int row = 1; row <= rowsz; row++)` ==> `for(int row = 0; row < rowsz; row++)` – Swordfish Oct 22 '18 at 20:19
  • 1
    If you are using ASCII encoding, you could simplify your `if` statement to `x = toupper(x) - 'A';`. – Thomas Matthews Oct 22 '18 at 20:20
  • Suggestion: use variable names `row` and `column` instead of `y` and `x`. The names `row` and `column` make the code easier to read (they only add negligible time to the compilation). – Thomas Matthews Oct 22 '18 at 20:22
  • You converted `x` from `'A'` to `'F'` to a number `0` to `5`. You missed to do something similar with `y` ... the value of `'1'` is `49` but you want to use it as an index into an array ... – Swordfish Oct 22 '18 at 20:39
  • change both `for(int row = 1; row <= rowsz; row++)`s to `for(int row = 0; row < rowsz; row++)` and your program will no longer crash, however, it will not offer your desired results, when the seats print after booking one, no 'X' marker shows, as if the reservation is not done. Basically, it doesn't save to your array. – Ahmed Aboumalek Oct 22 '18 at 20:50
  • @AhmedAboumalek "your program will no longer crash" is a very long shot for code with undefined behaviour. – Swordfish Oct 22 '18 at 20:52
  • @Swordfish I ran the code after the mentioned edits, and it does not crash. Unless the OP has more issues in his program which are not in this snippet. I am only talking about this code sample, that is all. – Ahmed Aboumalek Oct 22 '18 at 20:55
  • @AhmedAboumalek After changing the `for`-loops it still has undefined behaviour. – Swordfish Oct 22 '18 at 20:57
  • @Swordfish in what sense is it undefined? please elaborate. Aside from user input mistakes that is. – Ahmed Aboumalek Oct 22 '18 at 20:58
  • @AhmedAboumalek `seating[y]` is out of bounds access of an array. – Swordfish Oct 22 '18 at 20:59
  • @Swordfish right, due to memory access restriction, C++ doesn't allow editing individual values inside a char array because they are treated as constants. This one is a thinker. – Ahmed Aboumalek Oct 22 '18 at 21:18
  • @AhmedAboumalek There is no `char` array which is constant in this code. The code simply accesses the array `seating` with indexes that aren't valid indexes for that array. – Swordfish Oct 22 '18 at 21:33
  • @Swordfish what's that then `char seating[rowsz][colsz]` if not a multidimentional `char` array? – Ahmed Aboumalek Oct 22 '18 at 21:35
  • @AhmedAboumalek It is. But why should its elements be treated as constants? – Swordfish Oct 22 '18 at 21:37
  • @Swordfish C++ only grants read access to those elements that is why they cannot be modified. check this out : https://stackoverflow.com/questions/44958450/c-cant-change-element-in-char-array – Ahmed Aboumalek Oct 22 '18 at 21:52
  • @AhmedAboumalek The problem in your link has nothing to do with arrays of `char` (`char foo[42];` or `char bar[37][42];`). – Swordfish Oct 22 '18 at 21:57
  • The problem still persists. However I finally figured out why it was not working, the problem is with the x, he's trying to store an `int` in a `char`. – Ahmed Aboumalek Oct 22 '18 at 22:08
  • @AhmedAboumalek No, thats not a problem either as all values are in the range of `char`. – Swordfish Oct 22 '18 at 22:09
  • @Swordfish I did compile and run the code and it was an issue, as the value of `x`after the `if statements` becomes an empty constant or otherwise null, that means using it as a column index will not work. You can run it yourself. Anyhow, if you know how to fix it, please do tell. – Ahmed Aboumalek Oct 22 '18 at 22:15
  • @AhmedAboumalek "and it was an issue, as the value of x after the if statements becomes an empty constant or otherwise null" Wt...heck is a "empty constant"? After the ifs `x` is `0`, `1`, `2`, `3`, `4`, or `5`, or it is left unchanged if it was not `'A'`, `'a'`, `'B'`, `'b'`, `'C'`, `'c'`, `'D'`, `'d'`. `'E'`, `'e'`, `'F'` or `'f'` before. The problem is the value of `y` as I said before. – Swordfish Oct 22 '18 at 22:35
  • by empty constant I mean an empty string. Well, my compiler disagrees with you in that case. – Ahmed Aboumalek Oct 22 '18 at 23:33
  • @AhmedAboumalek "Well, my compiler disagrees with you in that case." With what? – Swordfish Oct 22 '18 at 23:47

1 Answers1

0

Instead of handing over code, I think it would be more beneficial to cover some basics on how to debug a case like this. Instead of looking at the whole program all at once, try breaking it down into pieces until you find out what section of your code is having the issue. For example, let's take a look at this section (hint, hint, hint):

while(toupper(choice) == 'C')
{
    cout << "Enter a row. Ex: 1,2,3... ";cin >> y;
    cout << "Enter a Letter for a seat: "; cin >> x;

    if(toupper(x) == 'A')
        x = 0;
    if(toupper(x) == 'B')
        x = 1;
    if(toupper(x) == 'C')
        x = 2;
    if(toupper(x) == 'D')
        x = 3;
    if(toupper(x) == 'E')
        x = 4;
    if(toupper(x) == 'F')
        x = 5;

    seating[y][x] = taken;
    for(int row = 1; row <= rowsz; row++)
    {
        for(int col = 0; col < colsz; col++)
        {
            cout << seating[row][col];
        }
        cout << endl;
    }
    cout << "Seat again? Press C to continue to seat and press Q to quit. "; cin >> choice;

Try inserting some couts in there and see what you're passing into each variable. If you take the time to debug your code this way, you'll be much better off than if we were to hand out the answer. This stuff always takes time at first, but if you have a more specific question, feel free to come back and we'd be happy to help!

bgregs
  • 166
  • 8
  • I didn't downvote (yet ;) but ... what would it help to print `y` when printing it would give the "desired" output? – Swordfish Oct 22 '18 at 20:35
  • hahaha well thank for refraining (for now ;) ). I think my point was probably missed, there's a lot of issues with the OPs code. I thought it would be more valuable for him to break it down small and see what each step of his code is doing. I didn't mean to imply just 'y', but more to dump everything to the console to see what's actually happening in that mess. – bgregs Oct 22 '18 at 20:43
  • What you wrote should be going in the comment section in my opinion. It's not a solution but rather a tip. – Ahmed Aboumalek Oct 22 '18 at 20:47
  • Lesson learned. In this case seems very subjective in my opinion, but ultimately it's not my call. Just trying to help the OP, didn't mean to offend. – bgregs Oct 22 '18 at 20:49