how to put floating numbers in arrays

hi all
i write this

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
main ()
{
	float a[2];
	int x;
	for(x=1;x<2;x++)
	cin>>a[x];
	cout<<a[x];
}

but in cout give me strange numbers for example when i give 2.5
will cout 2.8026e-450 :((
please some one guid me
Last edited on
Remember array subscripts start from zero, not one.
for (x=0; x<2; x++)

Line 9 is after the for loop has terminated. At that stage x will have the value 2 and a[2] is the 3rd element, that is it's outside the array.

Use braces { } if you want more than one statement to be controlled by the for loop.
you need to add braces to for loop if it has more than one statement concerning it....

1
2
3
4
5
	for(x=0;x<2;x++)  //as Chervil said above
      {    // see like this
	cin>>a[x];
	cout<<a[x];
         }  // an opening and a cloasing brace 
Last edited on
thanks
Topic archived. No new replies allowed.