Author Topic: Programming in basic for the first time in 20 years.. need some help.  (Read 151 times)

Hi, everyone. I hope this is the right place to post for code help.

I recently have been trying to program a bit. I used to do it a lot in the 90s and 80s, but totally stopped. I've been programming mostly in C since the mid 90s when I do anything, but I recently decided to go back to my childhood and try Basic again. I found out about QB64 a few days ago on Twitter , tried it out and here I am.

I'm a text based sort of coder, I don't really like doing stuff with graphics. Remembering a fun project I did in C about 10 years ago where I wrote a parser for a text based game, I decided to try something similar in basic and immediately got stuck. The first step I did is isolate the command word (as I call it) and process that. But it's not working well. The program is super short at this point so I'm including the entire thing here:

Code: [Select]
res$ = "This is a test."
rescount = LEN(res$)

FOR solved = 1 TO rescount
    IF MID$(res$, 1, solved) = CHR$(32) THEN phrase$ = MID$(res$, 1, solved): solved = rescount - 1
NEXT solved

PRINT phrase$; " is the word."

The output of this program should be "This is the word." The only thing I ever get out of it is " is the word." The IF statement in the For loop doesn't work as best I can tell. I don't think my condition is bad, as if I replace the If statement with  PRINT MID$(res$, 1, solved) it prints out res$ incremented by one character each loop, which is what should happen. As best I can tell after a surprisingly large amount of troubleshooting is that it never recognizes a space and the IF condition is never met, despite spaces being in the string.

Why the heck doesn't this work? I've been staring at that code for the past half hour and can't figure out the problem.

Also, as an aside, is there a way to set it to require variable declaration? This project will probably eventually be several hundred lines of code and I'd prefer to not be able to create new variables by accident.

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Hi astrosteve, welcome to the forum.

You seem to be getting the order of parameters wrong for MID$.

Instead of IF MID$(res$, 1, solved) it should read IF MID$(res$, solved, 1). The syntax is MID$(stringToAnalyze$, position%, length%).

To have QB64 require variable declaration, add OPTION _EXPLICIT to the beginning of your program.

PS: You can make it simpler, without a FOR/NEXT block if you wish. Use INSTR to figure where in the string is the first occurence of a space character, then grab what's to the left of it with LEFT$():

Code: [Select]
res$ = "This is a test."

IF INSTR(res$, " ") THEN
    phrase$ = LEFT$(res$, INSTR(res$, " ") - 1)
    PRINT phrase$; " is the word."
END IF
« Last Edit: June 13, 2018, 12:56:21 PM by FellippeHeitor »

.... I got the arguments for MID$ backwards? What the. I tested it the way it was and it seemed to work. Well, now I'm embarrassed. But thank you for the help!

The loop is an interesting idea. I wasn't particularly happy with the For/Next because I had to manually end it, which just looks ugly.  I'll probably use your suggestion. Thanks again!

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
My pleasure!

Online Petr

  • I am instructed.
Welcome to the forum, astrosteve!