c++ - storing data from a file into an array -


i have written following code read data in columns file (area.inp) array. when showing data in screen, first "for" loop shows correct numbers (so code reads numbers file correctly) second "for" loop shows incorrect set of numbers. cannot head around problem. appreciate guidance on problem.

area.inp

001.000    003.000 002.000    004.000 006.000    005.000 004.000    002.000 002.000    001.000 

code

#include <iostream> #include <iomanip> #include <fstream> #include <algorithm> #include <iterator>   using namespace std;   int main(int argc, char **argv) {     int j=0;     double x[j];     double y[j];     static double *p;     double *q;     p=x;     q=y;   //**** counting number of lines      ifstream myfile("area.inp");     myfile.unsetf(ios_base::skipws);      int points = count(         istream_iterator<char>(myfile),         istream_iterator<char>(),         '\n');      cout << "number of data in file: " << points << "\n";  //****  open data file , result     cout <<"file open"<< endl;     ifstream infile;     infile.open("area.inp", ios::in);       cout <<"reading data file"<< endl;      (j=0; j<points; j++)         {             cout << std::setprecision(3) << std::fixed;;              infile >> x[j] >> y[j];              cout << "value of x["<<j<<"]: " << x[j] << endl;              cout << "value of y["<<j<<"]: " << y[j] << endl;         }      cout <<"showing numbers stored in array"<< endl;               (j=0; j<points; j++)             {                  cout << "value of x["<<j<<"]: " << x[j] << endl;                  cout << "value of y["<<j<<"]: " << y[j] << endl;             }      infile.close();      return 0; }  number of data in file: 5 file open reading data file value of x[0]: 1.000 value of y[0]: 3.000 value of x[1]: 2.000 value of y[1]: 4.000 value of x[2]: 6.000 value of y[2]: 5.000 value of x[3]: 4.000 value of y[3]: 2.000 value of x[4]: 2.000 value of y[4]: 2.000 showing numbers stored in array value of x[0]: 5.000 value of y[0]: 3.000 value of x[1]: 2.000 value of y[1]: 4.000 value of x[2]: 4.000 value of y[2]: 5.000 value of x[3]: 4.000 value of y[3]: 2.000 value of x[4]: 2.000 value of y[4]: 2.000   ------------------ (program exited code: 0) press return continue 

as mentioned in comments, creating 0-length array. things way, have allocate arrays new after you've counted number of points in file:

double *x = new double[points]; double *y = new double[points]; 

and @ end of program, free memory delete.

i recommend avoid arrays , instead use std::vector. in example, use std::pair store points, can change that.

// using vector of pairs std::vector<std::pair<double, double>> data;  ifstream infile; infile.open("area.inp"); // should check here make sure file opened ok  // read file. no need know size ahead of time std::pair<double, double> tmp; while (infile >> tmp.first >> tmp.second) {     data.push_back(tmp); }  // print results std::cout << "number of pairs: " << data.size() << std::endl; (auto p : data) {     std::cout << p.first << ", " << p.second << std::endl; } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -