HELP 2D ARRAYS ...!

hello everybody ...
i am trying to do a cinema reservation .. but i have problem with the seats ...
this is my code for now
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;
int main()
{
    string seatPlan [5][5];

    string cols ;
    string rows;
       cout<<"A "<<"B "<<"C "<<"D "<<"E "<<endl;
    for (int i=0;i<5;i++){

    for (int j=0;j<5;j++){
    cout<<seatPlan[i][j];
    cout<<' * ';
    }
    cout<<endl;
    }
    cout<<"Please enter seat row and column you wish to reserve: \n";
    cout<<endl;
    for (int i=0;i<5;i++){

    cin>>rows;
    cin>>cols;
    seatPlan[rows][cols];//--- there is problem here ??!!
    }
    }


here i want to make the user chose the seat as A5 or A6
but i dont know how to do it?? what datatype i should use ??!!
and how i make the seat like this ((THE NUMBER OF ROWS))
- A B C D E
1 * * * * *
2 * * * * *
3 * * * * *
4 * * * * *
5 * * * * *

Last edited on
You could make it a bool ( true / false ).

Then;
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
int main()
{
	int rows = 0;
	int cols = 0;

	//init all to false - NOT taken
	bool seatplan[ 5 ][ 5 ] = { false };

	cin >> rows;
	cin >> cols;

	//take one away, because if a user enters
	//5, you will get an access violation.
	// seatplan[ 5 ][ 5 ] - elements are 0 - 4!
	seatplan[ --rows ][ --cols ] = true;

	cout << "Seatplan:\n\n";

	//loop rows
	for( int i = 0; i < 5; ++i )
	{
		//loop columns
		for( int j = 0; j < 5; ++j )
			cout << seatplan[ i ][ j ] << ' ';

		cout << endl;
	}
   
	return 0;
}
Last edited on
i want to change the zero(s) in the arrays to (*) how???
and how can i make the user input A3 which A is the coulmn and 3 is row ??!! what data type i should use??
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
int main()
{
	char rows = '*';
	int cols = 0;

	char seatplan[ 5 ][ 5 ] = { '*' };

	//init each element to *'s
	//loop rows
	for( int i = 0; i < 5; ++i )
	{
		//loop columns
		for( int j = 0; j < 5; ++j )
			seatplan[ i ][ j ] = '*';
	}


	cout << "Seatplan:\n\n";

	//loop rows
	for( int i = 0; i < 5; ++i )
	{
		//loop columns
		for( int j = 0; j < 5; ++j )
			cout << seatplan[ i ][ j ] << ' ';

		cout << endl;
	}

	cout << endl << endl;

	//get input
	cin >> rows;
	cin >> cols;

	//take one away, because it a use enters
	//5, you will get an access violation.
	// seatplan[ 5 ][ 5 ] - elements are 0 - 4!

	//typecast to an int( rows )
	seatplan[ ( (int)--rows - 96 ) ][ --cols ] = 'O';

	cout << "Seatplan:\n\n";

	//loop rows
	for( int i = 0; i < 5; ++i )
	{
		//loop columns
		for( int j = 0; j < 5; ++j )
			cout << seatplan[ i ][ j ] << ' ';

		cout << endl;
	}
   
	return 0;
}


When typecasting, it will give you the decimal value of the letter.
If you look at the ascii table, the lower case letter 'a' has a decimal value of 97. That's why, on line 41, I take away 96.

Ascii table: http://www.asciitable.com/

If you try this code, entering C 3 will add a O in the center of the seatplan.
Topic archived. No new replies allowed.