Problem with 2d arrays.

I'm trying to write a slot machine program.

Here's what i have done so far:
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
	const int row=3;
	const int column=3;
	char table[row][column];
	int correctRow=0;
	
	srand((unsigned int) time(NULL));

	for(int r=0; r<row; r++)
	{
		for(int c=0; c<column; c++)
			table [r][c] = (rand() %3) + 'a';
	}
	
	for(int r=0; r<row; r++)
	{
		for(int c=0; c<column; c++)
		{
			cout << table[r][c] << ' ';
		}
		cout << endl;

		if(table[0][r] == table[1][r] && table[1][r] == table[2][r])
		{
			correctRow++;
		}
	}
	
	cout << "You got: " << correctRow << "correct row" << endl;

	return 0;
}


My problem is that the program will only add to correctRow if i get 2 correct row's it seems.. Like one vertical and one horizontal (don't know if that matters)

I'm trying to understand how to work with the fields/arrays correctly.

Thanks in advance.
Last edited on
Don't know if this is your problem, but you seem to be doing table[r][c] everywhere except for line 31, where you're doing table[c][r]
I don't see any [c][r] at line 31. And I really don't know what my problem is, I'm still tinkering with it trying to understand how it works 100% for now it's alot of trail and error.

I wish somone could point me into the right direction. I got code from a simpler game i made, and i will reuse parts of that once i understand how to work with the 2d array.
On line 31: table[0][r]

Since r is the row, you're using '0' as the column. So in effect you're doing [0][r].

Shouldn't this be [r][0] instead of [0][r]?

Note that the rest of line 31 has the same problem

That is correct. Thanks!

I will keep the thread unsolved until I feel that I got the rest under control.
Topic archived. No new replies allowed.