为什么我的程序停止工作了?我的指示是错的吗?
Why has my program stopped working? Are my pointers wrong?
我正在编写一个程序,它可以从如下格式的文件中读取一些数据:
21
285 270 272 126 160 103 1
31
198 180 163 89 94 47 1
32
240 230 208 179 163 104 1
33
15 13 12 14 15 15 0
34
63 61 62 24 23 20 2
我试图读取第一个数字到一个指针数组和其他7个数字到一个并行的二维指针数组,但由于某种原因,每次我运行我的代码它只是停止工作。它没有返回一个错误,但我觉得我的指针使用是错误的,因为这是我第一次使用指针。数据文件名为"election_data_121.txt"。下面是代码。如果有人能看一看,我将非常感激:
#include <iostream>
#include <fstream>
using namespace std;
//bool openFileIn(fstream &, char *);
int main()
{
const int PREC_SIZE = 30;
const int CANIDATES = 7;
int *precinct_num[PREC_SIZE];
int *num_votes[PREC_SIZE][CANIDATES];
cout << "Declarations made." << endl;
fstream dataFile; //Make a file handle
cout << "File object made." << endl;
//Open the file and check that it opened correctly
if(!openFileIn(dataFile, "election_data_121.txt"))
{
cout << "File open error!" << endl;
return 0; //Exit the program
}
cout << "File opened." << endl;
//Read the contents of the file into the proper arrays
int counter = 0;
while(!dataFile.eof())
{
dataFile >> *precinct_num[counter];
for(int i = 0; i < 7; i++)
{
dataFile >> *num_votes[counter][i];
}
counter++;
}
//Print out the data
for(int j = 0; j < counter; j++)
{
cout << *precinct_num[j];
for(int i = 0; i < 7; i++)
{
cout << *num_votes[j][i];
}
}
dataFile.close();
cout << "End of file";
return 0;
}
bool openFileIn(fstream &file, char *name)
{
file.open(name, ios::in);
if(file.fail())
return false;
else
return true;
}
再次感谢!
这段代码根本不需要指针;你为什么认为是这样呢?只要改变precinct_num
和num_votes
的类型,并停止对它们的解引用,我认为(乍一看)应该没问题。
#include <iostream>
#include <fstream>
using namespace std;
//bool openFileIn(fstream &, char *);
int main()
{
const int PREC_SIZE = 30;
const int CANIDATES = 7;
int precinct_num[PREC_SIZE];
int num_votes[PREC_SIZE][CANIDATES];
cout << "Declarations made." << endl;
fstream dataFile; //Make a file handle
cout << "File object made." << endl;
//Open the file and check that it opened correctly
if(!openFileIn(dataFile, "election_data_121.txt"))
{
cout << "File open error!" << endl;
return 0; //Exit the program
}
cout << "File opened." << endl;
//Read the contents of the file into the proper arrays
int counter = 0;
while(!dataFile.eof())
{
dataFile >> precinct_num[counter];
for(int i = 0; i < 7; i++)
{
dataFile >> num_votes[counter][i];
}
counter++;
}
//Print out the data
for(int j = 0; j < counter; j++)
{
cout << precinct_num[j];
for(int i = 0; i < 7; i++)
{
cout << num_votes[j][i];
}
}
dataFile.close();
cout << "End of file";
return 0;
}
bool openFileIn(fstream &file, char *name)
{
file.open(name, ios::in);
return !file.fail();
}
是的,看起来你对指针的理解有缺陷。我不明白你为什么在这里使用指针语法。只要删除指针定义和解引用,它应该做你想要的。你正在声明一个指向整型的指针数组。也就是说,数组的每个元素都包含一个指向整型而非整型的指针。您的代码似乎希望每个数组元素都包含一个int类型。
我建议你找一本关于c++指针的好教程。
首先需要为precinct_num和num_votes分配一些空间。要么使用malloc/calloc,要么直接把它放在堆栈上
您应该更改precinct_num和num_votes变量的声明。现在它们是指向数组的指针。你遇到的错误是这些指针没有指向任何东西!所以你不需要指针指向数组。你只需要直接得到数组。所以改成:
int precinct_num[PREC_SIZE];
int num_votes[PREC_SIZE][CANIDATES];
然后,当你读入值时,你只需要:
dataFile >> precinct_num[counter];
和
dataFile >> num_votes[counter][i];
等等……
这只是指针方面。我没有查看代码的总体情况,看看它是否在做正确的事情。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- 我怎么知道我最后的指示
- 为什么我的程序停止工作了?我的指示是错的吗?