6

I am trying to initialize a tic-tac-toe board in c++ but the output always gives me hexa values. Is there a way to transform them into actual string values ?

#include <iostream>
#include<string>

using namespace std;

int main()
{
    string tab[5][5] = { "1","|","2","|","3",
                         "-","+","-","+","-",
                         "4","|","5","|","6",
                         "-","+","-","+","-",
                         "7","|","8","|","9" };

    for(int i = 0; i <= 24; i++)
    {
        cout << tab[i] << endl;
    }
}
2
  • 5
    Ask yourself what is the type of tab[i]. Now ask yourself how do I print that type. Commented Dec 12, 2016 at 18:57
  • 2
    Change string tab[5][5] to string tab[25] and print the std::endl after each 5th value. Commented Dec 12, 2016 at 19:00

4 Answers 4

4

You're sending the value of tab[i] to cout, so you're getting the memory address.

You probably want to get the items nested deeper, like tab[i][j].

Sign up to request clarification or add additional context in comments.

Comments

4

tab[i] is a std::string[] - that is, an array of std::string rather than a std::string itself.

Use ranged-for instead for your output. As well as the Standard Library containers, it works with built-in arrays:

for (const auto &row : tab) {
  for (const auto &c : row) {
    cout << c;
  }
  cout << endl;
}

1 Comment

the range for works with built in arrays because arrays are not pointers. The array name holds on to its size information and std::begin and std::end are overloaded to work with arrays and get the right values.
2

Of course the answers, that propose a loop at the place of output are correct. If you happen to have to output your tic tac toe field at many different places in your application, you might prefer to encapsulate that. One possible solution is to have a tic tac toe class:

struct TicTacToe : public std::array<std::array<int, 3>, 3> {
    TicTacToe() :
        // not sure, if the initialization will work like that
        std::array<std::array<int, 3>, 3>{{0,0,0},{0,0,0},{0,0,0}}
    {};
};

and then define an output operator for it:

auto operator << (std::ostream& out, const TicTacToe& field)
    -> std::ostream& {
  return
      out << std::accumulate(std::begin(field),
                             std::end(field),
                             std::string{},
                             [](const std::string& a,
                                const std::array<int, 3> b)
                                 -> std::string {
                               return 
                                   std::accumulate(std::begin(b),
                                                   std::end(b),
                                                   std::string{},
                                                   [](const std::string& a, int b)
                                                       -> std::string {
                                                     return std::string{b < 0 ?
                                                                          "O" :
                                                                          (b > 0 ? "X" : " ")} +
                                                            (a.empty() ? "" : "|")
                                                   }) +
                                   (a.empty() ? "" : "\n-+-+-\n");
                             });

}

Please note, that I have not tested this code. It is meant to give you an idea, not a source for copy-paste.

Comments

0

That's more like it thanks a lot !

#include <iostream>
#include<string>
using namespace std;

int main()
{
    string tab[5][5] = { "1","|","2","|","3",
                         "-","+","-","+","-",
                         "4","|","5","|","6",
                         "-","+","-","+","-",
                         "7","|","8","|","9" };

    for(int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            cout << tab[i][j];

            if (j == 4)
            cout << endl;
        }   
    }
}

2 Comments

Save yourself an if. Remove the if and move the cout << endl; out of the inner for loop. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << tab[i][j]; } cout << endl; }The inner for will finish printing the line and exit, the outer loop will print the end of line and loop back to the next line. Also consider replacing the endl with '\n' because endl does more than just end the line and that more is a very expensive operation.
It's better to use range-based for loop instead of for(int i = 0;.... You don't need to replace the 5s if the size changes; you can get rid of the [i]...

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.