Doing something stupid here (strings)

Posted by Zeno on Mon 04 Sep 2006 04:13 AM — 5 posts, 12,133 views.

USA #0
I'm doing something really stupid here:
void do_qwho( CHAR_DATA *ch, char *argument )
{
    DESCRIPTOR_DATA *d;
    CHAR_DATA *wch;
    char buf[MAX_STRING_LENGTH];
    char gender_color[MAX_STRING_LENGTH];
    char clan_str[MAX_INPUT_LENGTH];
    char council_str[MAX_INPUT_LENGTH];

    set_char_color( AT_WHO, ch );

    for ( d = first_descriptor; d; d = d->next )
    {
        if ( (d->connected == CON_PLAYING || d->connected == CON_EDITING )
        && ( wch = d->character ) != NULL && !IS_NPC(wch) && can_see( ch, wch ) )
        {
         if (wch != ch && IS_AFFECTED(wch, AFF_CLOAK) && (!ch || get_trust(ch) < LEVEL_GREATER ))
           continue;

        if ( wch->sex == 0 )
          sprintf( gender_color, "&w" );
        if ( wch->sex == 1 )
          sprintf( gender_color, "&B" );
        if ( wch->sex == 2 )
          sprintf( gender_color, "&P" );
        if ( IS_IMMORTAL(wch) )
          sprintf( gender_color, "&Y" );

        if ( wch->pcdata->clan )
        {
          strcpy( clan_str, " (" );
          strcat( clan_str, wch->pcdata->clan->name );
          strcat( clan_str, ")" );
        }

        if ( wch->pcdata->council )
        {
          strcpy( council_str, " [" );
          strcat( council_str, wch->pcdata->council_name );
          strcat( council_str, "]" );
        }

         sprintf(buf, "%s%-7s&D %s%s%s\n\r", gender_color,
          IS_IMMORTAL(wch) ? "Admin" : race_table[wch->race]->race_name,
          wch->name, clan_str, council_str );
         send_to_char( buf, ch );
        }
    }

}

But I can't tell what. This is suppose to be a who list.

The output is displayed totally wrong/corrupt.
Australia Forum Administrator #1

char clan_str[MAX_INPUT_LENGTH];

...

      if ( wch->pcdata->clan )
        {
          strcpy( clan_str, " (" );
          strcat( clan_str, wch->pcdata->clan->name );
          strcat( clan_str, ")" );
        }


If they are not in a clan then clan_str will contain random rubbish. Change it to:


char clan_str[MAX_INPUT_LENGTH] = "";


Same with the other ones.
Amended on Mon 04 Sep 2006 05:09 AM by Nick Gammon
Australia Forum Administrator #2
On an efficiency note, do you really need MAX_INPUT_LENGTH (1024 bytes) to hold the gender colour? Looks like 3 bytes would be enough.
USA #3
Yeah I was actually going to change that gender color size later on before I put it on the main MUD.

Thanks, that was it. Anything else you saw that could use improvement? Besides the poor ifcheck struct I have for gender. =P
Amended on Mon 04 Sep 2006 05:37 AM by Zeno
Australia Forum Administrator #4
Well, apart from changing the gender check to a switch, you could replace the strcats by simply doing sprintf.