开发者

Why has my program stopped working? Are my pointers wrong?

I'm working on a program which reads some data from a file formatted like so:

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

I'm trying to read the first number into one pointer array and the other 7 numbers into a parallel two dimensional pointer array but for some reason, every time I run my code it just stops working. It's not returning an error but I feel like my pointer usage is wrong because this is my first time using po开发者_如何学运维inters. The data file is called "election_data_121.txt", fyi. Heres the code. If anyone could take a look I'd be so grateful:

#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;
}

Thanks again!


This code doesn't need pointers at all; why would you think it does? Just change the types of precinct_num and num_votes and stop dereferencing them and I think (at a glance) that it should be fine.

#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();
}


Yes it looks like your understanding of pointers is flawed. I don't see why you are using pointer syntax at all here. Just remove the pointer definitions and dereferences and it should do what you want. You are declaring an array of pointers to ints. That is each element of the array holds a pointer to an int not an int. Your code seems to want each array element to hold an int.

I would suggest you just look up a good tutorial on C++ pointers.


You first need to allocate some space for precinct_num and num_votes. Either use malloc/calloc or just place it on the stack.


You should change the declaration of your precinct_num and num_votes variables. Right now they are pointers to arrays. The bug you are running into is these pointers aren't pointing to anything! So you don't want pointers to arrays. You just want the arrays directly. So change them to:

int precinct_num[PREC_SIZE];
int num_votes[PREC_SIZE][CANIDATES];

Then later, when you're reading in the values, you just want to:

dataFile >> precinct_num[counter];

and

dataFile >> num_votes[counter][i];

etc...

That's just the pointer aspect. I haven't looked at the code in general to see if it's doing the right thing overall.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜