c++ - 运行时检查错误2

标签 c++ arrays variables

好的,很抱歉又问了一个这样的问题。我环顾了 staackoverflow 寻找答案。我在变量 nums 周围得到错误堆栈。这是否意味着我在变量 nums 附近键入了一些额外的代码或其他什么?我试过弄清楚,但找不到。我是编码的菜鸟,所以很抱歉。这是我的代码,我认为问题出在我的

cout << "The numbers on file are:\n " << nums, size; 

我调用了 main 上面的 read_data 但我找不到问题所在。谢谢!

我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void read_data(int nums[], int size);

int main()
{
const int size = 24;
ifstream dataIn;
int nums[size];

   read_data(nums,size);

   cout << "The numbers on file are:\n " << nums, size;

system("PAUSE");
return 0;
}

void read_data(int nums[], int size)
{
    ifstream dataIn;
    dataIn.open("walrus.txt");

    if( dataIn.fail() )
            {
                    cout << "File does not exist." << endl;
                    exit(1);
            } 

    int count;
    for ( count = 0; count < size; count++ )
    {
             dataIn >> nums[size];
    }

    dataIn.close();
}

最佳答案

关于运行时问题-

dataIn >> nums[size];

尝试越界访问数组索引是未定义的行为。大小为 N 的有效数组索引为 0N-1

关于c++ - 运行时检查错误2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674737/

相关文章:

c++ - c++11 中的 gettimeofday() 等价物是什么

php - 如何更改 While 循环内的单行数组变量并使用新结果更新数据库表

python - 从不同线程修改 Python 字典

php - 从数组中分配变量

php - 如何在变量中存储包含哈希标记的完整 url?

c++ - 将 Boost 与 cmake 和 clang 链接 - undefined symbol 引用

c++ - 将一个结构写成二进制,其中一个成员是一个字符串?

c++ - 警告 MSB8012 - 在 Visual Studio 2015 上构建 Visual Studio 2003 项目

python - numpy 遍历两个二维数组

Java ArrayList<String[]>