an array outputting hex when i need dec

Apr 24, 2012 at 2:24am
Ok: first post ever

I made a multidimensional array with 2 indices and it worked great. Data comming out exactly as expected.

Then I took the same array and added a third indices, however when I run the program all my values come out just fine with the exception that they are in hexadecimal.

I'm not at home on the machine with the problem on it. But if needed I can put up a cut/paste as soon as I get home.

All I want to know is why is it in hexadecimal and how can I force into decimal.


Thanks for the help ahead of time, I spent two days in books and on google etc looking this up and finally decided it was time for help.
Apr 24, 2012 at 3:41am
It would be very hard to guess at an answer, so go ahead.. Cut 'n Paste please.
Apr 24, 2012 at 10:19pm
code for first is as follows:

#include <iostream>

using namespace std;

int main()
{
int arr[3] [3] = {{1,2,3} , {4,5,14}} ;
cout<<"arr[0][0] contains"<<arr[0][0]<<endl;
cout<<"arr[0][1] contains"<<arr[0][1]<<endl;
cout<<"arr[0][2] contains"<<arr[0][2]<<endl;

cout<<"arr[1][0] contains"<<arr[1][0]<<endl;
cout<<"arr[1][1] contains"<<arr[1][1]<<endl;
cout<<"arr[1][2] contains"<<arr[1][2]<<endl;

above this line is code for first problem which compiles and gives me whats in the array in decimals

below this line is same thing with an added (indices i think its called)
whatever is in the array comes out hexadecimal.


#include <iostream>

using namespace std;

int main()
{
int arr[3] [3] [3]= {{1,2,3} , {4,5,14} , {15,16,17}};
cout<<"arr[0][0] contains"<<arr[0][0]<<endl;
cout<<"arr[0][1] contains"<<arr[0][1]<<endl;
cout<<"arr[0][2] contains"<<arr[0][2]<<endl;

cout<<"arr[1][0] contains"<<arr[1][0]<<endl;
cout<<"arr[1][1] contains"<<arr[1][1]<<endl;
cout<<"arr[1][2] contains"<<arr[1][2]<<endl;

cout<<"arr[2][0] contains"<<arr[2][0]<<endl;
cout<<"arr[2][1] contains"<<arr[2][1]<<endl;
cout<<"arr[2][2] contains"<<arr[2][2]<<endl;


return 5;
}
Apr 24, 2012 at 10:23pm
With a 3 dimensional array, you need 3 brackets to access the values properly, otherwise the compiler thinks your accessing a pointer, explaining the hex.

 
arr[x][y]


should be:
 
arr[x][y][z]
Apr 24, 2012 at 11:52pm
int arr[3] [3] [3]= {{1,2,3} , {4,5,14} , {15,16,17}};
is [x][y][z] isnt it?
forgive me if I'm not gettin it, its not making sence or giving me decimal answers that way.
Your explanation I get about it thinking its accessing a pointer but with
[3][3][3] that seems to be what your saying. (i compiled it again just to make sure.) and it still gives hex.
Last edited on Apr 24, 2012 at 11:53pm
Apr 24, 2012 at 11:57pm
No, it thinks its a pointer when you access a 3d array using only 2 sets of brackets, which is what your doing in your cout statements. The reason its outputting actual numbers instead of addresses is because of how arrays work under the hood.
Apr 25, 2012 at 12:13am
thanks I think Ill table that till I get a little more experience in arrays.......turns out what I was trying to do is very rare.

One last question can I force a decimal out out in some manner?
Apr 25, 2012 at 1:27am
Yes you can, using manipulators. http://www.cplusplus.com/reference/iostream/manipulators/dec/ is the one you want.
Topic archived. No new replies allowed.