Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » stl string and allegro text functions

This thread is locked; no one can reply to it. rss feed Print
 1   2 
stl string and allegro text functions
Rick
Member #3,572
June 2003
avatar

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?

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

LennyLen
Member #5,313
December 2004
avatar

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
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

LennyLen
Member #5,313
December 2004
avatar

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
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Hard Rock
Member #1,547
September 2001
avatar

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

_________________________________________________
Hard Rock
[ Stars Dev Company ][ Twitter ][Global Warming: ARA My TINS 07 Entry][Pong Ultra Website][GifAllegS Ver 1.07]
"Well there's also coolwebsearch but we'll let that be an IE exclusive feature" - arielb on the New Browser Plugins "What's better, HTML or Variables?"

Rick
Member #3,572
June 2003
avatar

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.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Neil Walker
Member #210
April 2000
avatar

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

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Hano Wair
Member #5,243
November 2004
avatar

If I understood everything correctly, this should do it:

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

Rick
Member #3,572
June 2003
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Dustin Dettmer
Member #3,935
October 2003
avatar

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
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Dustin Dettmer
Member #3,935
October 2003
avatar

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
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

X-G
Member #856
December 2000
avatar

Quote:

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

Wrong language.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Rick
Member #3,572
June 2003
avatar

Quote:

Wrong language.

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

X-G
Member #856
December 2000
avatar

This is (basically) what I do. Works like a charm.

void UIInputBox::handleCharPress(int k) {
  text.insert(cursor, string(1, (char)k));
  cursor++;
}

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

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
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

X-G
Member #856
December 2000
avatar

Because 1 is beyond the end of the empty string?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Rick
Member #3,572
June 2003
avatar

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.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

X-G
Member #856
December 2000
avatar

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?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Rick
Member #3,572
June 2003
avatar

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

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

 1   2 


Go to: