Changing character in two dimensional arrays

I'm a student and for one of our worksheets we've had to write a program that allows you to change a character in a two dimensional array and then print back the array afterwards, I'm struggling with getting it to print back with the changes added in. Below is the bit I believe I'm struggling with, any help would be appreciated. (p.s. I know I've done it wrong.) :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		myArray[input1][input2] = i;

		cout << " " << endl;

		char myArray[8][8];
			for(int a = 0; a < 8; ++a)
			{
		
				for(int b = 0; b < 8; ++b)
				{

					for(int c = 0; c < i; ++c)
					{
					cout << i;
					}
			cout << "*";
			
				}
		cout << endl;
			}
	

What is the third loop supposed to do?

If you just want to print the array, print each element. Also, you must cause your array to exist before you can use it.

1
2
3
4
5
6
7
const int rows = 8;
const int cols = 8;
char myArray[rows][cols];

int input_row, input_col;
char c;
 
// (Get input here) 
35
36
37
38
39
40
41
42
43

for (int row = 0; row < rows; row++)
{
	for (int col = 0; col < cols; col++)
	{
		cout << myArray[row][col] << "*";
	}
	cout << endl;
}


Also, watch your indentation. You have a sudden shift to the right starting on line 6, and some confusing shifts to the left on lines 16 and 19.

Getting input (line 8) is actually not so simple if you want to make sure it handles errors properly:

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cout << "Please enter row and column to modify: ";
while (true)
{
	cin >> input_row >> input_col;

	// Make sure user entered integers (and not something else)
	if (!cin)
	{
		cout << "Hey! Two integers, please: ";
		cin.clear();
		cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
	}

	// Make sure user's coordinates are inside the 2D array
	else if (input_row < 0 || input_row >= rows ||  input_col < 0 || input_col >= cols)
	{
		cout << "In [0," << (rows-1) << "] and [0," << (cols-1) << "]: ";
	}

	// Otherwise, everything is fine, so we can leave the loop and continue with the program.
	else break;
}
31
32
33
34
cout << "Enter the new value: ";
cin >> c;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

Hope this helps.
I've got it working now, thanks a lot!
Topic archived. No new replies allowed.