I'm stuck on trying to get the rows and columns to read from the data file of the matrix

I need it to look like the below output however I am having trouble getting it to work. This is what I have so far. Can someone help me with the code I would write to get it to output like this:

l i o n s r a e b
s t a w n e l p a
d n a w s g a a d
i n e r g i o n n
o z o c a t k d a
o h a w k t o a p

my compiler has all equal signs (which I need a boarder.) I need to grab a file that the user inputs which can be any file. So far I created a demo one called animals.txt and it looks like this:

6 9
lionsraeb
stawnelpa
dnawsgaad
inergionn
ozocatkda
ohawktoap

This is my code so far:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

using namespace std;

const int ROWS=22;
const int COLS=22;
int rows;
int cols;
int x;


void print(char m[ROWS][COLS]);
void fill(char m[ROWS][COLS]);
int PuzzleInfo();



int main()
{
ifstream incode;//need on function
string lineA;
int arrayA [rows][cols];
string inputFile;
char mat[ROWS][COLS];//need on function

cout<<"Enter a file name:";
cin>>inputFile;
cout<<endl;

incode.open(inputFile.c_str());

incode>>rows;//need on function
incode>>cols;//need on function

fill(mat);
print(mat);




incode.close();

return 0;

while (incode)
{
while(getline(incode, lineA))
{
istringstream streamA;
cols=0;
while(streamA>>x)
{
arrayA[rows][cols] =x;
cols++;
}
rows++;
}
for (int r= 0; r<rows; r++)
{
for(int c=0; c<cols; c++)
{
incode>>rows;
incode>>cols;
}
}

}
}

void fill(char m[ROWS][COLS])
{
for(int r=0; r<ROWS; r++)
{
for(int c=0; c<COLS; c++)
{
m[r][c] = '=';
}
}
}

void print(char m[ROWS][COLS])
{
for(int r=0; r<ROWS; r++)
{
for(int c=0; c<COLS; c++)
{
cout << m[r][c] <<" ";
}
cout<<endl;
}
}
Last edited on
You don't need to use getline. You can use cin.get to get the individual character. Here is the link about it:

http://www.cplusplus.com/reference/istream/istream/get/

Edit: And if you still want to use getline then use an std::string with it and not a stringstream. you can input a line in the string and then can easily access a character in it using string.at() function. Here is the link to it:

http://www.cplusplus.com/reference/string/string/at/
Last edited on
Ok what if I change it to a function instead. I am trying to get my text file into the matrix. The file can be a variety of different sizes. It still will not input the file I need it to read.

I add this to it:

int fillPuz(char m [ROWS][COLS])
{

ifstream incode;
char mat[ROWS][COLS];

if ( !incode ) exit( 1 );
int rows, cols;
incode>>rows>>cols;

int **a = new int *[rows];

for ( int r = 0; r < rows; r++ )
{
a[r] = new int[cols];//getting an error
}


for ( int r = 0; r < rows; r++ )
{
for ( int c = 0; c < cols; c++ )
{
incode >> a[r][c];
}
}
return 0;
}
Okay, I'm looking at it. Will edit this post shortly.

[edit] Alright. Don't forget the proper includes.

1
2
3
4
#include <ciso646>
#include <fstream>
#include <iostream>
#include <string> 

The first thing you want to do is create the TYPES of things you will be manipulating. In your case, you have a game board of variable use. (It's size is constant; what changes is how many cells in the game board you actually use.)

1
2
3
const int MAX_ROWS = 100;
const int MAX_COLS = 100;
typedef char Gameboard[MAX_ROWS][MAX_COLS];

Now you can create the global objects your game uses. (Yes, this is perfectly fine.)

1
2
3
Gameboard gameboard;
int rows = 0;
int cols = 0;

Next, you want to create FUNCTIONS that do stuff TO or WITH your gameboard.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void LoadGameboard( const string& filename )
{
  // Open the named file
  ifstream f( filename );

  // Get the number of rows and columns
  f >> rows >> cols;

  // Skip all whitespace to the first element of the gameboard
  f >> ws;

  // (It is a good idea to check that your input is safe.)
  if ((rows > MAX_ROWS) or (cols > MAX_COLS))
    throw "File contains gameboard that is too large.";

  // For each row
  for (int row = 0; row < rows; row++)
  {
    // For each column
    for (int col = 0; col < cols; col++)
    {
      // Get the puzzle piece
      f >> gameboard[row][col];
    }

    // Skip to the beginning of the next row (if any)
    f >> ws;
  }

  // (Make sure that the gameboard loaded properly)
  if (f.fail())
    throw "File contains invalid gameboard.";
}

Another useful function, using the same kind of double loop:

1
2
3
4
void DisplayGameboard()
{
  ...   // (you fill in the code here)
}

Your main function, then, is much simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
  // Ask the user for the game board file name
  string filename;
  cout << "Gameboard filename? ";
  getline( cin, filename );

  // Load it (if possible)
  LoadGameboard( filename );

  ...

  // Use it
  bool done = false;
  while (!done)
  {
    DisplayGameboard();

    // Get user input
    // etc
  }
}

If you want to see those error messages if something goes wrong, wrap stuff in a try..catch block. You can put one around the entire main function if you wish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
  try { 
    // Ask the user for the game board file name
    string filename;
    cout << "Gameboard filename? ";
    getline( cin, filename );

    // Load it (if possible)
    LoadGameboard( filename );

    ...

    // Use it
    bool done = false;
    while (!done)
    {
      DisplayGameboard();

      // Get user input
      // etc
    }
  }
  catch (const char* error_message)
  {
    cerr << error_message << "\n";
  }
}

Hope this helps.
Last edited on
Topic archived. No new replies allowed.