Multidimensional Array help please

I'm making a program due tomorrow where we have to use a multidimensional array. The program is supposed to call for the name of a file. The file will be in this format:
X O .
. . .
. O .

We need to have a multidimensional array which save this information into itself, and then we will print it out with a board in this format
X | O |
---+---+---
| |
---+---+---
| X |

(sorry I just realized the formatting didn't come in correctly there... Imagine them to be nice squares...)
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
34
  #include <iostream>
#include <fstream>
using namespace std;

void getFileName(char fileName[])
{
   cout << "Enter source filename: ";
   cin >> fileName;
}

int main()
{
   char board[3][5];
   char fileName[256];
   getFileName(fileName);

   ifstream fin(fileName);
   
   for (int row = 0; row < 3; row++)
      for (int column = 0; column < 5; column++)
      {
         board[row][column] = fin;
         //fin = board[row][column];
      }

   for (int row = 0; row < 3; row++)
      for (int column = 0; column < 5; column++)
      {
         cout << board[row][column] << " ";
      }
   
   return 0;
   
}


Will the code I have do the trick? I'm currently getting an error on the first for loop where I'm trying to save board[row][column] = fin. It's saying something about unable to convert void* to char or something like that. I'm not quite sure what it means.

I guess my main issue is I have absolutely no clue how to work with multidimensional arrays, and my book isn't helping much and the lecture on the topic isn't until tomorrow...
Last edited on
fin is an object of ifstream, you cannot assign directly an object to a primitive type like int char.. etc.

i think you array size should be 3x3 not 3x5, spaces *aren't read when reading files*

You need to have a temporary variable where you will extract the readed character, then copy it to board[ index ][ index ] like :
1
2
3
4
5
6
char temp;
for( int i = 0; i < 3; i++ ) {
        for( int j = 0; j < 3; j++ ) {
            fin >> temp; // extract a character to temp variable
            board[ i ][ j ] = temp;
        }


And IMO, you should read a file instead using while() loop with fin >> temp as the condition, this is to prevent accidental reading even if something went wrong w/ the file

more datails here :
http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Topic archived. No new replies allowed.