How do you read and display a 2d array?
the input text is below
i just really have no idea of tried everything :(
any sort of help would be appreciated

* . . . . . . . . . . . . . . . . . . *
. * . . . . . . . . . . . . . . . . * .
. . * . . . . . . . . . . . . . . * . .
. . . * . . . . . . . . . . . . * . . .
. . . . * . . . . . . . . . . * . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . * . . . . . . . . . . * . . . .
. . . * . . . . . . . . . . . . * . . .
. . * . . . . . . . . . . . . . . * . .
. * . . . . . . . . . . . . . . . . * .
* . . . . . . . . . . . . . . . . . . *

Recommended Answers

All 9 Replies

heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works

wat im not sure about tho is storing information in a text file into a 2d array so lost!

int main()
{
    // VARIABLES
    string fileName;         // the name of the grid file
    char grid[SIZE][SIZE];   // a char array consiting of '*' or '.'
    string str;
    int rows = 0;
    int columns= 0;
    int i = 0;
    int j = 0;
    
    ifstream inFile;
    ofstream outFile;
    
    // FILENAME INPUT
    cout << "Enter the filename of the grid: " << flush;         
    cin >> fileName;                                               
    cout << endl;                                                 
    
    // STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }
    
    // PRINT USER INPUT
    inFile.open(fileName.c_str());
    
    for (int i = 0; i < SIZE; i++) 
    {
    getline(inFile, str);
    cout << str << endl;
    }   
 

    cout << endl;            
    system("pause");        
    return 0;                
}

Do you want to keep a poor user typing this grid char by char, line by line under your program control? It is hardly worth it. Let him/her prepare the grid in his/her favorite text editor then load the result text file in your 2D array. If you don't want to follow this way, why you ask him/her about inFile name?
That's code sceleton:

int i;
for (i = 0; i < SIZE && getline(inFile,str) && str.length() == SIZE; ++i)
{
    for (int j = 0; j < SIZE; ++j)
        grid[i][j] = str[j];
}
inFile.close();
if (i != SIZE)
{
     cout << "*** Invalid grid line #" << i << endl;
     return 1; // or what else...
}
// Now you have a filled grid
...
// Print it
for (int i = 0; i < SIZE; ++i)
{
    for (int j = 0; j < SIZE; ++j)
        cout << grid[i][j];
    cout << '\n';
}
cout.flush();
...

im confused?

i am asking them to input a file?

ive got it to output the txt file
i jsut cant get it store in a 2 d array

ure code helps a bit but im still lost as

Do you need to take in the input from the user ? And store those values into a file ?

Or Do you need to read from a file and then again put the same back into the file ?

Please Specify.

heres wat ive done so far
i can get it to display the txt file
but im kinda cheating and doing it in a string which still works

wat im not sure about tho is storing information in a text file into a 2d array so lost!

int main()
{
    // VARIABLES
    string fileName;         // the name of the grid file
    char grid[SIZE][SIZE];   // a char array consiting of '*' or '.'
    string str;
    int rows = 0;
    int columns= 0;
    int i = 0;
    int j = 0;
    
    ifstream inFile;
    ofstream outFile;
    
    // FILENAME INPUT
    cout << "Enter the filename of the grid: " << flush;         
    cin >> fileName;                                               
    cout << endl;                                                 
    
    // STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }
    
    // PRINT USER INPUT
    inFile.open(fileName.c_str());
    
    for (int i = 0; i < SIZE; i++) 
    {
    getline(inFile, str);
    cout << str << endl;
    }   
 

    cout << endl;            
    system("pause");        
    return 0;                
}

You have rows and columns initialized to 0 in lines 7 and 8 and they never change, so your for loop in lines 21 through 27 will never execute. You need to initialize row and columns. ArkM has a good point. For a grid like yours, that's an awful lot of typing. Better to read from a file. If it 's a small matrix, like 4 x 4, you may want to type it in to get it working. Use ArkM's code to display. It'll work, assuming that rows, columns, and SIZE are all the same.

// Print it
for (int i = 0; i < SIZE; ++i)
{
    for (int j = 0; j < SIZE; ++j)
        cout << grid[i][j];
    cout << '\n';
}

Do you need to take in the input from the user ? And store those values into a file ?

Or Do you need to read from a file and then again put the same back into the file ?

Please Specify.

no

their is a text file with a 20x20 grid called in1.txt

the user types in1.txt

the program is meant to store it in an array

then print that array on screen

and it will be the same as the txt input

Hey Timb, If you are using the same program till now, I see that you have initialised your rows and columns to zero, and therefore your program doesnt even start up getting in data from the file itself. I guess you will need to initialise to 19 both and then try them out.

no

their is a text file with a 20x20 grid called in1.txt

the user types in1.txt

the program is meant to store it in an array

then print that array on screen

and it will be the same as the txt input

If you are reading from a file, don't use cin in this loop:

// STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }

If you are reading from a file, don't use cin in this loop:

// STORE USER INPUT
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            {
            cin >> grid[i][j];
            }
    }

Yes you should use Infile instead of cin.

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.