So I want to make a text-based game that has different maps, displayed in ASCII art-style. I've been tinkering with fstream, and I wrote up this code:

#include <iostream>
using namespace std;
#include <fstream>

int main()
{
    ifstream indata;
    int i;
    int j;
    int k;
    char grid[9][9];
    char letter;
    indata.open("grid.txt");
    indata >> letter;
    while(!indata.eof() )
    {
        grid[i][j] = letter;
        indata >> letter;
        i++;
        k++;
        if(k == 10)
        {
            k = 0;
            j++;
        }
    }
    for(int l; l < 10; l++)
    {
        cout << endl;
        for(int m; m < 10; m++)
            cout << grid[l][m];
    }
}

It should take the contents of the grid.txt file and map them to the grid array, and then display the grid array. I'm doing this to save me a couple hundred lines of code, because otherwise I would have to change each array element's value for each map. This way I can just write the ASCII map in a .txt file and then add it to an array.
This is where the problem comes in. When I run the program, it says "FileTests.exe has stopped working" (FileTests, of course, being the file name). Does anyone know what I'm doing wrong, or if there is a simpler solution to mapping an array from a file?

PS: I've included the contents of the grid.txt file, simply some characters and numbers to test the potential of this mapping solution:

1234567890
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ<>?!
1234567890
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ<>?!
1234567890
ABCDEFGHIJ

You are doing it the hard way.
First, make the dimensions of grid large. This way you can have different size maps if you want them.

Read a line into the first row of grid[0]
Read the next line into grid[1]
etc.
Count the number of lines as you read them.

Then just output each row in a loop.

You are doing it the hard way.
First, make the dimensions of grid large. This way you can have different size maps if you want them.

Read a line into the first row of grid[0]
Read the next line into grid[1]
etc.
Count the number of lines as you read them.

Then just output each row in a loop.

How do I read a certain line of a file? I know that you can use .getline(), but I don't see any parameters that allow you to enter a specific line in the file.

You don't. You read the first line, then the second, then the third...

I thought you were reading a map. Why would you want only one line of the map?

I would read each individual line of the map file (maybe with a for loop), and then assign the line's contents to each grid array element. Mostly, I need a function to start reading at a certain point in the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.