C++ 如何测试数组元素是否为空?

C++ 如何测试数组元素是否为空?,c++,arrays,pointers,testing,null,C++,Arrays,Pointers,Testing,Null,为了简化它,我需要从文件中读取数字并将其存储在二维数组中。然后我必须检查以确保文件中有足够的数字来填充数组 文件中的前两个数字用于声明数组应有多少行和列 我正在努力解决的问题是,文件中的数字也可能包含0 我使用这个方法来测试元素是否为空 double numbers[MAX_ROW][MAX_COL]; for(int i = 0; i <= row; i++) { for(int n = 0; n <= col; n++) { if(!numbers[

为了简化它,我需要从文件中读取数字并将其存储在二维数组中。然后我必须检查以确保文件中有足够的数字来填充数组

文件中的前两个数字用于声明数组应有多少行和列

我正在努力解决的问题是,文件中的数字也可能包含0

我使用这个方法来测试元素是否为空

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;
        }

    }
}
这将不起作用,因为文件中为零

   3    3
   1    3   4
   3    0   4
   3    5   2
这是我想测试的错误类型。 它说有3行3列,但没有数字填充数组的其余部分。因此,它们将被初始化为0,您可以得出结论,这也将导致相同的问题

   3    3
   1    3   4
   3    2   4
   3    
有人知道我如何测试空元素而不是包含0的元素吗??还是我只是做错了什么

提前感谢您的帮助:

在我改变了之前的建议后

我设置了一个bool函数,如果文件中没有足够的数字,它将返回一个false语句。但是,即使文件具有正确数量的数字,该文件仍将执行if语句并返回假值。我的语法在某种程度上错了吗

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;

读取文件内容时必须捕获错误

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;
      }
   }
}

然后我必须检查以确保文件中有足够的数字来填充数组。最好的方法是跟踪成功读取的数字的计数。您应该发布两个示例文件:一个包含有效数据,另一个包含无效数据。这将允许其他人提出处理缺失数字的策略。我应该声明,程序被分配了一定数量的元素,它应该为每个文件保存这些元素。基本上idk文件中应该有多少元素,我只是修改了我在这里发布的函数来给出一个例子。我希望现在它更容易理解@rsahu出于某种原因,每当我在循环中添加if语句时,如果该语句是从文件中读取的,那么每次都会执行。@MohamedElmalah,也许您可以添加一个break;在错误消息之后。我不认为这是问题所在,我只是觉得我的语法可能有点错误。我已经更新了我的问题,并添加了您建议的声明,但似乎有些地方不对劲。很抱歉要求这么多,但如果你能看看更新的问题,也许能帮我解决,我会非常感激@萨胡
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;
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;
      }
   }
}
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;
for(int i = 0; i <= row; i++) {
    for(int n = 0; n <= col; n++) {
        if(!(inFile >> numbers[i][n])) {
            return false;
        }
    }
}
return true;