1

I am trying to make a program to print the names of an array, the array represents the first row of the chess board. My attempt is with the following program but the only thing that I can achieve is printing numbers :( .

    #include <iostream>

     using namespace std;

      void  main()

       enum pions // giving the names in pions 
       { 
       Tower=1 ,
       Horse ,
       Officer ,
       Princes ,
       King };

     int Chess [0][8]={Tower , Horse , officer , Princes , King , Officer , Horse , Tower };    

     // putting the names of each coordinate in the array (above ) .

    cout << " in this place the pion that is placed is the  " << chess [0][1] << endl;

I know that the program will print the number 2 , but how can I make it print the word "Horse" instead of 2 ???

The cout command is written by the creator of the program (me :P) how i can give the option to the user to choose the part of the array that is going to be printed ?

Thanks in advance for your time and effort, a big sorry in case I am not clear, its my first post :D .

Any recommendations are welcome.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 1
    Your first problem is that you have an array of ints, not Strings or pions as you call them. – HopAlongPolly Nov 11 '13 at 16:21
  • 1
    The declaration `int Chess [0][8]` makes a zero-sized array of arrays, which is not quite what you want I'm guessing. – Some programmer dude Nov 11 '13 at 16:23
  • 1
    possible duplicate of [Is there a simple script to convert C++ enum to string?](http://stackoverflow.com/questions/201593/is-there-a-simple-script-to-convert-c-enum-to-string) – Barmar Nov 11 '13 at 16:24

4 Answers4

2

You need to write a function that takes a parameter of type Chess and returns a string. Here's one that would work:

const char *chessPieceName(Chess piece)
{
    switch(piece) {
        case Tower:
            return "Tower";
            break;
        case Horse:
            return "Horse";
            break;
        // etc.
        default:
            return "Not a valid piece";
            break;
    }
}

You can call then call this function from main:

cout << " in this place the pion that is placed is the  " << chessPieceName(chess[0][1]) << endl;

Having said that, you have numerous other issues in your code that should prevent it from compiling. I'm not going to go through them, since you seem to just be interested in the question you asked.

godel9
  • 7,340
  • 1
  • 33
  • 53
  • Please note that the compiler will not even issue a warning for incorrectly calling a knight a horse. ;-) – godel9 Nov 11 '13 at 16:25
  • 2
    It is not a good idea to use the switch statement instead of an array of names. – Vlad from Moscow Nov 11 '13 at 16:34
  • 1
    Thank you, This was really helpful!! Will try to combine it with Daniel's answer! – user2979553 Nov 11 '13 at 16:35
  • @VladfromMoscow Using a switch statement has the advantage that it works even if the values of the `enum` are defined to be something other than the default. – godel9 Nov 11 '13 at 16:37
  • godel9 It is a very bad style of programming. For example the enum can be changed but you forgot to change the function. Or you will change the function but because it contains so much code you can omit the break statement and so on. – Vlad from Moscow Nov 11 '13 at 16:49
1

You could have a translator function that takes the int value and transforms it into a printable string value:

string translate(int piecenum){
    string [5] ref = { "Tower", "Horse", "Officer", "Princes" "King"}
    return ref[piecenum];
}

You can print them out using cout << translate(chess[0][1]) << endl

yizzlez
  • 8,757
  • 4
  • 29
  • 44
0

You cannot print out identifiers of the enum.

You need to create a different function which takes your enum as an input parameter and returns an std::string with the name you want. For example, if you pass it pions::Tower, it will return "Tower". If you pass it pions::Horse, it will return "Horse". Etc.

Hint: Use a switch statement in the body of that function, as godel9's answer illustrates.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • First of all there will be much code and it will be difficult to support it – Vlad from Moscow Nov 11 '13 at 16:42
  • @VladfromMoscow This doesn't make any sense. – Daniel Daranas Nov 11 '13 at 16:44
  • Maybe for you it doesn make any sense. As for me it is a bad style of programming when you hard code a set of literals in the body of some function. – Vlad from Moscow Nov 11 '13 at 16:46
  • @Vlad This has absolutely nothing to do with the switch statement. You could define the string constants in a separate place and then use each of them in each section of the switch statement. In production code, I use string constants isolated in the appropriate text modules, to make internationalization easy, but in a SO answer I can just suggest a correct answer to the OP, and he will take care of his strings in the way he likes best. My answer is correct, since it will provide a correct solution to the OP's problem. The fact that you don't like its style is not relevant to its correctness. – Daniel Daranas Nov 11 '13 at 16:50
0

Either use C# instead of C++ or define yourself an array of chess figure names. For example

   enum pions // giving the names in pions 
   { 
   Tower = 0 , // <== I prefer to use 0 instead of 1
   Horse ,
   Officer ,
   Princes ,
   King };


const char *name = { "Tower", "Horse", "Officer", "Princes", "King" };

//...

for ( int i : chess[0] ) std::cout << name[i] << std::endl; 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335