Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » string::resize and string::max_size

This thread is locked; no one can reply to it. rss feed Print
 1   2 
string::resize and string::max_size
julian_boolean
Member #8,201
January 2007

While I'm still on sorta the same subject, the last thing I wanted to do was make a function that can censor input by just replacing it with some kinda symbol like "*" or "#".

1 
2 if(ASCII >= 32 && ASCII <= 126 && edittext.length() < max_length)
3 {
4 if(insert || iter == edittext.end())
5 {
6 if(!censored)
7 {
8 iter = edittext.insert(iter, ASCII);
9 }
10 
11 else
12 iter = edittext.something..
13 }
14
15 else
16 edittext.replace(caret, 1, 1, ASCII);
17 caret++;
18 iter++;
19 }

For that I'm guessing I should use replace?

Matthew Dalrymple
Member #7,922
October 2006
avatar

Yes you would need to use replace, unless you are using this for a password and you are wanting *'s to show up instead of the actually text. Is that what you mean?

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

Unless? It will be used for passwords, is there something else already made for that?

Matthew Dalrymple
Member #7,922
October 2006
avatar

There is a different method. Where you are actually drawing the text just a number of *'s related to the size of the text they have entered. Keeping it synchronized with the actually amount in it of course (factoring in backspace and what not).

EDIT: If you use replace you will actually be overwriting what is in the string. I thought you might be using this to replace cuss words and such with !@#$%^&*'s.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

What is this method? Overwriting the input as * works too doesn't it?

Well for filtering certain words, I figure that would have to be a seperate function on it's own.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Quote:

What is this method? Overwriting the input as * works too doesn't it?

You can go with this method, but when you get to the point of trying to do something with the password they entered in all you are going to see is "*********" The star's are meant for people around you not to see your password. If you overwrite their password with the actual stars then their password then becomes stars. Everyone's password will be almost the same, just varying lengths of stars.

What you want to accomplish is keeping their password in the string, then when it comes time to draw their password to the screen you just draw the number of stars that is equivalent to the number of characters in their password.

Type in: zomgtheholyinterents
They see: ********************

If I'm correct with your method their password will be "********************" instead of "zomgtheholyinterents"

Now you can keep your method as long as you have another string to store their actual password and one string to store whats actually being drawn to the screen. My method reduces the number of strings you need.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

I never actually thought of that.. Damn. Well I guess it doesn't really matter if I use an extra string to do that, it's easier for me anyway since I don't know any other way to go about doing it.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Yeah I thought I'd stop you before you ran into that run-time. If you show me a clip of your drawing method I can help you draw it without using another string, but there is nothing wrong in using another string if that's what works for you.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

Here's what my rough guess at what I think it would look like:

1if(ASCII >= 32 && ASCII <= 126 && edittext.length() || editcensor.length() < max_length)
2{
3 if(insert || iter == edittext.end())
4 {
5 if(!censored)
6 {
7 iter = edittext.insert(iter, ASCII);
8 }
9 
10 else
11 iter = editcensor.something..
12 }
13 
14
15 else
16 edittext.replace(caret, 1, 1, ASCII);
17 caret++;
18 iter++;
19}
20 
21if(!censored)
22 textout_ex(dbuffer, font, edittext.c_str(), textx, texty, WHITE, -1);
23 
24else
25 textout_ex(dbuffer, font, edittcensor.c_str(), textx, texty, WHITE, -1);

Edit:

I noticed that if that even worked, it would be only doing the input for editcensor..

Matthew Dalrymple
Member #7,922
October 2006
avatar

Just remove the replaces and do this...:

if(!censored)
  textout_ex(dbuffer, font, edittext.c_str(), textx, texty, WHITE, -1);
else
{
  string censor;
  for(int i = 0; i < edittext.length(); i++)
    censor += "*";
  textout_ex(dbuffer, font, censor.c_str(), textx, texty, WHITE, -1);
}

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

That seems to work, sorta. When I input the text it will show up as *'s, but every keystroke seems to double the amount. In other words, if I type in "test" the "t" would show up as 1 *, the "e" would show up as 2 *'s, etc.

Maybe saying "censor = edittext.length();" or something like that might work.. Trying that now. :P

I was fooling around with it again and now a single keystroke makes it automatically print out an ass load of *'s.. I'm sure the problem is very simple, still picking at it..

Matthew Dalrymple
Member #7,922
October 2006
avatar

Oh I'm sorry, here is the fix:

if(!censored)
  textout_ex(dbuffer, font, edittext.c_str(), textx, texty, WHITE, -1);
else
{
  string censor;
  for(int i = 0; i < edittext.length(); i++)
    censor += "*";
  textout_ex(dbuffer, font, censor.c_str(), textx, texty, WHITE, -1);
  /*    / THIS IS THE FIX /    */
  //censor.erase();
  // EDIT:
  censor.clear();
}

The problem was that censor was not erasing, so it would keep re stacking the edittext onto itself.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

You are not a man but a god! Hehe that works perfectly, one question though:

  textout_ex(dbuffer, font, censor.c_str(), textx, texty, WHITE, -1);
  censor.erase();
  censor.clear();

What exactly is the difference between erase and clear?

Matthew Dalrymple
Member #7,922
October 2006
avatar

I had to go double check myself after I posted on http://www.cppreference.com
erase takes parameters, so you can erase a specific field while clear() will erase the whole string.

Quote:

You are not a man but a god!

Nah, it's just problems that I've ran into before so I know exactly what was going on. I mean.. yes I am a god.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

Hehe! I just wanted to thank everyone for helping out me so much, especially Matthew. You've all been great, thanks again! :)

Matthew Dalrymple
Member #7,922
October 2006
avatar

FYI: This forum has a new feature that when the creator of the topic posts you can check a box that says something around the lines of "My question was answered fully" then you give props so people know your question has been answered. But a last post like yours is good too :D

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

But a last post like yours is good too :D

No. I demand cookies.

PS: That checkbox doesn't appear unless you create the thread with one in the first place.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

julian_boolean
Member #8,201
January 2007

For future reference, how can I do this (is it the thing that says "Classification"?) Thanks. ;)

Tobias Dammers
Member #2,604
August 2002
avatar

if(!censored)
  textout_ex(dbuffer, font, edittext.c_str(), textx, texty, WHITE, -1);
else
{
  string censor('*', edittext.length()); // initialize to a given number of asterisks
  textout_ex(dbuffer, font, censor.c_str(), textx, texty, WHITE, -1);
// no need to clear: censor will clear itself when it goes out of scope, since it's non-static
}

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Matthew Dalrymple
Member #7,922
October 2006
avatar

Quote:

For future reference, how can I do this (is it the thing that says "Classification"?) Thanks. ;)

Yeah, normally choose "A question seeking a specific answer."

To Tobias:
With my example that's what should be happening but I think he might have made it a member variable, so clearing the string was appropriate. But I do like your method.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

 1   2 


Go to: