0

The code below is my main. I wrote a method to create a random file. The first line is two numbers. Number one is the number of rows, number 2 is the number of columns. Then I get a square of random numbers based on the first 2 values. That works fine, I get files the way I want them. My problem is the first if statement must be giving me true since my output is "There is no file to use." so according to that im guessing my file is not being seen.

I'll figure out how to make the array with the correct values after this is fixed but I can not make it work. I even tried changing grid1.txt to the file adress. yes i'm new at this and yes I did search the internet quite a bit trying to figure this out. Ive been stumped on what my issue is for like 2 days now. So could someone please help me figure out why my file is not being seen by this program?

int row1 = 0;
int col1 = 0;

generateGridFile(row1, col1);

ifstream gridFile("grid1.txt");

gridFile.open("grid1.txt");

if (! gridFile)
{
    cout << "There is no file to use.";
    return 0;
}

while (!gridFile.eof())
{

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

    for (int i = 0; i < row1; i++)
    {
        a[i] = new int[col1];
    }

        for (int i = 0; i < row1; i++)
        {
            for (int j = 0; j < col1; j++)
            {
                a[i][j] = 1; // gridFile >> a[i][j];
                cout << a[i][j];
            }
            cout << "\n";
        }
}
return 0;

1 Answers1

0

The gridFile.open() call is not superfluous.

According to this, calling open() when the stream is already open (as you did by calling a non-default constructor) will fail, thus making your code step into the condition.

Just remove the line ;-)

Magix
  • 3,557
  • 3
  • 18
  • 43