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.

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

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...

09-08-2006

MacNilly

what is your definition of "DP y?"

09-08-2006

Salem

> for (int i=0; i++; i<temp.dimofX)
Well this for loop is the wrong way round
i++ is going to be false right from the start,

> while(! readfile.eof())
Read the FAQ on why using feof() to control a loop is bad.

Oh, and your indentation sucks.
I would suggest you turn OFF tabs in your editor, and set it to always indent with just 2 or 4 spaces (your choice). Then at least what you see will be what everyone else sees.

09-08-2006

dwks

Code:

struct observation_model_1 temp;

In C++ you can leave out "class" and "struct" when you're declaring an instance of a class/struct, but you can leave it in too if you like.

Gee; I made such a stupid mistake in the looping! I can't believe it; obviously the cold these days destroy my brain...

Thanks a lot!

09-08-2006

asmileguo

Hi can I turn OFF tabs in Dev-C++? That's the editor I'm using...

Quote:

Originally Posted by Salem

> for (int i=0; i++; i<temp.dimofX)
Well this for loop is the wrong way round
i++ is going to be false right from the start,

> while(! readfile.eof())
Read the FAQ on why using feof() to control a loop is bad.

Oh, and your indentation sucks.
I would suggest you turn OFF tabs in your editor, and set it to always indent with just 2 or 4 spaces (your choice). Then at least what you see will be what everyone else sees.

09-08-2006

asmileguo

BTW, in above codes, I find I can't access dimofX as defined in the structure (even for print out), and got an error message:
[linker error]undefined reference to `observation_model_1::dimofX'.
How to fix it?

09-08-2006

Daved

Static class members must be defined once somewhere in a source file. Add const int observation_model_1::dimofX; to your cpp file.

09-08-2006

maxorator

Salem, he just didn't use tabs at all, so you can't say it's the problem of tabs. Still, using tabs is the cleanest way to write code (that's what you learn when you start with HTML, too).

09-08-2006

Salem

Yeah, you're right, there are no tabs in the post.

Code:

> readfile>>temp.Y;
> //read in the first number of one row in the data file;

It's just crap indentation - nothing more, nothing less.

09-09-2006

jafet

I always use tabs in Dev-C++. What's wrong with tabs? It's when you start mixing tabs with spaces then indentation gets out of hand.

09-09-2006

Salem

> I always use tabs in Dev-C++. What's wrong with tabs?
When you start working in project teams, or downloading source packages from the net you'll soon come across files which are indented using tabs, but with a different tab-stop setting to the one you like. Such files always look like a mess until you guess the correct tab-stop setting.

At one place I worked, it was so bad I worked out a set of editor macros to quickly rotate through all the editor tab-stop settings just so I could look at the code in that file without going crazy.

09-09-2006

anon

In DevC++ go to: "Tools>Editor Options" and uncheck "Smart tabs". This is no good and just produces awful indentation.