Need help

Hello, I cant figure out why my code will only display up to number 9 and not 13

#include <iostream>
#include <string>
using namespace std;

int main()
{
char arr[13][5];
for (int i = 0; i < 13; i++)
{

arr[i][0] = i + 1 + '0';
for (int j = 1; j < 5; j++) {

arr[i][j] = 'A' + j - 1;
}
}

cout << "initial seat arrangements........\n";
display(arr);

airline(arr);

return 0;
}
int seatsFull(char arr[13][5])
{
int count = 0;
for (int i = 0; i < 13; i++)
{
for (int j = 1; j < 5; j++)
if (arr[i][j] == 'X')
count++;
}

if (count == 28)
{
return 1;
}
return 0;
}

void display(char arr[13][5])
{
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

return;
}


string getData()
{
string p;
cout << "Where would you like to sit? ";
cin >> p;
return p;
}


void update(char arr[13][5], int row, int col)
{
cout << "congrats, your seat is valid. Reserved for you\n";
cout << "updated seat status..........\n";
arr[row][col] = 'X';
}
int check(char arr[13][5], string s)
{

if (s[0] > '13' || s[0] < '1' || s[1]>'D' || s[1] < 'A')
{
cout << "invalid seat no\n";
return 0;
}
int row = -1, col = -1;
for (int i = 0; i < 13; i++) {
if (arr[i][0] == s[0])
row = i;
}
for (int j = 0; j < 5; j++)
{
if (arr[row][j] == s[1])
col = j;
}

if (col == -1)
{
cout << "Seat is already occupied\n";
return 0;
}
else
{
update(arr, row, col);
}
return 1;
}


void airline(char arr[13][5]) {

cout << "enter Q if you are done!\n";
string s;

while (true) {
s = getData();

if (s[0] == 'Q')
break;

if (check(arr, s))
display(arr);

if (seatsFull(arr)) {
cout << "Sorry plane is full...please try again later" << endl;
break;
}
}
cout << "Goodbye.." << endl;
}
Last edited on
Use code tags, so your program is presentable - https://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
using namespace std;

int main()
{
  char arr[13][5];
  for (int i = 0; i < 13; i++) {

    arr[i][0] = i + 1 + '0';
    for (int j = 1; j < 5; j++) {

      arr[i][j] = 'A' + j - 1;
    }
  }

  cout << "initial seat arrangements........\n";
  display(arr);

  airline(arr);

  return 0;
}

int seatsFull(char arr[13][5])
{
  int count = 0;
  for (int i = 0; i < 13; i++) {
    for (int j = 1; j < 5; j++)
      if (arr[i][j] == 'X')
        count++;
  }

  if (count == 28) {
    return 1;
  }
  return 0;
}

void display(char arr[13][5])
{
  for (int i = 0; i < 13; i++) {
    for (int j = 0; j < 5; j++) {
      cout << arr[i][j] << " ";
    }
    cout << endl;
  }

  return;
}


string getData()
{
  string p;
  cout << "Where would you like to sit? ";
  cin >> p;
  return p;
}


void update(char arr[13][5], int row, int col)
{
  cout << "congrats, your seat is valid. Reserved for you\n";
  cout << "updated seat status..........\n";
  arr[row][col] = 'X';
}

int check(char arr[13][5], string s)
{

  if (s[0] > '13' || s[0] < '1' || s[1] > 'D' || s[1] < 'A') {
    cout << "invalid seat no\n";
    return 0;
  }
  int row = -1, col = -1;
  for (int i = 0; i < 13; i++) {
    if (arr[i][0] == s[0])
      row = i;
  }
  for (int j = 0; j < 5; j++) {
    if (arr[row][j] == s[1])
      col = j;
  }

  if (col == -1) {
    cout << "Seat is already occupied\n";
    return 0;
  } else {
    update(arr, row, col);
  }
  return 1;
}


void airline(char arr[13][5])
{

  cout << "enter Q if you are done!\n";
  string s;

  while (true) {
    s = getData();

    if (s[0] == 'Q')
      break;

    if (check(arr, s))
      display(arr);

    if (seatsFull(arr)) {
      cout << "Sorry plane is full...please try again later" << endl;
      break;
    }
  }
  cout << "Goodbye.." << endl;
}


Compile with lots of warnings enabled.

$ g++ -Wall foo.cpp
foo.cpp:72:14: warning: multi-character character constant [-Wmultichar]
   if (s[0] > '13' || s[0] < '1' || s[1] > 'D' || s[1] < 'A') {
              ^

You need to have a different approach if the first TWO characters are "13".


This can be greatly simplified by just having the arr array to record whether occupied or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
#include <limits>
#include <cctype>
using namespace std;

constexpr int norows = 13;
constexpr int nocols = 4;

void airline(bool arr[norows][nocols]);
void display(bool arr[norows][nocols]);

int main()
{
	bool arr[norows][nocols] {};

	cout << "initial seat arrangements........\n";
	display(arr);
	airline(arr);
}

int seatsFull(bool arr[norows][nocols])
{
	for (int i = 0; i < norows; i++)
		for (int j = 0; j < nocols; j++)
			if (arr[i][j] == false)
				return 0;

	return 1;
}

void display(bool arr[norows][nocols])
{
	for (int i = 0; i < norows; i++) {
		std::cout << i + 1 << " ";

		for (int j = 0; j < nocols; j++) {
			if (arr[i][j] == true)
				cout << 'X';
			else
				cout << (char)('A' + j);

			cout << ' ';
		}
		cout << endl;
	}

	return;
}

string getData()
{
	string p;

	cout << "Where would you like to sit? ";
	cin >> p;
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	return p;
}

void update(bool arr[norows][nocols], int row, int col)
{
	cout << "congrats, your seat is valid. Reserved for you\n";
	cout << "updated seat status..........\n";

	arr[row][col] = true;
}

int check(bool arr[norows][nocols], const string& s)
{
	char* c {};
	const int row {strtol(s.data(), &c, 10)};
	const char col = toupper(*c);

	if (row < 1 || row > norows || col < 'A' || col >= 'A' + nocols) {
		std::cout << "Invalid seat\n";
		return 0;
	}

	if (arr[row - 1][col - 'A'] == true) {
		cout << "Seat is already occupied\n";
		return 0;
	}

	update(arr, row - 1, col - 'A');
	return 1;
}


void airline(bool arr[norows][nocols])
{
	cout << "enter Q if you are done!\n";

	while (true) {
		string s {getData()};

		if (s[0] == 'Q' || s[0] == 'q')
			break;

		if (check(arr, s))
			display(arr);

		if (seatsFull(arr)) {
			cout << "Sorry plane is full...please try again later" << endl;
			break;
		}
	}

	cout << "Goodbye.." << endl;
}


Note no spaces allowed in input of seat position.
Hello maulk,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



You have an array of "char"s that can store a single byte.

The line of code: arr[i][0] = i + 1 + '0'; works fine for the numbers 0 - 9, or ASCII decimal characters 48 - 57. The problem is when "i" == 9 you have 9 + 1 + 48 = 58 or the ASCII character ":" because you can not store 10 in a single byte. It is possible to store the characters (:, ; and <) and later change these characters into the numbers that you need when you output to the screen.

With some work this could be made to work, but I agree with seeplus that all you really need is an array, such as an array of bools, to keep track of if the seat is empty, (false), or used, (true).

The information that you are trying to store "1ABCD" can be generated when you display the array based on if the seat is false or true.

Another possibility is skip the numbers in the array and just store (A, B, C, D) and remove the letters or change to "X" when a seat is taken.

Your program would work better as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;
constexpr int MAXROWS{ 13 }, MAXCOLS{ 5 };

// <--- If your functions follow "main" you need prototypes here.

int seatsFull(char arr[MAXROWS][MAXCOLS]);  // <--- The MAXROWS here is not needed, but OK if you leave.
void display(char arr[MAXROWS][MAXCOLS]);
string getData();
void update(char arr[MAXROWS][MAXCOLS], int row, int col);
int check(char arr[MAXROWS][MAXCOLS], string s);
void airline(char arr[MAXROWS][MAXCOLS]);

int main()
{
    char arr[MAXROWS][MAXCOLS]{};  // <--- Should initialize all elements to "\0".

    for (int i = 0; i < MAXROWS; i++)
    {
        //arr[i][0] = i + 1 + '0';

        for (int j = 0; j < MAXCOLS - 1; j++)  // <--- Changed. Gives elements 0 - 3 (A B C D) and leaves the last element with "\0".
        {
            arr[i][j] = 'A' + j /*- 1*/;
        }
    }

I have not looked at the "display" function yet, but it will need adjusted.

Andy
airline sit reservation with C++ implantation

Airline seats need to be assigned in a way that respects the center of gravity (CG).
Since the OP seems to have a habit of deleting their posts after getting answers:

maulk wrote:
Hello, I cant figure out why my code will only display up to number 9 and not 13

#include <iostream>
#include <string>
using namespace std;

int main()
{
char arr[13][5];
for (int i = 0; i < 13; i++)
{

arr[i][0] = i + 1 + '0';
for (int j = 1; j < 5; j++) {

arr[i][j] = 'A' + j - 1;
}
}

cout << "initial seat arrangements........\n";
display(arr);

airline(arr);

return 0;
}
int seatsFull(char arr[13][5])
{
int count = 0;
for (int i = 0; i < 13; i++)
{
for (int j = 1; j < 5; j++)
if (arr[i][j] == 'X')
count++;
}

if (count == 28)
{
return 1;
}
return 0;
}

void display(char arr[13][5])
{
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

return;
}


string getData()
{
string p;
cout << "Where would you like to sit? ";
cin >> p;
return p;
}


void update(char arr[13][5], int row, int col)
{
cout << "congrats, your seat is valid. Reserved for you\n";
cout << "updated seat status..........\n";
arr[row][col] = 'X';
}
int check(char arr[13][5], string s)
{

if (s[0] > '13' || s[0] < '1' || s[1]>'D' || s[1] < 'A')
{
cout << "invalid seat no\n";
return 0;
}
int row = -1, col = -1;
for (int i = 0; i < 13; i++) {
if (arr[i][0] == s[0])
row = i;
}
for (int j = 0; j < 5; j++)
{
if (arr[row][j] == s[1])
col = j;
}

if (col == -1)
{
cout << "Seat is already occupied\n";
return 0;
}
else
{
update(arr, row, col);
}
return 1;
}


void airline(char arr[13][5]) {

cout << "enter Q if you are done!\n";
string s;

while (true) {
s = getData();

if (s[0] == 'Q')
break;

if (check(arr, s))
display(arr);

if (seatsFull(arr)) {
cout << "Sorry plane is full...please try again later" << endl;
break;
}
}
cout << "Goodbye.." << endl;
}
Registered users can post here. Sign in or register to post.