c++ - Why has my program stopped working? Are my pointers wrong? -


i'm working on program reads data file formatted 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 read first number 1 pointer array , other 7 numbers parallel 2 dimensional pointer array reason, every time run code stops working. it's not returning error feel pointer usage wrong because first time using pointers. data file called "election_data_121.txt", fyi. heres code. if take i'd 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 file handle     cout << "file object made." << endl;     //open file , check opened correctly     if(!openfilein(datafile, "election_data_121.txt"))     {         cout << "file open error!" << endl;         return 0; //exit program     }      cout << "file opened." << endl;      //read contents of file proper arrays     int counter = 0;     while(!datafile.eof())      {         datafile >> *precinct_num[counter];         for(int = 0; < 7; i++)         {             datafile >> *num_votes[counter][i];         }         counter++;     }      //print out data     for(int j = 0; j < counter; j++)     {         cout << *precinct_num[j];         for(int = 0; < 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 @ all; why think does? change types of precinct_num , num_votes , stop dereferencing them , think (at glance) should 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 file handle     cout << "file object made." << endl;     //open file , check opened correctly     if(!openfilein(datafile, "election_data_121.txt"))     {         cout << "file open error!" << endl;         return 0; //exit program     }      cout << "file opened." << endl;      //read contents of file proper arrays     int counter = 0;     while(!datafile.eof())      {         datafile >> precinct_num[counter];         for(int = 0; < 7; i++)         {             datafile >> num_votes[counter][i];         }         counter++;     }      //print out data     for(int j = 0; j < counter; j++)     {         cout << precinct_num[j];         for(int = 0; < 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(); } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -