I'm trying to display a two-dimensional string array and only receive a weird string of letters and numbers?

So this is the table I am making

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
	    string tables[5][14] = 
	    { { "0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "2 to 1" },
	    { "2", "5", "8", "11", "14", "17", "20", "23", "26", "29", "32", "35", "2 to 1" },
	    { "1", "4", "7", "10", "13", "16", "19", "22", "25", "28", "31", "34", "2 to 1" },
	    { "1st 12", "2nd 12", "3rd 12" },
	    { "1-10", "Even", "Red", "Black", "Odd", "19-36" } };

	     cout << tables << endl;

        system("pause");
        return 0;    
    }


So I run this code and my compiler does not show any errors but all that is returned is a strange string of letters and numbers,
!http://i.imgur.com/ebhgyrQ.png

I have never encountered this before and could really use some advice, thanks for your time!
When you create an array, what's happening behind the scenes is that the variable "tables" is a pointer to your array, and it points to the first address of your array.

When you try to print this pointer, it spits out its value, which is a memory address.

You'll have to make your own code using a loop to print out each value in your array.
1
2
3
for (int i = 0; i < 5; i++)
    for (int j = 0; j < 14; j++)
        cout << tables[i][j] << " ";


Side note: Be careful when working with basic types that aren't strings, memory will be stored for them, but they aren't initialized to a default value when you iterate through the list (not all of your inner arrays have 14 strings in them).
Last edited on
The problem you're having is that when you output an array without using the [] index operator, the program prints out the memory location where the array begins (it has to do with pointers).

If you actually want to output the contents of the array rather than its location use:
1
2
3
for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 14; ++j)
        cout << tables[i][j] << endl;
Hi steamengines,

At line 14 you have cout<< tables << end1;
That is telling the computer to give you the memory address of the array. In order for you to see one of your values you need to use something like

cout << tables[0][0] << end1; <----this will give you 0 because its the first number in the array.

cout << tables[0][1] << end1; <---this will give you 3 and so on.

Hope this helps

-MRangel
Ahhhh!!!!!!! thank you everyone I understand what I did wrong and can now thanks to your help! Now I may continue with my program!

Topic archived. No new replies allowed.