954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading in a file into a 2d array

I would like to note that this is for John Conway's game of life but I must write it in C++. I am making a 10x10 version of the game and I am using jGRASPto write the code. I am having a problem when it is reading in the starting set of data from a text doc. The game of Life page is http://www.bitstorm.org/gameoflife/

code for game
#include
#include
#include
using namespace std;

//using std::cout;
//using std::cin;
//using std::endl;
//using std::ios;
//using std::ifstream;
//using std::ofstream;

void printArray2(int array[][10], int rows, int cols);
void printArray3(int array[][10], int row, int col);

int main(void){
int a00 = 0,a01 = 0,a02 = 0,a03 = 0,a04 = 0,a05 = 0,a06 = 0,a07 = 0,a08 = 0,a09 = 0,
a10 = 0,a11 = 0,a12 = 0,a13 = 0,a14 = 0,a15 = 0,a16 = 0,a17 = 0,a18 = 0,a19 = 0,a20 = 0,
a21 = 0,a22 = 0,a23 = 0,a24 = 0,a25 = 0,a26 = 0,a27 = 0,a28 = 0,a29 = 0,a30 = 0,a31 = 0,
a32 = 0,a33 = 0,a34 = 0,a35 = 0,a36 = 0,a37 = 0,a38 = 0,a39 = 0,a40 = 0,a41 = 0,a42 = 0,a43 = 0,
a44 = 0,a45 = 0,a46 = 0,a47 = 0,a48 = 0,a49 = 0,a50 = 0,a51 = 0,a52 = 0,a53 = 0,a54 = 0,a55 = 0,
a56 = 0,a57 = 0,a58 = 0,a59 = 0,a60 = 0,a61 = 0,a62 = 0,a63 = 0,a64 = 0,a65 = 0,a66 = 0,a67 = 0,
a68 = 0,a69 = 0,a70 = 0,a71 = 0,a72 = 0,a73 = 0,a74 = 0,a75 = 0,a76 = 0,a77 = 0,a78 = 0,a79 = 0,
a80 = 0,a81 = 0,a82 = 0,a83 = 0,a84 = 0,a85 = 0,a86 = 0,a87 = 0,a88 = 0,a89 = 0,a90 = 0,a91 = 0,
a92 = 0,a93 = 0,a94 = 0,a95 = 0,a96 = 0,a97 = 0,a98 = 0,a99 = 0;


int array[10][10] = {{a00,a01,a02,a03,a04,a05,a06,a07,a08,a09},{a10,a11,a12,a13,a14,a15,a16,a17,a18,a19},
{a20,a21,a22,a23,a24,a25,a26,a27,a28,a29},{a30,a31,a32,a33,a34,a35,a36,a37,a38,a39},
{a40,a41,a42,a43,a44,a45,a46,a47,a48,a49},{a50,a51,a52,a53,a54,a55,a56,a57,a58,a59},
{a60,a61,a62,a63,a64,a65,a66,a67,a68,a69},{a70,a71,a72,a73,a74,a75,a76,a77,a78,a79},
{a80,a81,a82,a83,a84,a85,a86,a87,a88,a89},{a90,a91,a92,a93,a94,a95,a96,a97,a98,a99}};

// printArray2(array,10,10);
int o;
int p;
int num =1;
int m;
int n;
string snum;


ifstream inClientFile("input2.dat", ios::in);

if (!inClientFile)
{
cout<<"file could not be opened"<

sladesan
Newbie Poster
10 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

It print out a 10 x 10 matrix of 49 not the input values. I got the most of the other part of the project working now.

sladesan
Newbie Poster
10 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Assuming your input file is in the form of a whitespace separated 10x10 table of ints then reading it in could be as straightforward as :

for(n=0; n<10; n++)// col. number
{
  for(m=0; m<10; m++)// rows number
  {
     inClientFile >> array[m][n];
  }
}


in your version:

for(n=0; n<10; n++)// col. number
{
  for(m=0; m<10; m++)// rows number
  {
    array[m][n]=num;
  }
}


the value of num never changes and all values of array[m][n] will be the same.

I would note in passing that, generally, rows are read in one column at a time

for()  //controls rows
  for() //controls columns

rather than columns being read in one row at a time

for() //controls columns
  for() //controls rows


but you will want to confirm you are in row major as opposed to column major mode as it is possible to do it your way. It just seems clumsier under usual circumstances.

NB: To have you code appear indented on this board like the way you write it (I hope), place code tags before and after the code section (see the FAQ if you're not sure how to do it).

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You