0

My code prints out a wacked out version of a .txt file that displays a 20x20 table of characters and white spaces. How can i get the array to display properly as it does in the .txt. I can not use vectors or global variables. It can be done without those. The first two lines in the text are 20 and 20 to get the dimensions for the array.

ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();

char gameBoard[20][20];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        gameBoard[row][col] = inputFile.get();
    }
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {
        cout << gameBoard[row][col];
    }
    inputFile.get();
    cout << endl;
}
return 0;


20

20

WWWWWWWWWWWWWWWWWWWW
  W GO  W          W
W WW      w    S   W      
W   W   GW  w      W  
WPW  WW          G W    
 WK       W        W     
W W W  W    w   w  W  
W WK W             W    
W   SW  U    w  w  W
                   W
    w          G   W
  G         w    w W 
D   wwwww          W
             w  D  W
w w   W w   w      W
    ww  w     w w  W
  G        w       W
    ww  w S    w   W
   WWW      G      W
WWWWWWWWWWWWWWWWWWWW
Michael Ramos
  • 41
  • 1
  • 2
  • 6

3 Answers3

3

This is the answer :) Ok several edits: The input:

   3
   3
   2 2 3 
   2 2 3 
   2 2 3 

The code:

#include <fstream>
#include <iostream>

int main(){
using namespace std;
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;

char *gameBoard= new char[boardSizeRow*boardSizeCol];
for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        inputFile >> *(gameBoard + boardSizeCol * row + col);
    }   
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        cout << *(gameBoard + boardSizeCol * row + col) << " ";
    }   
    cout << endl;
}
delete []gameBoard
return 0;

}

  • No it's not, not completely anyway. C++ doesn't have variable-length arrays, so the declaration of `gameBoard` is not valid. Also, you don't get the newline properly. – Some programmer dude Apr 10 '13 at 16:55
  • char gameBoard[boardSizeRow][boardSizeRow]; im sure that you have to use constant values there, not variables – Michael Ramos Apr 10 '13 at 16:55
  • Another way is: `char *gameBoard= new char[0xfff];` and `inputFile >> *(gameBoard + boardSizeCol * row + col);` and `cout << *(gameBoard + boardSizeCol * row + col) << " ";` But what's problem with new lines? It works. – Spoonwalker Highwater Apr 10 '13 at 17:04
2

From How do I declare a 2d array in C++ using new?

Just do:

inputFile >> boardSizeRow;
inputFile >> boardSizeCol;

char **gameBoardRow = new char*[boardSizeCol];
for(int i = 0; i < sizeY; ++i) {
    ary[i] = new char[boardSizeRow];
}

for (int row = 0; row < boardSizeRow; row++)
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        inputFile >> gameBoard[row][col];
     }   
}

for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
    for (int col = 0; col < boardSizeCol; col++)
    {   
        cout << gameBoard[row][col] << " ";
    }   
    cout << endl; 
}

for(int i = 0; i < boardSizeCol; ++i) {
    delete [] gameBoard[boardSizeRow];
}
delete [] gameBoard;
Community
  • 1
  • 1
0

The file contains newlines between the rows, you don't handle that. So after you have read the first line, you read the one (or two on Windows platform) characters of the newline as the first character(s) in the second row.

I suggest you use a std::vector of std::string. One string per row.

Something like:

inputFile >> boardSizeRows;
inputFile >> boardSizeCols;

// Skip newline after the number of columns
inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// And the empty line after that
inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// Declare our game board
std::vector<std::string> gameBoard;

// Read the board
std::string line;
for (int row = 0; row < boardSizeRows && std::getline(inputFile, line); row++)
    gameBoard.push_back(line);

// And finally print the board
for (const std::string& l : gameBoard)
    std::cout << l << '\n';

The condition in the for-loop when reading the file is checking first that we don't read more than requested in the file, and also that the read-operation went successfully.


If your compiler can't handle C++11 range-base for-loops (used to print out the board), you have to do it the "old-fashioned" way with iterators:

std::vector<std::string>::const_iterator li;
for (li = gameBoard.begin(); li != gameBoard.end(); li++)
    std::cout << *li << '\n';
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621