|
This thread is locked; no one can reply to it. |
1
2
|
stl string and allegro text functions |
Rick
Member #3,572
June 2003
|
When the user presses a key, how could I append these to a stl string variable. I've tried: if(keypressed()) { k = readkey(); //append the key to the end of the string m_text.insert(index, k & 0xff); } But it doesn't seem to work. Any ideas? ======================================================== |
LennyLen
Member #5,313
December 2004
|
This is really a guess, since I don't use C++, but... if(keypressed()) { k = readkey(); string str (1, k & 0xff); //append the key to the end of the string m_text.insert(index, str); }
|
Rick
Member #3,572
June 2003
|
It compiles but it blows up on m_text.insert(index, str); for some reason. ======================================================== |
LennyLen
Member #5,313
December 2004
|
Hmmm, maybe you might want to cout << str; to check that the way I created it actually was valid.
|
Rick
Member #3,572
June 2003
|
It works in I put something in m_text first. If m_text doesn't have anything then it gives the error. ======================================================== |
Hard Rock
Member #1,547
September 2001
|
This may be an obvious question, but what is the value of index? (I've been using C#/Java strings/Strings for to long so I've been used to them just working(tm)) _________________________________________________ |
Rick
Member #3,572
June 2003
|
Starts out at 0 and gets incremented as you go. If I put for example: m_text = "Test"; in the constructor everything works fine, but of course I don't want anything to start with. ======================================================== |
Neil Walker
Member #210
April 2000
|
remember, strings are immutable. If you want to update strings, you should really use the stringbuffer Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Hano Wair
Member #5,243
November 2004
|
If I understood everything correctly, this should do it: if(keypressed()) { k = readkey(); m_text += (k & 0xff); }
|
Rick
Member #3,572
June 2003
|
Quote: If you want to update strings, you should really use the stringbuffer Never used them before. Some searching on C++ StringBuffer seems to bring up more java stuff than C++. ======================================================== |
ImLeftFooted
Member #3,935
October 2003
|
if(keypressed()) { k = readkey(); //insert the key at index m_text.insert(index, 1, k & 0xff); } Also do checks to see if index is valid. |
Rick
Member #3,572
June 2003
|
Dustin, that doesn't work unless I put something in m_text first like m_text="Test"; Why is that? ======================================================== |
ImLeftFooted
Member #3,935
October 2003
|
Your index variable would be something invalid. See if this code works if(keypressed()) { k = readkey(); //insert the key at index m_text.insert(0, 1, k & 0xff); } Btw, there is a reason that you're not using the append function right? |
Rick
Member #3,572
June 2003
|
My index value is 0. I step through the code to see this. It still doesn't work unless I put something in m_text on init.
Because I don't know much about the string class. ======================================================== |
X-G
Member #856
December 2000
|
Quote: remember, strings are immutable. If you want to update strings, you should really use the stringbuffer Wrong language. -- |
Rick
Member #3,572
June 2003
|
Quote: Wrong language. That explains it. Maybe I'll just use good old C with the details here. ======================================================== |
X-G
Member #856
December 2000
|
This is (basically) what I do. Works like a charm. void UIInputBox::handleCharPress(int k) { text.insert(cursor, string(1, (char)k)); cursor++; }
-- |
Peter Hull
Member #1,136
March 2001
|
You can use push_back on a string, as it's also a Sequence. string s; s.push_back('X'); s.push_back('-'); s.push_back('G'); cout << s << endl; The STL thing that's most like a StringBuffer is a Rope I think. Pete
|
netcat
Member #6,097
August 2005
|
std::string str = "abc"; str += 'd'; That is the standard for appending characters to strings AFAIK... |
Rick
Member #3,572
June 2003
|
Why would it be that if I don't do anything to the string variable it fails, and if I set the variable to anything first, then it works. ie. string var; var.insert(1, 'd'); doesn't seem to work but string var; var = "Test"; var.insert(1, 'd'); works ======================================================== |
X-G
Member #856
December 2000
|
Because 1 is beyond the end of the empty string? -- |
Rick
Member #3,572
June 2003
|
That's what I was thinking, so I did a reserve() but that didn't seem to work. Not sure what direction to go in now. I guess I thought it would grow automatically. ======================================================== |
X-G
Member #856
December 2000
|
Reserve doesn't matter if you're trying to insert something in a space that's beyond the size of the string. That would leave one character in between what you are trying to insert and the beginning of the string, which should be... what exactly? Does the code I posted work? -- |
Rick
Member #3,572
June 2003
|
I don't see the difference between the code you posted and the code I have. So I would say yes if I assign text to something to begin with and no if I don't. Your code void UIInputBox::handleCharPress(int k) { text.insert(cursor, string(1, (char)k)); cursor++; } My code
======================================================== |
X-G
Member #856
December 2000
|
Read what I said. You're trying to insert text in an index that is beyond the length of the string. That's not supposed to work. -- |
|
1
2
|