stl string and allegro text functions
Rick

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

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

It compiles but it blows up on m_text.insert(index, str); for some reason.

LennyLen

Hmmm, maybe you might want to cout << str; to check that the way I created it actually was valid.

Rick

It works in I put something in m_text first. If m_text doesn't have anything then it gives the error.

Hard Rock

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

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

remember, strings are immutable. If you want to update strings, you should really use the stringbuffer

Hano Wair

If I understood everything correctly, this should do it:

if(keypressed())
{
  k = readkey();
  m_text += (k & 0xff);
}

Rick
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
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

Dustin, that doesn't work unless I put something in m_text first like m_text="Test"; Why is that?

ImLeftFooted

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

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.

1class Textbox
2{
3private:
4 string m_text;
5 int index;
6public:
7 Textbox()
8 {
9 index = 0;
10 }
11 string getText();
12 void poll();
13 void draw(BITMAP* buffer);
14};
15 
16void 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. :)

X-G

Quote:

remember, strings are immutable. If you want to update strings, you should really use the stringbuffer

Wrong language.

Rick
Quote:

Wrong language.

That explains it. Maybe I'll just use good old C with the details here.

X-G

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

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
std::string str = "abc";
str += 'd';

That is the standard for appending characters to strings AFAIK...

Rick

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

Because 1 is beyond the end of the empty string?

Rick

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

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

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

  k = readkey();
    string str (1, k & 0xff);

    //append the key to the end of the string
    m_text.insert(index, str);
    index++;

X-G

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.

Rick

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 :)

X-G

Quote:

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.

Rick

Oh well that's not what I expected because my index does start at 0 and that didn't work for me.

ImLeftFooted

Maybe you're misinterpretting the crash.

Neil Walker
Quote:

Wrong language.

This is the problem when you have to code in 5 languages at once per day :)

Rampage

Which compiler can accept five languages at once, being java one of them? ???

ImLeftFooted
Quote:

Which compiler can accept five languages at once, [java being] one of them? ???

gcc

Rampage

At once? I thought it could compile only one language at a time :P.

23yrold3yrold
Quote:

How do I increase the length of the string?

resize(). reserve() is generally worthless.

Neil Walker

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.

piccolo

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 = "";

Michael Faerber

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.

Tobias Dammers
Quote:

+= 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.

Peter Hull
Quote:

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;
}

Quote:

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>).

basic_string info

Pete

ImLeftFooted

std::string does not have a trailing '\0'.

Peter Hull
Quote:

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

ImLeftFooted

Hm, interesting..

Thread #587873. Printed from Allegro.cc