Page 1 of 1

Strings equaling another string??

Posted: Sat Dec 09, 2006 10:00 pm
by Mike Alexander
I have run into an interesting issue with my non-printable character issue...

I read an input from a file to determine an intensity and color to be used.. In this example, the line is read into a strings named INTENSITY1$ BARCOLOR$ and is "NORMAL" and "RED"

INTENSITY1$ ="NORMAL"
BARCOLOR$ ="RED"


I build the color string to be used... When it is all said and done. The output of this will be NREDFG$

BARCOLOR$= LEFT$(INTENSITY3$,1)+LTRIM$(RTRIM$(BARCOLOR$))+"FG$"

Now I am definine what exactly a NREDFG$ is... To the file I am writing, I need NREDFG$ to actually be CHR$(001)NCHR$(001)R so I have the following line next...

NREDFG$= CHR$(001)+"N"+CHR$(001)+"R"

When I do a PRINT NREDFG$ if comes out exactly how I want, BUT when I do a PRINT BARCOLOR$ is actually uses NREDFG$....

How can I make the BARCOLOR$ point to NREDFG$ instead of literally printing NREDFG$ ??? In C programming I can use a pointer to it... how do I do this in my example?

Posted: Thu Mar 01, 2007 9:02 am
by Skyler
You can't, as far as I know.

Posted: Thu Mar 01, 2007 11:57 am
by Z!re

Code: Select all

BARCOLOR$ = NREDFG$
And yes, I'm aware this isnt waht you asked.
In FreeBASIC you can use pointers

Posted: Sat Jun 16, 2007 1:34 pm
by historian
Unfortunately, QBASIC, being a primitive language, doesn't actually have pointers (perhaps there's a reason why it's obsolete). So, you can't make BARCOLOR$ be a pointer to NREDFG$.

However, your code doesn't even try to make BARCOLOR$ be a pointer to NREDFG$. All it does is generate the name of NREDFG$, which wouldn't work in C, either:

Code: Select all

/* phpBB f?cked up the #includes, so I'm leaving
 * them out. phpBB sucks.
 */
#define COLOR "RED"

main() {
  const char *NREDFG = "\001N\001R";
  char BARCOLOR[30];
  const char *INTENSITY1 = "NORMAL";

  /* Let's attempt to access NREDFG based on its
   * name.
   */

  /* Build a string into BARCOLOR */
  BARCOLOR[0] = INTENSITY1[0];
  BARCOLOR[1] = 0;
  strcat(BARCOLOR, COLOR);
  strcat(BARCOLOR, "FG");
  /* Whoops! */
  printf("%s\n", BARCOLOR);
}
Output:
<pre>
NREDFG
</pre>

What you want to do instead is build an array into which you can index based on the values found in your input. Taking advantage of what little support QB has for structured programming, you could create a FUNCTION that takes "NORMAL", "BRIGHT", etc as an argument and returns a number. You would do the same for color names. Then, you set up a 2-dimensional array that contains all the NREDFG$-like strings:

Code: Select all

' Assuming that you're going to support three intensities ("DIM",
' "NORMAL", and "BRIGHT") and 16 colors. The numbers
' in the DIM statement are maximum array subscripts, rather
' than sizes.
DIM colorTable(2, 15) AS STRING

' Populate the table.
colorTable(0,0) = CHR$(1)+"D"+CHR$(1)+"B"
colorTable(1,0) = CHR$(1)+"N"+CHR$(1)+"B"
' Etc...
Access to this table might look like this:

Code: Select all

DIM colorCode AS STRING
colorCode = colorTable(getIntensity%(INTENSITY$), getColor%(BARCOLOR$))
In the event that $INTENSITY="NORMAL" and BARCOLOR$="RED", colorCode will then be identical to $NREDFG.
________
Dodge Hornet