Strings equaling another string??

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Strings equaling another string??

Post 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?
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
User avatar
Skyler
Coder
Posts: 16
Joined: Wed Jun 07, 2006 4:15 pm
Location: Delta Quadrant

Post by Skyler »

You can't, as far as I know.
For God so loved the world he gave His only begotten Son that whosoever believeth on Him shall have everlasting life.
John 3:16
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Code: Select all

BARCOLOR$ = NREDFG$
And yes, I'm aware this isnt waht you asked.
In FreeBASIC you can use pointers
I have left this dump.
historian
Newbie
Posts: 4
Joined: Sat Jun 16, 2007 12:11 pm

Post 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
Post Reply