For loop not updating the numeric array?!

This is one of the weirdest things I've ever seen.
1
2
3
4
5
6
7
			 while(myReader->Read()) {
				 for ( int j = 0; j < myReader->FieldCount; j++ ) {
					iASpeed[j] = myReader->GetDouble(j);
					String ^Mesaj = "iASpeed = " + iASpeed[j];
					MessageBox::Show(Mesaj);
				 }
			 }

For this code, I get a messagebox for every value of iASpeed without any problems, everything works as intended.
If I change the j in a number, let's say:
1
2
3
4
5
6
7
			 while(myReader->Read()) {
				 for ( int j = 0; j < myReader->FieldCount; j++ ) {
					iASpeed[j] = myReader->GetDouble(j);
					String ^Mesaj = "iASpeed = " + iASpeed[2];
					MessageBox::Show(Mesaj);
				 }
			 }

I get a messagebox everytime, as intended, but it only displays "iASpeed = 0".
Other calculations as well, when it comes to the numeric value, they display a 0. It's like there is an array for iASpeed[j] and another one for iASpeed[2]
Any idea why this may be happening?
Using C++/CLi (Windows Form Application)
Any help is apreciated!
Thank you in advance!
Last edited on
Salut!
Well, I don't know how is iASpeed initialized (I suppose you give it the length of FieldCount before starting the loop) but I believe you ask for iASpeed[2] before a value have been assigned to it.

I mean you are giving a value to iASpeed[j] as you go from j=0 to j=myReader->FieldCount-1. You can ask for iASPeed[2] only after j is greater than 2.

Also, check whether myReader->GetDouble(j) returns the expected values.

Try this:
1
2
3
4
5
6
7
8
9
while(myReader->Read()) {
     for ( int j = 0; j < myReader->FieldCount; j++ ) {
           iASpeed[j] = myReader->GetDouble(j);
           if (j>1) {
                String ^Mesaj = "iASpeed = " + iASpeed[2];
                MessageBox::Show(Mesaj);
           }
    }
}
Last edited on
Salut! ^^
Well, this means that the problem should be solved whenever j passes over the 2nd value, but I get 20 messages (in this case) with iASpeed = 0, no matter which is the value of j here.
Also, I tried using a messagebox out of the While loop, still the same problem, it seems that the value isn't assigned to the variable
Perhaps myReader->GetDouble(j) does return 0
Try this
1
2
3
4
5
6
7
while(myReader->Read()) {
     for ( int j = 0; j < myReader->FieldCount; j++ ) {
           iASpeed[j] = 2;
           String ^Mesaj = "iASpeed = " + iASpeed[2];
           MessageBox::Show(Mesaj);
    }
}


See if you get "iAspeed = 2". If so, myReader->GetDouble(j) is wrong.
What type of variable is iASpeed?
I have Visual Studio 2010, if you upload your project, or this part of it, somewhere I could take a look at it.
Last edited on
Well, the GetDouble can't return 0, as I said, it works just perfect when I let the j where it is ^^

1
2
3
4
5
6
7
while(myReader->Read()) {
				 for ( int j = 0; j < myReader->FieldCount; j++ ) {
					iASpeed[j] = 2;
					 //iASpeed[j] = myReader->GetDouble(j);
					String ^Mesaj = "iASpeed = " + iASpeed[2];
					MessageBox::Show(Mesaj);
				 }

This is the same, every messagebox I get says "iASpeed = 0" ^^

This is how I defined iASpeed
 
static array <double>^ iASpeed = gcnew array<double>(92);


However, this way, it tells me the exact valor:
1
2
3
4
5
6
7
8
			 while(myReader->Read()) {
				 for ( int j = 0; j < myReader->FieldCount; j++ ) {
					iASpeed[2] = 2;
					 //iASpeed[j] = myReader->GetDouble(j);
					String ^Mesaj = "iASpeed = " + iASpeed[2];
					MessageBox::Show(Mesaj);
				 }
			 }


Also, after some testing, I found out that it basically tries to write all the values in 0...

1
2
3
4
5
6
7
			 while(myReader->Read()) {
				 for ( int j = 0; j < myReader->FieldCount; j++ ) {
					iASpeed[j] = myReader->GetDouble(0);
					String ^Mesaj = "iASpeed = " + iASpeed[0];
					MessageBox::Show(Mesaj);
				 }
			 }

It seems that the FOR loop isn't working properly, even though it rolls 20 times, the j seems to stay the same, as it keeps updating the value in iASpeed[0]

Again, this does the same thing, it stores all values in iASpeed[1]
1
2
3
4
5
6
7
8
9
			 while(myReader->Read()) {
				 //for ( int j = 0; j < myReader->FieldCount; j++ ) {
					int j = 0;
					iASpeed[j+1] = myReader->GetDouble(0);
					String ^Mesaj = "iASpeed = " + iASpeed[1];
					MessageBox::Show(Mesaj);
					j++;
				 //}
			 }
Last edited on
Try this
1
2
3
4
5
6
7
int j = 0;
while(myReader->Read()) {
     iASpeed[j+1] = myReader->GetDouble(0);
     String ^Mesaj = "iASpeed = " + iASpeed[1];
     MessageBox::Show(Mesaj);
     j++;
}


It seems like for each while loop you are redefining the j variable
Last edited on
God you are so right...That was so stupid of me! Rookie mistake :DDD
That actually fixed it, I think that the For loop wasn't working for the same reason, it kept redifining the int j = 0;
Thank you!
The definition and redefinition of your j variable in a for loop is taken the first time when you enter the for loop so it should not be the reason for your program not working the proper way instead since we don't know your variables i would say for you to look the mistake in the for loop but not in the j variable but in the myReader->FieldCount what kind of variable is this one and i'm allmost 100 % sure that this one gives you the troubles. Check if myReader->FieldCount is an int or is a container or array so you are missing the size of it as int. I reply cos it's always better to rethink your first solution and find the answer it might be useful for some other problems in future.
All the best (I ment no harm :D) !!!
The thing is that even if I used ´20´instead of that FieldCount, I still had the same problem...
Topic archived. No new replies allowed.