问题:
我使用指针创建了一个动态数组,并成功打开了一个文本文件,将整数的数量读入一个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
期望的行为: (因为对于将其删除的人来说显然不是很明显&#34;关于主题&#34;)
动态数组应填充文本文件中的整数
我尝试了什么:
使用此方法将每个数组元素设置为整数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];
它并没有真正解决问题。
一个重现问题的示例程序(我个人认为发布整个程序是错误的,但每个回答的人都无法提供答案。他们坚持认为问题出在程序的其他地方所以我被迫扩大问题):
#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;
}
}
如果您将上述COMPLETE程序作为没有任何文本文件的空控制台项目运行,它将列出所有零。如果你给它一个test.txt文件,它将列出文件中的最后一个数字。
再次,渴望的行为:我试图用文本文件的所有项目填充数组。
我知道我使用的指针多于普通指针,我知道每个回答的人都认为问题出现在代码的其他部分,但事实并非如此,我能够解决我自己的问题。
这个问题不应该被删除或搁置,因为至少在最初的书面文字中,这个问题很清楚,而且关于某个特定问题。我别无选择,只能添加细节,因为许多不知道自己在做什么的用户都在投票。
答案 0 :(得分:2)
我在这里做错了什么?
您正在阅读*temp
的输入内容。
file >> *temp;
您要将*temp
分配给intArray[*x]
intArray[*x] = *temp;
您正在递增*x
。
*x = *x + 1;
现在您要将donationsArray[*x]
打印到cout
。
cout << donationsArray[*x];
在打印到donationsArray[*x]
之前,我没有看到任何为cout
分配值的代码。
看起来您正在打印未初始化的数组元素的内容。
<强>更新强>
更改行
cout << donationsArray[*x];
到
cout << intArray[*x];
更有帮助。但是,它仍然没有初始化。请记住,在将*x
打印到intArray[*x]
之前,您已经增加了cout
。你需要的是切换那些线。
file >> *temp;
intArray[*x] = *temp;
cout << intArray[*x];
*x = *x + 1;
答案 1 :(得分:0)
为了清楚起见,您可以使用指针
创建一个数组int* ar = new int[10];
你似乎过度使用指针。
另外,不要忘记使用
释放为阵列分配的内存delete[] ar;
答案 2 :(得分:0)
我最终弄明白了。正如我一直说的那样,问题在于我是如何填充阵列的。这是使用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;
}