   [C++] Input text file into an array
  Hello!I'm trying to create a battleship game with a LoadGame function. I've been trying this same function for the past 3 hours and can't get passed it. The external text file looks like this...X *   * * * * * * * X * * * * * * * * * * * * * * * * * * * * * * * X X * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * X * * * * * * * * * * * * * * * * * * * * * * And I want to put it into an array called gameboard[10][10]. This is what I think is the closest answer to my problem...void LoadGame(string gameboard[10][10]){fstream inputfile;inputfile.open ("Output.txt",ios::in);if (inputfile.fail()){cout << "Cannot open the output file! Terminating ...";exit (1);}else {while (!inputfile.eof()){for (int i=0;i<10;i++)for (int j=0;j<10;j++)getline(inputfile,gameboard[j],' ');}}inputfile.close();}
  Is there a reason why you're storing the board as a 2d array of strings instead of chars?  In any case, try using the extraction operator, instead of getline, since extraction by default ignores whitespace:char gameboard[10][10] = {};for ( int row = 0; row < 10; ++row )for ( int col = 0; col < 10; ++col )inputfile >> gameboard[row][col];;  Okay It worked perfectly! Thank you!However, if I do have a blank space in my text file that marks a missed shot... how do I get that as an input?EX: X * * * * * * * * *X * ** * * * ** * * * * * * * * ** * * * X X * * * ** * * * * * * * * **   * * * * * *   ** * * * * * * * * ** * * * * * * X * ** * * * * * * * * ** * * * * * * * * *   ;Quote:Original post by keikawaguchiHowever, if I do have a blank space in my text file that marks a missed shot... how do I get that as an input?Quite simply, use something other than a blank space to indicate a miss, such as a period:char ch;char gameboard[10][10] = {};for ( int row = 0; row < 10; ++row ) {for ( int col = 0; col < 10; ++col ) {inputfile >> ch;if ( ch == '.' )ch = ' ';gameboard[row][col] = ch;}}<a href=">[Edited by - _fastcall on June 10, 2010 12:43:10 AM]