Hi folks,
Thanks to some guys replying to my post this morning and I'm able to finish the codes. However, I don't know why the codes don't work. My goal is pretty humble: read an ASCII data file like the following:
Code:
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
....
into a vector object defined by me for quantitative modelling purposes.
The main function is:
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <vector>
#include "head.h"
using namespace std;
int main()
{
//define the vector corresponding to the model I
vector<struct observation_model_1> observations;
//read the data into this vector by calling the function
//readdata_model_1
readdata_model_1("datafile", observations);
checkdata_model_1(observations);
system("PAUSE");
return EXIT_SUCCESS;
}
The "head.h" includes the function prototypes:
Code:
struct observation_model_1 {
static const int dimofX=8;
//Notice that the dataset has 9 numbers for any row
double X[dimofX];
//This will take the 2th-9th numbers of one row
double Y;
//This will take the first number of one row
};
void readdata_model_1(char *filename, vector<struct observation_model_1> &observations);
//read the data into a vector with its pointer passed on
void checkdata_model_1(vector<struct observation_model_1> &observations);
And these two functions are defined in "function.cpp"
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include "head.h"
void readdata_model_1(char *filename, vector<struct observation_model_1> &observations) {
struct observation_model_1 temp;
ifstream readfile(filename);
if (! readfile) {
cout<<"File ("<<filename;
cout<<") could not be opened."<<endl;
exit(0);
}
while(! readfile.eof()){
readfile>>temp.Y;
//read in the first number of one row in the data file;
for (int i=0; i++; i<temp.dimofX) {
readfile>>temp.X[i];
}
//read in the 2th-9th numbers of one row in the datafile;
observations.push_back(temp);
//push into the vector observations defined in the main file;
}
readfile.close();
}
/*****This file is to check the correctness in data reading*******************/
/*****************************************************************************/
//It prints out the first, third and last row of the data readed on
//on the screen for checking
void checkdata_model_1(vector<struct observation_model_1> &observations) {
cout<<"The number of observations in the dataset is: "<<observations.size()<<endl;
cout<<endl;
cout<<"The 1st row is: "<<endl;
cout<<observations[0].Y<<endl;
for (int i=0;i++;i<observations[0].dimofX) {
cout<<observations[0].X[i]<<endl;
}
cout<<endl;
cout<<"The 3rd row is: "<<endl;
cout<<observations[2].Y<<endl;
for (int i=0;i++;i<observations[2].dimofX) {
cout<<observations[2].X[i]<<endl;
}
cout<<endl;
cout<<"The last row is: "<<endl;
cout<<observations[observations.size()-1].Y<<endl;
for (int i=0;i++;i<observations[observations.size()-1].dimofX) {
cout<<observations[observations.size()-1].X[i]<<endl;
}
cout<<endl;
}
Unfortunately when I checked the data, X[.] can never be printed out. What is printed on the screen is that:
Code:
The 1st row is:
1
The 3nd row is:
3
The last row is:
1000
Press any key to continue...
Any thoughts about this? I will appreciate it as I'm in the midst of a computing project as you can tell. Thanks a lot in advance...