Friday, March 15, 2013

Escape char that Percentage

One of the unique aspects of programming is that it can sometimes need codes. These codes have to work in some specific ways and it makes things a little more complicated or unusual in its appearance.

My example for this is the Escape chars used in string formatting in C/C++ and also many other languages. These codes are necessary to complete a variety of jobs. Although I have been using these special characters and string functions now for about 18 years I can sometimes forget exactly what I need, though I admit it is rare.

Tonight I came across one that I believe I have never had cause to use. Let me explain the situation.

In the new Menu I just coded I enter a game code, this will allow a player to enter some special codes supplied by people on Twitter or Facebook, and entering a valid code would empower and reward the player in some way. One of the rewards happens to be +20% Energy restored Immediately. So I have a table of strings for the rewards so I can explain the valid code entered has given the player 'X'.

Well the string it printed on my screen was quite amazing. The '+20% Energy' became '+20 2.20000E+22 nergy'. Boy did it make a mess. Now my experience told me it was a simple escape char problem, however I have never encountered the % used with the normal escape char '/'. So I was initially puzzled.

Upon investigation I found that I needed to do this '%%' to escape the percentage symbol with itself. Wonderful.. and the result is good too.

Even after all these years, somethings I take for granted can still produce surprises.

Good Night,
Da Voodoochief

No comments:

Post a Comment