Problem with Array

Hi

Basically i am trying to get the array working, but i keep getting an error message saying that i is an undeclared identifier, Can any one help?

Thanks


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
#include <iostream>
using namespace std;
int main(){
	char ans;
	char grid[12][11] = {
        {' ','A','B','C','D','E','F'},
        {'1','O','O','O','O','O','O'},
        {'2','O','O','O','O','O','O'},
        {'3','O','O','O','O','O','O'},
        {'4','O','O','O','O','O','O'},
        {'5','O','O','O','X','O','O'},
		{'6','O','O','O','O','X','O'},
		{'7','O','O','O','O','O','O'},
		{'8','O','O','O','O','O','O'},
		{'9','O','O','O','O','O','O'},
		{'1','O','O','O','O','O','O'}};
	char character = 'X';
    
	int position[10] = {11,11};
	int row;
	char col;

    
	// Draw the grid once
	for (int i =0; i < 11; i ++){
		for (int j =0; j < 11; j++){
			cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
	
	}

		
	cout<< " enter Row = ";
	cin >> row;

	cout<< "\n enter column = ";
	cin >> col;

	//Draw grid based on user choicea
	grid[row][col]
		= 'X';
	system ("cls");
	for ( i =0; i < 10; i ++){
		for (int j =0; j < 10; j++){
			
				cout << grid[i][j];
			cout << " ";
		}
		cout << endl;

		grid [row][col - 'A' + 1] = character; //








	}
	return 0;
} 


It seems as though you're not using the ans variable. Maybe if you get rid of it your code would work. Line 4.
Hi thanks, i deleted it and made no different, i think there is something wrong with the array itself rather than the variables
Oh.

Look at line 45. Within the for loop you have an i that does not have int in front of it.
Ok so its defiantly the array, when i change the array on line 5 from 12,11 to 7,11 i get an error message stating that i have too many initialisers
The array itself is fine. The error message you're getting is saying that the variable i has no type definition attached to it.

What you did with your array was simply tell the processor that you've shrunk the size without removing any of the elements. The computer doesn't want the extra information, so it's telling you to remove some.

You should look at line 45 in your code. In your for loop the variable i is missing the int identifier. All you need to do is add the int in front of i so that it has a type.
o yeah lol, how did i miss that, can i ask that when i correct this issue, i get a warning message about truncation from int to char. This is because on line 16 i try putting the value 10 in the array, is there any way i can get it to print properly
nope, not with char
could i change it from char to int, would the grid still work?
As far as I know, you can't get the value 10 in as a character, since characters are meant to be one character, and 10 is 2.

There is a tricky way to do it, but what you would be doing is instead of entering the value 10, you enter '\n' which has an ascii value of 10. Then within every for loop you add an if statement that checks to see if you're in the first column. If you are in the first column, then add an extra space unless you're in the last row, in which case you'd print 10 (as simple as cout<<10; or cout<<(int)'\n';.
ok that is really tricky how would i do that, basically this is what i am trying to achieve:

I am trying to write a program that gives me a seating plan from rows A-f
and columns 1-10. The user will be prompted where they want to sit and replace the value O in the array with an X. I will also need to search this seating plan, so if someone is sitting in C4, it will already have an X and the user will be unable to book the seat.

Is there any easy way to do this, maybe using a loop or something?
Okay.

So you can do this in a loop, although it would be a do-while loop instead of a for loop.

1
2
3
4
5
6
7
8
9
10
	
	cout<< " enter Row = ";
	cin >> row;

	cout<< "\n enter column = ";
	cin >> col;

	//Draw grid based on user choicea
	grid[row][col]
		= 'X';


This area of your code would go in the do-while, and you'd make a new bool variable outside the loop to set as a flag.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	
bool seatBooked=false;
do
{
	cout<< " enter Row = ";
	cin >> row;

	cout<< "\n enter column = ";
	cin >> col;

	//Draw grid based on user choice
        //Run an if statement to see if the grid[row][col] already =='X'
        //If it does, let them know.
        //Else the statement below runs and seatBooked is set to true
	grid[row][col]
		= 'X';
}while(!seatBooked);
OMG how did i forget to use a boolean expression. Thank you. Just one other question, could you tell me what code i need to fix that 10 in the seat plan.

Thanks again, you have been a huge help
i treid doing what you said, and its succesfful in some parts but, for some reason the X no longer gets placed when i choose the row and coloumn. Here is my new source code:

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
#include <iostream>
#include <cctype>
using namespace std;
int main(){
	char grid[12][11] = {
        {' ','A','B','C','D','E','F'},
        {'1','O','O','O','O','O','O'},
        {'2','O','O','O','O','O','O'},
        {'3','O','O','O','O','O','O'},
        {'4','O','O','O','O','O','O'},
        {'5','O','O','O','X','O','O'},
		{'6','O','O','O','O','X','O'},
		{'7','O','O','O','O','O','O'},
		{'8','O','O','O','O','O','O'},
		{'9','O','O','O','O','O','O'},
		{'1','O','O','O','O','O','O'}};
	char character = 'X';
    
	int position[10] = {11,11};
	int row;
	char col;

    
	// Draw the grid once
	for (int i =0; i < 11; i ++){
		for (int j =0; j < 11; j++){
			cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
	
	}

	bool seatBooked=false;
	do
	{
		
		cout<< "\nEnter Column Letter = ";
		cin >> col;

		cout<< "Enter Row Number = ";
		cin >> row;

	//Draw grid based on user choice
		if ((row== 5 && col== 'D') || (row=6 && col=='E'))
		cout<<"This seat has been taken, Please enter a new seat number:\n";
		
		else
	grid[row][col]
		= 'X';
	}while(!seatBooked);

	system ("cls");
	for ( i =0; i < 10; i ++){
		for (int j =0; j < 10; j++)
		{
			cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
		grid [row][col - 'A' + 1] = character;		
	}

	return 0;
} 



Thanks Again
bump
Topic archived. No new replies allowed.