写入动态数组

Write to dynamic array

问题:

我使用指针创建了一个动态数组,并成功地打开了一个文本文件,将整数的数量读入一个int指针。现在我试图将文本文件中的项目按顺序存储到数组中。

int * x;
x = new int;
*x = 0;

int * arraySize;
arraySize = new int;
*arraySize = 0;

int * temp;
temp = new int;
*temp = 0;

// The arraySize is populated. It's not zero when this runs.

int * intArray; // The output is the same even if I set equal to something

intArray = new (nothrow) int[*arraySize];

if (intArray == nullptr)
    cout << "Error: memory could not be allocated";
else
{
    do
    {
        file >> *temp;
        intArray[*x] = *temp;
        *x = *x + 1;
        cout << intArray[*x];

    } while (*x < *arraySize);

上面使用cout给我看测试输出,它只产生一个内存地址(所有数组位置重复相同的内存地址)。

-842150451

期望的行为: (因为删除它的人显然不明显 "off topic" )

动态数组应使用文本文件中的整数填充

我试过的:

使用此方法将每个数组元素设置为整数 27,这恰好是文本文件中的最后一个数字。

file >> *temp;
intArray[*x] = *temp;
cout << intArray[*x] << endl;
*x = *x + 1;

在文本文件末尾添加不同的数字只会改变发送到数组所有位置的数字。

在循环外使用:

cout << "\n" << intArray[5];

无论使用哪个索引位置,都显示相同的数字。

所以我真正的问题似乎是我无法为每个数组位置分配不同的值。

使用这种方法:

int* intArray = new int[*arraySize];

同理:

int * intArray; // The output is the same even if I set equal to something
intArray = new (nothrow) int[*arraySize];

实际上并没有解决问题。

重现问题的示例程序(我个人认为 post 整个程序是错误的,但每个回答的人都无法提供答案。他们坚持认为问题出在其他地方程序所以我被迫扩大问题):

#include <iostream>
#include <fstream>
#include <string>
#include <new>

using namespace std;

int main()
{
    int * arraySize;
    arraySize = new int;
    *arraySize = 483;

    int * temp;
    temp = new int;
    *temp = 0;

    int * x = 0;
    x = new int;
    *x = 0;

    ifstream file;
    file.open("test.txt");

    int* intArray = new int[*arraySize];

    if (intArray == nullptr)
    {
        cout << "Error: memory could not be allocated";
        file.close();
    }
    else
    {
        do
        {
            file >> *temp;
            intArray[*x] = *temp;
            cout << intArray[*x] << endl;
            *x = *x + 1;

        } while (*x < *arraySize);

        cout << "\n" << intArray[5];

        delete[] intArray;

        cout << "\n";
        system("pause");
        return 0;
    }
}

如果您 运行 上面的完整程序是一个没有任何文本文件的空控制台项目,它将列出所有零。如果你给它一个 test.txt 文件,它会列出文件中的最后一个数字。

同样,期望的行为:我试图用文本文件的所有项目填充数组。

我知道我使用的指针比常见的要多,而且我知道每个回答的人都认为问题出在代码的其他部分,但事实并非如此,我能够自己解决问题。

这个问题不应该被删除或搁置,因为至少在最初写的时候,它是关于一个特定问题的明确和中肯的。我别无选择,只能添加详细信息,因为许多不知道自己在做什么的用户投票否决了这个问题。

What am I doing wrong here?

您正在将输入读入 *temp

        file >> *temp;

您正在将 *temp 分配给 intArray[*x]

        intArray[*x] = *temp;

您正在递增 *x

        *x = *x + 1;

现在您正在打印 donationsArray[*x]cout

        cout << donationsArray[*x];

在打印到 cout 之前,我没有看到任何代码为 donationsArray[*x] 赋值。

您似乎正在打印未初始化数组元素的内容。

更新

换行

        cout << donationsArray[*x];

        cout << intArray[*x];

更有帮助。但是,它仍未初始化。请记住,在将 intArray[*x] 打印为 cout 之前,您已经递增了 *x。你需要的是切换这些线路。

        file >> *temp;
        intArray[*x] = *temp;
        cout << intArray[*x];
        *x = *x + 1;

为了清楚起见,您可以使用指针创建数组,如

int* ar = new int[10];

您似乎过度使用了指针。

此外,不要忘记使用

释放为数组分配的内存
delete[] ar;

我终于明白了。正如我一直说的,问题在于我如何填充数组。这是使用 for 循环正确填充我的指针数组的一整段代码:

#include <iostream>
#include <fstream>
#include <string>
#include <new>

using namespace std;

int main()
{
    int * arraySize;
    arraySize = new int;
    *arraySize = 483;

    int * temp;
    temp = new int;
    *temp = 0;

    int * x = 0;
    x = new int;
    *x = 0;

    ifstream file;
    file.open("test.txt");

    int* intArray = new int[*arraySize];

    for (* x = 0; *x < *arraySize; (*x = *x + 1))
    {
        file >> *temp;
        intArray[*x] = *temp;

        cout << intArray[*x] << endl;
    }

    delete[] intArray;

    cout << "\n";
    system("pause");
    return 0;
}