RIGHT$ problem

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
Dr. D'nar
Newbie
Posts:6
Joined:Tue Sep 28, 2004 10:49 pm
RIGHT$ problem

Post by Dr. D'nar » Mon Dec 06, 2004 11:46 pm

RIGHT$ doesn't seem to be working propley...

Code: Select all

INPUT "File to read from: ", infile$
INPUT "File to output to: ", outfile$
OPEN infile$ FOR INPUT AS #1
OPEN outfile$ FOR APPEND AS #2

DO
temp$ = ""
sep! = 0
ans$ = ""
que$ = ""
INPUT #1, temp$
cnt! = cnt! + 1
sep! = INSTR(temp$, "|")
ans$ = LEFT$(temp$, sep! - 1)
que$ = RIGHT$(temp$, sep! + 1)
PRINT sep! 'not really need; for debugging only
PRINT ans$ 'not really need; for debugging only
PRINT que$ 'not really need; for debugging only
PRINT #2, sep! 'not really need; for debugging only
PRINT #2, que$ + "?" + "*" + ans$
LOOP UNTIL EOF(1) = -1
CLOSE #2
CLOSE #1
PRINT "All finished!"
Input file...
answer|question
hijklmnop|abcdefg
zsdx|adsd
67890|12345
efghgg|abcdff

Output file...
9
r|question?*answer
16
hijklmnop|abcdefg?*hijklmnop
7
sdx|adsd?*zsdx
6
0|12345?*67890
7
g|abcdff?*efghgg

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Tue Dec 07, 2004 4:59 am

You might want to use MID$ instead... Is this what you want?

Code: Select all

CLS
Temp$ = "Mid$ is|a better solution."

Sep% = INSTR(Temp$, "|")
ans$ = MID$(Temp$, 1, Sep% - 1)
que$ = MID$(Temp$, Sep% + 1)
PRINT Sep% 'not really need; for debugging only
PRINT ans$ 'not really need; for debugging only
PRINT que$ 'not really need; for debugging only

PRINT
PRINT "All finished!"
Output:
8
Mid$ is
a better solution.

All finished!
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!

Dr. D'nar
Newbie
Posts:6
Joined:Tue Sep 28, 2004 10:49 pm

Post by Dr. D'nar » Fri Dec 10, 2004 2:00 am

Yes!

(But why doesn't RIGHT$ work?)

Dr. D'nar
Newbie
Posts:6
Joined:Tue Sep 28, 2004 10:49 pm

Post by Dr. D'nar » Sun Dec 12, 2004 8:55 pm

Okay here's the new code:

Code: Select all

INPUT "File to read from: ", infile$
INPUT "File to output to: ", outfile$
OPEN infile$ FOR INPUT AS #1
OPEN outfile$ FOR APPEND AS #2

DO
'Temp$ = ""
'Sep! = 0
'ans$ = ""
'que$ = ""
INPUT #1, Temp$
cnt! = cnt! + 1
Sep! = INSTR(Temp$, "|")
ans$ = MID$(Temp$, 1, Sep% - 1)
que$ = MID$(Temp$, Sep% + 1)
PRINT Sep!
PRINT ans$
PRINT que$
'PRINT #2, sep!
PRINT #2, que$ + "?" + "*" + ans$
LOOP UNTIL EOF(1) = -1
CLOSE #2
CLOSE #1
PRINT "All finished!"
And of course it refuses to work. Illagle fuction call. Right. Illagle error call.

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

RIGHT$ problem

Post by Ralph » Wed Dec 15, 2004 6:22 am

Hmm... I like to find one answer at a time, so:

If your first reading is:
"answer|question "
then the INSTR function should return 7, not 9, as "|" is the 7th character in the string "answer|question". So, how about clearing up this discrepancy first?
With 9 as the answer, "r|question" is correct. Could it be that the reading is, actually, " answer|question", that is, "answer|question", with two spaces in front?

I tried your program with "answer|question" as the string, and I got the correct answers, 7, "answer" and "question".
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Thu Dec 16, 2004 4:07 am

Try converting all the numeric variables to integer(%) first. I don't have much time right now... Mid$ works correctly for me though.
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!

Post Reply