www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/06/23:01:45

From: "Chia" <chia AT top DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Strange Function Behavior
Date: Thu, 6 Aug 1998 21:54:20 -0500
Organization: 404 Software
Lines: 66
Message-ID: <6qdq5j$as2@nnrp2.farm.idt.net>
NNTP-Posting-Host: flex-17.top.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I ran into a problem with a GUI I'm writing, and after some creative problem
hunting have managed to eliminate the problem.  However, I'm not sure why
what I did fixed it.

    I had buttons which had text with the & symbol so that they would
underline.  (e.g. "E&xit", etc.)

    Anyway, I had a function that I used to parse the character array,
delete the & symbol, and return the position that it was in the string.
This function was called parseTitle().

    The original function looked like this:

    int parseTitle( char *name )
    {
      for( int i = 0; i < (int)strlen(name); i++)
      {
        if (name[i] == '&')
        {
        for( int c = i; c < (int)strlen(name); c++)
          name[c] = name[c + 1];
        return i;
        }
      }
    return -1;  // returns negative if not found.
    }

Anyway, I had created a buttons with the text "&Show Barquie" and "&Hide
Barquie".  This apparently worked fine.

    Then I started work on the menus, and "&File" worked. I was making the
menu options.  These options, too, used the & symbol for the underlining.  I
needed some way to test my menu, and I used "&Show Barquie" and "&Hide
Barquie".

    This didn't work at all.  I began to suspect the compiler was being
screwy and I posted a message about how when I passed these strings it lost
the & sign.

    Well, I commented out code and found that in parseTitle, I found that
the loop

       for(int c = i; c < (int)strlen(name);c++) name[c] = name[c + 1];

    was causing the problems.

    I rewrote the function to use strcpy() and use a temporary string, then
copy the string over, and it worked ok.

    Does anyone know what was happening in the original code?  I don't
understand why it would act that way.  I figure it was probably something
with the name[c + 1] running over the boundaries or something, but I don't
have any idea why this would do that.

I mean, the problem was like this:

    fn("&Show Barquie");
    void fn(char *txt) { cout << txt; }

    prints "Show Barquie".  ???

    Any hypotheses are welcome.

// chia AT top DOT net -- http://www.top.net/chia/


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019