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?
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); }
It compiles but it blows up on m_text.insert(index, str); for some reason.
Hmmm, maybe you might want to cout << str; to check that the way I created it actually was valid.
It works in I put something in m_text first. If m_text doesn't have anything then it gives the error.
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))
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.
remember, strings are immutable. If you want to update strings, you should really use the stringbuffer
If I understood everything correctly, this should do it:
if(keypressed()) { k = readkey(); m_text += (k & 0xff); }
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++.
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.
Dustin, that doesn't work unless I put something in m_text first like m_text="Test"; Why is that?
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?
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.
| 1 | class Textbox |
| 2 | { |
| 3 | private: |
| 4 | string m_text; |
| 5 | int index; |
| 6 | public: |
| 7 | Textbox() |
| 8 | { |
| 9 | index = 0; |
| 10 | } |
| 11 | string getText(); |
| 12 | void poll(); |
| 13 | void draw(BITMAP* buffer); |
| 14 | }; |
| 15 | |
| 16 | void Textbox::poll() |
| 17 | { |
| 18 | int k; |
| 19 | |
| 20 | if(keypressed()) |
| 21 | { |
| 22 | k = readkey(); |
| 23 | string str (1, k & 0xff); |
| 24 | |
| 25 | //append the key to the end of the string |
| 26 | m_text.insert(index, str); |
| 27 | index++; |
| 28 | } |
| 29 | } |
Because I don't know much about the string class.
remember, strings are immutable. If you want to update strings, you should really use the stringbuffer
Wrong language.
Wrong language.
That explains it. Maybe I'll just use good old C with the details here.
This is (basically) what I do. Works like a charm.
void UIInputBox::handleCharPress(int k) { text.insert(cursor, string(1, (char)k)); cursor++; }
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
std::string str = "abc"; str += 'd';
That is the standard for appending characters to strings AFAIK...
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
Because 1 is beyond the end of the empty string?
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.
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?
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
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.
OK, so no, your example doesn't work. How do I increase the length of the string?
[EDIT}
+= should work I think 
or push_back(), or maybe append()
I think that is what you were trying to get me to find
How do I increase the length of the string?
Put something on the end. Or insert at index 0, which is just at the end of the string, and not 1 index into oblivion.
Oh well that's not what I expected because my index does start at 0 and that didn't work for me.
Maybe you're misinterpretting the crash.
Wrong language.
This is the problem when you have to code in 5 languages at once per day
Which compiler can accept five languages at once, being java one of them?
Which compiler can accept five languages at once, [java being] one of them?
gcc
At once? I thought it could compile only one language at a time
.
How do I increase the length of the string?
resize(). reserve() is generally worthless.
Of course, you know I meant separately. But the .NET compiler supports about 20 or so languages at present and you can mix your code if you wish.
some times to get strings to work with allegro you need to convert then to a string of chars using the .c-str() example: stringName.c-str()
also when you add 2 string the have to be blank or have something in them.
example of a blank string : stringName = "";
I had problems with Allegro and std::string before, and due to this I always called set_uformat(U_ASCII); once to have Allegro play well with std::string.
+= should work I think 
or push_back(), or maybe append()
+= or +. Don't use push_back(), string inherits it from vector; this will append data after the trailing 0, which is not what you want.
And remember to cast to char so that operator += does what you want (unless you use wide characters, in which case you should use short IIRC).
string s; /* ... */ char c = readkey() & 0xFF; if (c >= 32) s += c;
That's it. If it crashes, something else is wrong.
Don't use push_back(), string inherits it from vector; this will append data after the trailing 0,
This is wrong (try it!)
#include <string> #include <iostream> using namespace std; int main() { string s; s.push_back('X'); s.push_back('-'); s.push_back('G'); cout << s << endl; return 0; }
in which case you should use short IIRC).
Use wchar_t (string is a typedef for basic_string<char>, wstring is a typedef for basic_string<wchar_t>).
Pete
std::string does not have a trailing '\0'.
std::string does not have a trailing '\0'.
I thought that too, but I checked in the include file <string> (look at note 3, below).
| 1 | // ------------------------------------------------------------ |
| 2 | // Class basic_string. |
| 3 | |
| 4 | // Class invariants: |
| 5 | // (1) [start, finish) is a valid range. |
| 6 | // (2) Each iterator in [start, finish) points to a valid object |
| 7 | // of type value_type. |
| 8 | // (3) *finish is a valid object of type value_type; in particular, |
| 9 | // it is value_type(). |
| 10 | // (4) [finish + 1, end_of_storage) is a valid range. |
| 11 | // (5) Each iterator in [finish + 1, end_of_storage) points to |
| 12 | // unininitialized memory. |
| 13 | |
| 14 | // Note one important consequence: a string of length n must manage |
| 15 | // a block of memory whose size is at least n + 1. |
The relevant part of the standard is 21.3.4.
Pete
Hm, interesting..