0

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; 
5
  • 2
    I then must check to make sure that the there were enough numbers in the file to fill the array. The best way to do that would be to keep track of the count of numbers that were successfully read. Commented Feb 8, 2017 at 6:27
  • You should post two sample files: one containing valid data and the other containing invalid data. That would allow others to suggest a strategy for dealing with missing numbers. Commented Feb 8, 2017 at 6:29
  • I should have stated that the program is assigned a certain amount of elements it should hold for every file. Basically idk how many elements are supposed to be in the file, I merely modified the functions i posted here to give an example. Commented Feb 8, 2017 at 6:29
  • I hope now it is a little bit more understandable. @RSahu Commented Feb 8, 2017 at 6:37
  • You may want to take a look at the Eigen library Commented Feb 8, 2017 at 7:44

1 Answer 1

1

You have to catch error while reading the contents of the file.

std::ifstream ifile("The input file"); ifile >> row >> col; for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if( ! (ifile >> ptrNumbers[i][n])) { // Problem reading the number. cout << "Error: There is not enough data found file :(...." << endl; cout << "The Program will now exit....." << endl; } } } 

Update

The updated function is faulty.

for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(!(inFile >> numbers[i][n])) { return false; } else { // You are now reading into the same element // of the array again. inFile >> numArray[i][n]; } } } return true; 

You don't need the else part in that function.

for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(!(inFile >> numbers[i][n])) { return false; } } } return true; 
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason whenever I added that if statement inside the loop that was meant to read from the file, it would execute every time.
@MohamedElmalah, perhaps you can add a break; after the error message.
I don't think that is the issue with it, I just think my syntax may be wrong somehow. I have updated my question and added the statement that you recommended but something seems to be wrong. Sorry for asking for so much but I would really appreciate it if you can look at the updated question and maybe help me out. @RSahu

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.