I am a beginner at this. I'm trying to read a file and put it into a 2D array. Here is my code. after it outputs the file it displays the garbage in the memory and the loop never ends unless it hits 50.
include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
char arr[50][50];
ifstream fin;
fin.open("Map.txt");
for (int i = 0; i < 50; i++)
{
for ( j = 0; j < 50; j++)
{
fin.get(arr[i][j]);
}
}
for (int i = 0; arr[i]!=NULL; i++)
{
for (int j = 0; arr[j]!=NULL; j++)
{
cout<< arr[i][j];
}
}
}
The text file looks like this
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@ @@
@@ @@
@@ @@
@@ ^ @@
@@ @@
@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@@@@@@@@@@@@@@@ @@
@@ @@
@@ x x @@
@@ @@
@@ o @@
@@ @@
@@ o @@
@@ @@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
arr[j]in inner loop seems wrong and why you can't write same conditions as in input loops?