gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:52:45 26 4
gpt4 key购买 nike

好的,很抱歉又问了一个这样的问题。我环顾了 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com