Qbasicnews.com
chr$() in data statements? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: chr$() in data statements? (/thread-177.html)



chr$() in data statements? - clairvoyant - 02-09-2003

This is my first post. I'm trying to get DATA statements to read literals with a chr$() in them. Since they are not enclosed in quotes, the data statment rejects them.

Example:
"Pr" + CHR$(130) + "f" + CHR$(130) + "rence "

Any help would greatly be appreciated!!


chr$() in data statements? - Agamemnus - 02-09-2003

Data statements are unnecessary complications that are quite unique to QB.. some other langs have them but not all..

Don't use data statements.

You can do it using external files:

first load your file by (edit) writing in the variables:

CHDIR "C:\qbasic"

OPEN "sample.dat" for output as #1
FOR I% = 1 to 100
PRINT #1, datastring$
NEXT I%
CLOSE

OPEN "sample.dat" for input as #1
fOR I% = 1 to 100
LINE INPUT #1, datastring$(I%)
NEXT I%
CLOSE

'or..
'if you don't know how many you want..

OPEN "sample.dat" for input as #1
DO
I% = I% + 1
LINE INPUT #1, datastring$(I%)
LOOP UNTIL EOF(1)
CLOSE


chr$() in data statements? - na_th_an - 02-09-2003

You don't need CHR$ in DATA strings, it is unuseful for constant values. To enter special characters, just press the left ALT key and type the ASCII number in the numeric keypad, then release the ALT key. You'll get the characters you want.


chr$() in data statements? - clairvoyant - 02-09-2003

Thanks to both of the suggestions! They are good ideas, and I tried them, but I still have the same problem.

For clarification, the ascii codes I'm using are to display words translated into French. The chr$() is used to display individual letters with an accent on them.

The program is a questionnaire that shows a sequential series of words to the user, who makes a response to each one. I want to call them up as a series of statements.

So I don't think directly entering the codes into the keyboard will help here (please correct me if I'm wrong).

I also tried the input file method, but again literals a with "chr$()" in them would hang at run time. If I enclose the whole thing in quotes:

""Pr"+ chr$(136) + "t(e)?""

then I simply get back:

"Pr"+ chr$(136) + "t(e)?"

which is what one would expect, correct?

The only thing I could think of is to assign each word a variable name like statement1$, statement2$, etc. Then I use a Select Case routine to select each statement and display it. Cumbersome, but this does correctly display the words with an embedded ascii code.

But I still would like to read them in from an external file, just as is possible with a regular list of words:

"happy"
"sad"
"tired"

etc.

Thanks again, and any further thoughts again would be appreciated.


chr$() in data statements? - na_th_an - 02-09-2003

You want the characters with accents, so just use ALT+number. You'll get the character. It is just the same thing to write:

Code:
"HOL"+CHR$(65)

and to write:

Code:
"HOLA"



chr$() in data statements? - Agamemnus - 02-09-2003

If you read it from a file and that file has ASCII french characters, you will get those characters; unless you do CHR$(n).

You can use mine or make a routine to parse anything that has a CHR$(n) in an external file:

Code:
CLS
'this string is actually: "fran" + CHR$(135) + "ais"
string1$ = CHR$(34) + "fran" + CHR$(34) + " + CHR$(135)" + " + " + CHR$(34) + "ais" + CHR$(34)
FOR I% = 1 TO LEN(string1$)
k1% = INSTR(I%, string1$, CHR$(34))
k2% = INSTR(I%, string1$, " + CHR$(")
k3% = INSTR(I%, string1$, "CHR$(")
IF k1% + k2% + k3% > 0 THEN
'gotta sort em
IF k1% = 0 THEN k1% = LEN(string1$) + 1
IF k2% = 0 THEN k2% = LEN(string1$) + 1
IF k3% = 0 THEN k3% = LEN(string1$) + 1
IF k1% < k2% AND k1% < k3% THEN k% = k1%: GOSUB getstring
IF k2% < k3% AND k2% < k1% THEN k% = k2%: GOSUB getchar
IF k3% < k2% AND k3% < k1% THEN k% = k3%: GOSUB getchar
END IF
NEXT I%

PRINT string2$
SYSTEM

getchar:
j% = INSTR(k%, string1$, ")")
c1$ = MID$(string1$, k% + 8, j% - k% - 8)
I% = j% + 1
string2$ = string2$ + CHR$(VAL(c1$))
RETURN

getstring:
j% = INSTR(k% + 1, string1$, CHR$(34))
c1$ = MID$(string1$, k% + 1, j% - k% - 1)
I% = j%
string2$ = string2$ + c1$
RETURN



chr$() in data statements? - clairvoyant - 02-11-2003

Thanks to both Agamemnus and na_th_an. First, using INPUT rather than DATA statements is indeed a better way to go. Second, I had thought I had to actually have the chr$() in the string but, as pointed out, directly entering the ASCII characters using the Alt key works just fine.

Very helpful forum.