0

I need to print a 'T' at the location of the treasure for my 2D array. I am having trouble with this. I have a global constant where TREASURE = 'T'...when I print my map it prints the treasure location after the map instead of at the actual location. I have to do the same for my Start location. Code:

void PrintMap(const char Map[][COLS], const bool showTreasure)
{
    int TreasureR, TreasureC;
    int StartR, StartC;

    for (int row = 0; row < ROWS; row++)
    {
       for (int col = 0; col < COLS; col++)
       {
           if ((row == TreasureR && col == TreasureC) && showTreasure == true)
               cout << Map[row][col];
           else if ((row == StartR && col == StartC) && showTreasure == true)
               cout << START;
           else
               cout << EMPTY;

       }
    cout << endl;
    }


}
savannaalexis
  • 55
  • 1
  • 1
  • 10
  • It is unclear exactly what is wrong.. Can you provide a example that we can run to reproduce the problem? Or describe it a bit more? Your code iterates and will print `Map[I][J]` fine.. Is this you as well: http://stackoverflow.com/questions/22925146/c-printing-array ??? – Brandon Apr 07 '14 at 23:55
  • How about if you simplify your life and place the treasure character in the `Map[][]` at the correct position. – Thomas Matthews Apr 08 '14 at 00:18
  • The treasure is placed randomly so there isn't an exact position for the treasure. It prints fine, but the treasure doesn't show. – savannaalexis Apr 08 '14 at 00:26

1 Answers1

0

You can add conditional logic on how to handle the item at the map location.

Here's a simple example:

for (unsigned int col = 0; col < maximum_columns; ++col)
{
  for (unsigned int row = 0; row < maximum_rows; ++row)
  {
    char c = Map[row][col];
    // Process any special printing
    switch (c)
    {
       case 'T':
         if (!showTreasure)
         {
            c = ' ';
         }
         break;
       case 'M':
         if (!showMonster)
         {
            c = ' ';
         }
         break;
    }
    cout << c;
  }
  cout << '\n';
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154