how to convert a short array to a string??

Started by
7 comments, last by Sneftel 16 years, 2 months ago
i decided to try a general method of converting a short array to a string. here is my code.


short myArray = new short[10];

for(int i = 0; i < 10; i++)
  myArray = i *10;

ostringstream myString;

myString << myArray;

cout << "myArray: ";
for(int i = 0; i < 10; i++)
  cout << myArray << " ";

cout << endl;

cout << "myString: " << myString.str() << endl;


output:
Quote: myArray: 0 10 20 30 40 50 60 70 80 90 myString: 0x8050000
obviously i used the ostringstream stuff wrong. what did i do wrong? thanks!
heh
Advertisement
Tell me: Why did you output myArray into myStream one way, and output it into cout another way?
Quote:Original post by Sneftel
Tell me: Why did you output myArray into myStream one way, and output it into cout another way?



i dont understand what you are saying.

i was using the << operator on the stream to convert it to a string like you can do with other types.

what did you mean?

heh
Sneftel asks because ostringstream behaves just like cout (only to a string, not standard output) and you managed to get it right with cout, so why did you get it confused with ostringstream?

I think you misunderstand C/C++ arrays:
C++ arrays are very minimal - they are just a pointer to the first element. When you send an array to a function you are actually passing a pointer to the first element. The function has no way to know how large the array is without extra info.

So when you passed the array to ostringstream it does what you would expect it to do with a pointer - it translates the address to a printable string.

If you want to print the array you need to feed ostringstream one array element at a time, like you did with cout.

You can wrap the array with a class ("MyArray") and then make a function operator<<(stream&, const MyArray&) which would have the loop, and then use that function to print your arrays.
Quote:Original post by OpenGL_Guru
Quote:Original post by Sneftel
Tell me: Why did you output myArray into myStream one way, and output it into cout another way?



i dont understand what you are saying.

i was using the << operator on the stream to convert it to a string like you can do with other types.

what did you mean?


You have two streams: myString and cout. You tried to output myArray to both of these streams. You did it two different ways. Which way worked? Which way didn't?
A stringstream behaves just like a console stream, which is what std::cout is. Let's obscure the variable names, and see if you can now recognize the problem:

foo << myArray;for(int i = 0; i < 10; i++)  bar << myArray << " ";// Why do we see different things in the two streams?
well i think i got what i wanted now. not the prettiest thing in the world.

string str;stringstream out;short myArray = new short[10];for(int i = 0; i < 10; i++){ myArray = i * 10; out << myArray; str = out.str(); out.clear();}cout << "myArray: ";for(int i = 0; i < 10; i++) cout << myArray << " ";cout << endl << "string array: " << str << endl;



output:

Quote:

myArray: 0 10 20 30 40 50 60 70 80 90
string array: 0102030405060708090



looks like that worked fine. thanks all for the help. i guess i really didnt need the ostringstream stuff. i just needed to get the shorts into a string format, i guess in essence i was making it too hard.
heh
Quote:Original post by OpenGL_Guru
well i think i got what i wanted now. not the prettiest thing in the world. ... i guess i really didnt need the ostringstream stuff.

OMG... here's a less subtle clue. Replace "cout" with "myString" in your first example.

EDIT: At this point you have something that works for you, so I'll just show you what people were trying to explain this whole time. You had it working with cout... to get it to work with the stringstream you just had to do EXACTLY what you did with cout.
ostringstream myString;for(int i = 0; i < 10; i++)  mystring << myArray << " ";
OpenGL_Guru, you make me sad.

This topic is closed to new replies.

Advertisement