Hi
Once again a newbie posting the same homework assignment! I´ve searched alot for this problem but I´m probably not good enough in searching because I just can´t figure this out. My main problem now is to insert the airplane layout into the 2D array. I was trying to do it via double for loop but it seems to give me an "error C2440: '=' : cannot convert from 'char' to 'char [5]'"

I know that I´m just starting but plz give me some hints, you probably don´t believe how many hours I´ve been looking at this without getting anywhere.

The airplane layout should look out like this
1 A B C D
2 A B C D
3 A B C D
...
7 A B C D

btw. the cout is just for me to see how it looks like.


Thx.
Gottlieb.

#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int SEATROWS = 7;
//void insertSeats (char seats[][5], int SEATROWS);
void insertSeats (char [][5]);

int main ()
{
	char seats [7][5];
	//insertSeats (seats, SEATROWS);
	//cout << seats [0][0]<< endl;
	insertSeats (seats);
	
system("PAUSE");
return 0;

}

void insertSeats (char seats [][5])
{
	int row = 1;
	char A;
	for (int i = 0; i < SEATROWS; i++)
	{
		cout << row << " ";
		seats [i] = '1';
		row++;

		for (int j = 0; j < 4; j++)
		{
			if (j == 0)
				cout << 'A' << " ";
				seats[j] = A;
			if (j == 1)
				cout << 'B'<< "  ";
			if (j == 2)
				cout << 'C'<< " ";
			if (j == 3)
				cout << 'D'<< " ";
			//cout << seats[i][j];
		}
			cout << endl;
	}
}


/*void insertSeats (char seats [][5], int SEATROWS)
{
	int row = 1;
	char A = 'A';
	char B = 'B';
	for (int i = 0; i < SEATROWS; i++)
	{
		cout << row << " ";
		//seats [i] = '1';
		row++;

		for (int j = 0; j < 4; j++)
		{
			//seats [j] = 'A';
			if (j == 0)
				cout << 'A' << " ";
				seats[j] = A;
			if (j == 1)
				cout << 'B'<< "  ";
			if (j == 2)
				cout << 'C'<< " ";
			if (j == 3)
				cout << 'D'<<" ";
			//cout << seats[i][j];
			
		}
		cout << endl;
		
	}
}*/

Recommended Answers

All 18 Replies

Something like this?

// I edit your code on some parts, because I assumed it were mistakes
void insertSeats (char (*seats)[5])
{
	int row = 1;
	char A = 'x'; // you should initialize A before you use it (like you did in the x-line)
	for (int i = 0; i < SEATROWS; i++)
	{
		cout << row << " ";
		*seats [i] = '1';
		row++;

		for (int j = 0; j < 5; j++) //the array has a length of 5 and you initialize the array in the for loop like it has a length of 4
		{
			if (j == 0)
				cout << 'A' << " ";
			    *seats[i] = A; // x-line
			if (j == 1)
				cout << 'B'<< "  ";
			if (j == 2)
				cout << 'C'<< " ";
			if (j == 3)
				cout << 'D'<< " ";
			//cout << seats[i][j];
		}
			cout << endl;
	}
}

Just for your knowledge.

*seats[i] == seats[i][0]
**seats == seats[0][0]
void insertSeats (char seats [][5])
{
	int row = 1;
	char A;
	for (int i = 0; i < SEATROWS; i++)
	{
		cout << row << " ";
		seats [i] = '1';
		row++;

		for (int j = 0; j < 4; j++)
		{
			if (j == 0)
				cout << 'A' << " ";
				seats[j] = A;
			if (j == 1)
				cout << 'B'<< "  ";
			if (j == 2)
				cout << 'C'<< " ";
			if (j == 3)
				cout << 'D'<< " ";
			//cout << seats[i][j];
		}
			cout << endl;
	}
}

Since it's a 2D array you can't reference it in a 1D way as you do in line 8 and 15:

seats = '1'; --> and by the way...every row is gonna be labeled as "1" row?
I think here you want to say seats[0] = row, or even delete row and use "i + 1" instead: seats[0] = i + 1]

seats[j] = A; --> You're forgetting 2D array too: seats[j] = A;

Thanks, Kashaku!
This prints out fine via cout but I´m supposed to do it in a 2D array, first one with first line and ABCD next line 2 with ABCD.
1 A B C D
2 A B C D
3 A B C D
...
7 A B C D

When I print out the array I get nothing, so I´m still quite lost.

cout << seats [j]

thx.

When I print out the array I get nothing, so I´m still quite lost.

cout << seats [j]

thx.

Repaste your entire code with its corrections.

Thanks, Kashaku!
This prints out fine via cout but I´m supposed to do it in a 2D array, first one with first line and ABCD next line 2 with ABCD.
1 A B C D
2 A B C D
3 A B C D
...
7 A B C D

When I print out the array I get nothing, so I´m still quite lost.

cout << seats [j]

thx.

Because the code does barely initialize the array.

if (j == 0)
        cout << 'A' << " ";
if (j == 1)
	cout << 'B'<< "  ";
if (j == 2)
	cout << 'C'<< " ";
if (j == 3)
	cout << 'D'<< " ";

This part of the code only outputs A, B, C and D. To initialize A to the array you should do for example:

// with i == 2, j == 3 and A == 'D'
seats[i][j] = A;
// when you later output it
cout << seats[2][3];
//outputs D, since seats[2][3] == 'D'

To give you a small hint.

//this code
if (j == 0)
	cout << 'A' << " ";
//should be
if (j == 0)
{
        A = 'A';
}

Tanks alot neithan I appreciate the help.

I think I´m getting there ; )

But it seems like I´m not getting the row number into the array, at least it´s not displaying when I print out the array:
"ABCD
ABCD
ABCD
ABCD
ABCD
ABCD
ABCD"

#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int SEATROWS = 7;
//void insertSeats (char seats[][5], int SEATROWS);
void insertSeats (char [][5]);

int main ()
{
	char seats [7][5];
	//insertSeats (seats, SEATROWS);
	//cout << seats [0][3]<< endl;
	insertSeats (seats);
	
system("PAUSE");
return 0;

}


void insertSeats (char seats [][5])
{
	int row = 1;
	char A = 'A';
	char B = 'B';
	char C = 'C';
	char D = 'D';
	

	for (int i = 0; i < SEATROWS; i++)
	{
		seats[i][0] = i + 1;

		for (int j = 0; j < 4; j++)
		{
			if (j == 0)
				//cout << 'A' << " ";
				seats[i][j] = A;
			if (j == 1)
				//cout << 'B'<< "  ";
				seats[i][j] = B;
			if (j == 2)
				//cout << 'C'<< " ";
				seats[i][j] = C;
			if (j == 3)
				//cout << 'D'<< " ";
				seats[i][j] = D;
			cout << seats[i][j];
		}
		cout << endl;			
	}
}

Oh that's my bad. I misread some part. Let me correct it for you:

if(j == 0)
{
        seats[i][j] = i; // i = rownumber
}
if (j == 1) // 0 should be 1
{
	//cout << 'A' << " ";
	seats[i][j] = A;
}
if (j == 2) // 1 should be 2
{
	//cout << 'B'<< "  ";
	seats[i][j] = B;
}
// and so on

EDIT: it is pointless to initialize all these characters to variables

char A = 'A';
char B = 'B';
char C = 'C';
char D = 'D';

If you intend to do it that way, then you could better use:

seats[i][j] = 'B';
// instead of
char B = 'B';
...
seats[i][j] = B;

Because... where are you printing that row layout? You are not.

You want to print the number of the row everytime you start with a new row right? And that happens everytime the first for cicles.

for (int i = 0; i < SEATROWS; i++){
seats[i][0] = i + 1;

You're doing well here. Notice that you don't need the int variable "row" since you're wisdomly using "i + 1" instead.

Here what you are doing is counting from 1 (first cicle i = 0, so i + 1 will be the 1st row which is what you want) to 7 (the last cicle i will be SEATROWS - 1, which is 6, but i + 1 makes it 7th row). From all letters in every row string (i.e. 3ABCD) you want it to be written in the first position of that string, that's what that 0 is doing in seats[0] = i + 1;

If you want to see it you just have to add the cout:

cout << seats[0];

Hope i helped you.

If you want to see it you just have to add the cout:

cout << seats[0];

Hope i helped you.

This doesn´t work for me! not sure why.

☺ ABCD
☻ ABCD
♥ ABCD
♦ ABCD
♣ ABCD
♠ ABCD
 ABCD

This is what I get.

Might it be because it´s char not int? how can I typecast it?

This is my last post before I go.

I'll give you the final step to finish it:

for (int i = 0; i < SEATROWS; i++)
	{
		seats[i][0] = i + 1;

		for (int j = 0; j < 4; j++)
		{
			if (j == 0)
				//cout << 'A' << " ";
				seats[i][j] = A;
			if (j == 1)
				//cout << 'B'<< "  ";
				seats[i][j] = B;
			if (j == 2)
				//cout << 'C'<< " ";
				seats[i][j] = C;
			if (j == 3)
				//cout << 'D'<< " ";
				seats[i][j] = D;
			cout << seats[i][j];
		}

Should be

for (int i = 0; i < SEATROWS; i++)
	{
		for (int j = 0; j < 5; j++)
		{
                        if (j == 0)
                                seats[i][j] = i;
			if (j == 1)
				//cout << 'A' << " ";
				seats[i][j] = A;
			if (j == 2)
				//cout << 'B'<< "  ";
				seats[i][j] = B;
			if (j == 3)
				//cout << 'C'<< " ";
				seats[i][j] = C;
			if (j == 4)
				//cout << 'D'<< " ";
				seats[i][j] = D;
			cout << seats[i][j];
		}

And again, read my previous post about the A, B, C and D variable. It's much better to do

seats[i][j] = 'C';

immediately instead of

char C = 'C';
seats[i][j] = C;

for A, B, C and D.

I´ve already changed the char variable as you mentioned before, I´ve also raised the number in the for loop by one, but there is still something not right that I´m not figuring out. But thx alot for your help, I´ve learned alot about arrays tonight.

void insertSeats (char seats [][5])
{	
	for (int i = 0; i < SEATROWS; i++)
	{
		//seats[i][0] = i +1;
		//cout << seats [i][0];
		
		for (int j = 0; j < 5; j++)
		{
			if (j == 0)
				seats[i][j] = j;
			if (j == 1)
				seats[i][j] = 'A';
			if (j == 2)
				seats[i][j] = 'B';
			if (j == 3)
				seats[i][j] = 'C';
			if (j == 4)
				seats[i][j] = 'D';
			cout << seats[i][j];
		}
		cout << endl;			
	}
}

I'll look in this topic tomorrow. If your problem still isn't solved, then I'll try to help you.

If I don't see any new messages by then, then I assume that it is all solved.

No problem, and have fun with programming ;)

Hi again, still in trouble displaying the numbers in front.
1 ABCD
2 ABCD

but I think it is because it´s a char array, how can I display only the numbers as int?

Hi again, still in trouble displaying the numbers in front.
1 ABCD
2 ABCD

but I think it is because it´s a char array, how can I display only the numbers as int?

You have to be more specific when you say "still in trouble displaying the numbers", what kind of trouble? What's the output?

And if you ain't gonna treat them, it doesn't matter if it's a integer or a char, it's gonna show "1" the same way.

Post your actual code.

commented: Been really helpful! +1

Hi again, still in trouble displaying the numbers in front.
1 ABCD
2 ABCD

but I think it is because it´s a char array, how can I display only the numbers as int?

I see what you mean. The '0' is indeed not the same as char no. 0. To be more specific, the '0' is no. 48. We don't need to know the exact number, the only thing we need to know is whether after '0' comes '1', after '1' comes '2' and so on... Which is true, so you just need to add '0'.

Easy to fix, see the following:

if (j == 0)
	seats[i][j] = j + '0';

This is my entire program, after alot of learning.

Thank you guys for your help.

If you see anything that should be done diffrently, plz let me know.

#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int SEATROWS = 7;
void insertSeats (char [][5]);
void getSeats (int& row, char& column);
void printOut (char seats[][5], int row, char column);

int main ()
{
	char seats [7][5];
	char column;
	char answer;
	int row;
	cout << "These seats are available: \n";
	insertSeats (seats);
	cout << "Do you want to select your seat now? Y/N?\n";
	cin >> answer;
	answer = static_cast<char>(toupper(answer));
	while (answer == 'Y')
	{
		getSeats (row, column);
		printOut (seats, row, column);
		cout << "Do you want to select another seat? Y/N?\n";
		cin >> answer;
		answer = static_cast<char>(toupper(answer));
		if (answer == 'N')
			return 0;
	}
system("PAUSE");
return 0;
}
void insertSeats (char seats [][5])
{	
	for (int i = 0; i < SEATROWS; i++)
	{
		//seats[i][0] = i +1;
		//cout << seats [i][0];
		cout << i+1 ;
		for (int j = 0; j < 4; j++)
		{
			if (j == 0)
				seats[i][j] = 'A';
			if (j == 1)
				seats[i][j] = 'B';
			if (j == 2)
				seats[i][j] = 'C';
			if (j == 3)
				seats[i][j] = 'D';
			cout << setw(3) << seats[i][j];
		}
		cout << endl;			
	}
}
void getSeats (int& row, char& column)
{
	cout << "Please enter the row nr.(1-7) you want to sit in.\n";
	cin >> row;
	cout << "Please enter the seat nr.(A-D) you want to sit in.\n";
	cin >> column;
	column = static_cast<char>(toupper(column));
}
void printOut (char seats[][5], int row, char column)
{
	while (seats[row-1][static_cast<int>(column-65)]=='X')
	{
		cout << "This seat is already occupied, please choose\n"
			 << "another seat\n";
		cin >> column;
		column = static_cast<char>(toupper(column));
	}
	for (int i = 0; i < SEATROWS; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			seats [row-1][static_cast<int>(column-65)]='X';
			cout << setw(3) << seats[i][j];
		}
		cout << endl;			
	}
}

try this

1.	#include <iostream>
2.	#include <cctype>
3.	#include <iomanip>
4.	using namespace std;
5.	const int SEATROWS = 7;
6.	//void insertSeats (char seats[][5], int SEATROWS);
7.	void insertSeats (char [][5]);
8.	 
9.	int main ()
10.	{
11.		char seats [7][5];
12.		//insertSeats (seats, SEATROWS);
13.		//cout << seats [0][0]<< endl;
14.		insertSeats (seats);
15.	 
16.	system("PAUSE");
17.	return 0;
18.	 int = num;
19.	num = 1;
20.	}
21.	 
22.	void insertSeats (char seats [][5])
23.	{
24.		int row = 1;
25.		char A;
26.		for (int i = 0; i < SEATROWS; i++)
27.		{
28.			cout << row << " ";
29.			seats [i] = '1';
30.			row++;
31.	 
32.			for (int j = 0; j < 4; j++)
33.			{
34.				if (j == 0)
35.					cout << num<<'A' << " ";
36.					seats[j] = A;
37.				if (j == 1)
38.					cout <<num+1<< 'B'<< "  ";
39.				if (j == 2)
40.					cout << num+2<<'C'<< " ";
41.				if (j == 3)
42.					cout <<num+3<< 'D'<< " ";
43.				//cout << seats[i][j];
44.			}
45.				cout << endl;
46.		}
47.	}
48.	 
49.	 
50.	/*void insertSeats (char seats [][5], int SEATROWS)
51.	{
52.		int row = 1;
53.		char A = 'A';
54.		char B = 'B';
55.		for (int i = 0; i < SEATROWS; i++)
56.		{
57.			cout << row << " ";
58.			//seats [i] = '1';
59.			row++;
60.	 
61.			for (int j = 0; j < 4; j++)
62.			{
63.				//seats [j] = 'A';
64.				if (j == 0)
65.					cout <<num<< 'A' << " ";
66.					seats[j] = A;
67.				if (j == 1)
68.					cout <<num+1<< 'B'<< "  ";
69.				if (j == 2)
70.					cout <<num+2<< 'C'<< " ";
71.				if (j == 3)
72.					cout <<num+3<< 'D'<<" ";
73.				//cout << seats[i][j];
74.	 
75.			}
76.			cout << endl;
77.	 
78.		}
79.	}*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.