character spacing smaug/swr

Posted by Jason on Thu 08 Feb 2007 06:41 PM — 17 posts, 63,299 views.

#0
ok here is my question.

On swr while coding you can put in something like this:

    ch_printf(ch, "|Name: %5s|&\n\r", ch->name,

where %5s makes a spacing of 5 characters. now lets say a persons name is bobbie. it will add the 6th character space automaticly and throw off my ANSI picture i was trying to create... is there a way to cut off the input early so that it doesn't add the extra characters so it looks like this:


+-----------+
|Name: bobbi|
+-----------+
instead of this:
+-----------+
|Name: bobbie|
+-----------+
Amended on Thu 08 Feb 2007 06:51 PM by Jason
USA #1

char buf[6];
ch_printf(ch, "|Name: ");
snprintf(buf, 6, "%5s", ch->name);
ch_printf(ch, "%s|&\n\r", buf);


You can (and of course should) reuse the 'buf' variable for every player.

As an editorial comment, only 5 characters for player names is a little short. I'd go for something like 8 or 9.
#2
actual spacing is 20.... put i need the info for other spots in my code... this was just a for instance
#3
and i need to know if there is away to do it in one line
cause finished progect looks like this:


+--------+-----------+---------------+---------------------------+---------------------------------+
|AGE: 229|ALIGN:-920 |ARMOR:-392     |NAME:           Koumintang |CREDITS:536690                   |
+--------+-----+-----+--------+------+----------+----------------+---------------------------------+
#4
like with age.... if it gets over 999 and moves to 1000 then it will move the whole rest the line over one
USA #5
Just do what I did, but multiple times. Don't print out the newline until you want to.

The point of the snprintf function is to print only so-many characters into the buffer (the number includes the \0, so 6 really means 5 characters).

See man snprintf for more information.
#6
k i'll give it a shot
#7
now the different spaces needs different maxes will that matter?
USA #8
Of course, you'll need to use different sizes for snprintf.

If I may make a suggestion you should slow down a little and understand why what I posted does what you want. The questions you're asking indicate to me that you don't really know what the code I posted does, because your questions have very easy answers. Sometimes, taking the time to slow down will let you go much faster in the future. :-)
Australia Forum Administrator #9
Read up on the printf function. You can use "*" in the string to indicate that the width is a variable amount - that is, you supply the width itself as an argument.
Australia Forum Administrator #10
Quote:

if it gets over 999 and moves to 1000 then it will move the whole rest the line over one


Do you really want to truncate the age? If you do, and someone has age 1020, then it will show as 20, is that really what you want? Isn't that misleading?
USA #11
I think he wants to truncate the length of the string to e.g. 3, not the actual number. So in his case, 102 would appear as 102 -- which still isn't what he wants (well, maybe it is).

Jason, if you want to truncate numbers you will have to determine what the max is, and if it's above the max just print the max (e.g. 999) and otherwise print the normal value.
#12
ok lets try this again

+--------+-----------+---------------+---------------------------+---------------------------------+
|AGE: 229|ALIGN:-920 |ARMOR:-392     |NAME:           Koumintang |CREDITS:536690                   |
+--------+-----+-----+--------+------+----------+----------------+---------------------------------+

ignore age all together
we will use the name field
it is 20 characters long
if a person's name is longer than 20 characters
it will strech out that spacing... instead of it streching it out i want it to just cut it off
example

this is what it does now
+--------+-----------+---------------+---------------------------+---------------------------------+
|AGE: 229|ALIGN:-920 |ARMOR:-392     |NAME: thisislongerthan20characters |CREDITS:536690                   |
+--------+-----+-----+--------+------+----------+----------------+---------------------------------+
this is what i want it to do
+--------+-----------+---------------+---------------------------+---------------------------------+
|AGE: 229|ALIGN:-920 |ARMOR:-392     |NAME: thisislongerthan20ch |CREDITS:536690                   |
+--------+-----+-----+--------+------+----------+----------------+---------------------------------+

i want it to just cut it off and not move everything after it
Amended on Thu 08 Feb 2007 07:56 PM by Jason
USA #13
I already told you how to do that. I'm not sure why you are repeating the question; did the answer not work?
#14
Ok the reason i reposted was so that nick knew what i was talking about... cause in his post he was still wondering. and the reasoni was asking the questions was so i could understand it better.. And thanx for the help... i did get it to work after a little fiddling with it
works perfectly thanx again
USA #15
i know this is probably late, but why not just limit character names to 20 characters from creation? or if that is no longer a viable option, write a code that will expand as needed.

length = UMAX( strlen(ch->name), 20 );

ch_printf( "%*s", length, ch->name );
Amended on Sun 11 Feb 2007 11:23 AM by Gohan_TheDragonball
#16
LOL ok.. well the problem didn't lay with how long the names are.... i was just using it as an example LOL... for some of the fields they was to short so i was just using that as an example lol but thanx though