I'm working on a program that is supposed to load a 2D array from a file.
My file looks like this, with the first number indicating the size of the array:
10
7 6 9 4 5 4 3 2 1 0
6 5 5 5 6 5 4 3 2 1
6 5 6 6 7 6 8 4 3 2
1 5 6 7 7 7 6 5 4 3
5 5 6 7 6 7 7 6 5 9
5 6 7 6 5 6 6 5 4 3
5 6 7 9 5 5 6 5 4 3
5 5 6 7 6 6 7 6 5 4
5 9 5 6 7 6 5 0 3 2
5 5 5 5 6 5 4 3 2 7
And here is my code so far:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream prog;
prog.open("../prog1.dat");
//If file can't be opened, exit
if (!prog) {
cerr << "File could not be opened" << endl;
exit(EXIT_FAILURE);
}
else {
while (!prog.eof()) {
int size = 100, i, j;
prog >> size;
int **numArray = new int* [size];
for(i = 0; i < size; i++) {
numArray[i] = new int[size];
for(j = 0; j < size; j++) {
cout <<numArray[i][j] << " ";
}
cout << endl;
}
prog.close();
return 0;
}
}
}
But one of the errors I'm getting says that "expression must have a constant value" on the
int numArray[size][size];
portion of my code.
My problem is that I don't know how to go about making this a constant since I'm getting the size from the file as if I don't already know the size of the array.
This is my first C++ program and I'm pretty much teaching it to myself as I go since my professor seems to think that because we should know how to do these things in Java that we don't have to go over it in class. The examples I've found dealing with using constants say it should be something like this:
const int size = *value*;
but since my program is supposed to look in the file for the size, I'm not sure how to do this. Any suggestions? Also, like I said, I'm really new at this so if you happen to spot anything else in my code that needs to be fixed, that'd be greatly appreciated as well.