To simplify it, I need to read numbers from a file and store them in a 2D array. I then must check to make sure that the there were enough numbers in the file to fill the array.
the first two numbers in the file are the ones that declare how many rows and columns there should be for the array.
The part I am struggling with is that the numbers in the file can also include a 0 in them.
I was using this method to test if an element was empty
double numbers[MAX_ROW][MAX_COL]; for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(!numbers[i][n]){ cout << "Error: There is not enough data found file :(...." << endl; cout << "The Program will now exit....." << endl; return 0; } } } But then I realized that the program would exit if the file contained the number 0. Which is something that I don't want to happen.
I also tried to using a pointer and testing for NULL but that gave me a warning about (comparison between NULL and non-pointer) and it would still do the same thing, if there was a 0 in the file it would just exit.
double (*ptrNumbers)[MAX_COL] = numbers; for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(ptrNumbers[i][n] == NULL){ cout << "Error: There is not enough data found file :(...." << endl; cout << "The Program will now exit....." << endl; return 0; } } } Example files:
This one works fine
3 3 1 3 4 3 2 4 3 5 2 This will not works because of the zero in the file
3 3 1 3 4 3 0 4 3 5 2 This is the type of error i would like to test for. It says there are 3 rows and 3 columns but there aren't numbers to fill the rest of the array. Therefore they will be initialized to 0 which as you can conclude will also cause the same problem.
3 3 1 3 4 3 2 4 3 Anyone have any idea how I can test for "empty" elements but not elements containing 0s?? Or am I just doing something wrong?
Thanks in advance for any help :)
After I altered my program from the previous recommendations.
I set up a bool function to return a false statement if there was not enough numbers in the file. However even if the file had the correct amount of numbers the file would still execute the if statement and return a false value. Is my syntax wrong in some way?
for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(!(inFile >> numbers[i][n])) { return false; } else { inFile >> numArray[i][n]; } } } return true;