Michael Ramos
Michael Ramos

Reputation: 39

C++ 2d array from .txt file

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

Upvotes: 0

Views: 16185

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409442

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

Upvotes: 0

Spoonwalker Highwater
Spoonwalker Highwater

Reputation: 411

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;

}

Upvotes: 3

Binayaka Chakraborty
Binayaka Chakraborty

Reputation: 1335

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;

Upvotes: 2

Related Questions