Drew
Drew

Reputation: 29

How to display a two-dimensional string array properly?

So this is the table I am making

#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;
    return 0;    
}

So I run this code and my compiler does not show any errors but all that is printed is a strange string of letters and numbers,

007EF610

I have never encountered this before and could really use some advice, thanks for your time!

Upvotes: 0

Views: 2530

Answers (5)

user966379
user966379

Reputation: 3003

First understand what does it mean this declaration

string tables[5][14]

It means table is an 2d array(which has 5 rows and 14 columns) of string object

It would help you to understand the mistake you have done

Upvotes: 0

zhirzh
zhirzh

Reputation: 3459

You know, in C and C++, simply printing the name of the variable won't print it's contents, if it's anything other than the simpler data types, ie, int,float,long,string,char etc..

the variable tables is, as you said, a 2D array. To print its contents, you need to loop over each element in the array and print that:

for(int i=0; i<5; i++) {
      for(int j=0; j<14; j++){
        cout<<tables[i][j]<<'\t';
      }
      cout<<endl;
}

PS: what you're getting as the output - 007EF610 - is the base address of the 2D array, ie, the location in memory where it is stored.

Upvotes: 1

speed488
speed488

Reputation: 306

First, properly initialize the array (you need to fill each entry [5][14]:

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", "", "", "", "", "", "", "", "" }
};

Then loop through it, skip the empty values:

for(int i=0;i<5;i++){
    for(int j=0;j<14;j++){
        if (!tables[i][j].empty()) {
            cout << tables[i][j] << " ";
        }
    }
    cout << endl;
}

Output:

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 

Complete code:

#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", "", "", "", "", "", "", "", "" }
    };
    for(int i=0;i<5;i++){
        for(int j=0;j<14;j++){
            if (!tables[i][j].empty()) {
                cout << tables[i][j] << " ";
            }
        }
        cout << endl;
    }
    return 0;
}

Upvotes: 1

Wagner Patriota
Wagner Patriota

Reputation: 5684

You are trying to do something like this:

#include <iostream>
#include <string>

#define ROWS 5
#define COLS 14

using namespace std;

int main()
{
    string tables[ ROWS ][ COLS ] =
    {
        { "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", "", "", "", "", "", "", "", "" }
    };

    for ( int i = 0; i < ROWS; i++ )
    {
        for ( int j = 0; j < COLS; j++ )
        {
            cout << tables[ i ][ j ] << " ";
        }

        cout << endl;
    }

    system( "pause" );

    return 0;    
}

Output:

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

What is happening on you code is: You assumed that cout << tables << endl is able to print the whole table. But this is not true. cout can not automatically have any information about your table. So you must loop over it correctly.

The number you were getting (like 007EF610) is in fact the address of the tables array in memory.

Upvotes: 1

Boris Dalstein
Boris Dalstein

Reputation: 7788

Your code prints tables, which is the address of the first string. If you want to print all strings contained in your array of array of strings, you have to write a loop yourself that goes through all tables[i][j], for instance:

for(int i=0; i<5; ++i)
{
  for(int j=0; j<14; ++j)
  {
    cout << tables[i][j] << endl;
  }
}

Upvotes: 2

Related Questions