0

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!

1
  • 2
    That's not a weird string of letters/numbers, that's hexadecimal. Commented Dec 2, 2014 at 4:51

5 Answers 5

2

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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.