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

0 Members and 1 Guest are viewing this topic.

Offline astrosteve

  • Newbie
  • Posts: 3
    • View Profile
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: QB64: [Select]
  1. res$ = "This is a test."
  2. rescount = LEN(res$)
  3.  
  4. FOR solved = 1 TO rescount
  5.     IF MID$(res$, 1, solved) = CHR$(32) THEN phrase$ = MID$(res$, 1, solved): solved = rescount - 1
  6. NEXT solved
  7.  
  8. PRINT phrase$; " is the word."
  9.  

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.

FellippeHeitor

  • Guest
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: QB64: [Select]
  1. res$ = "This is a test."
  2.  
  3. IF INSTR(res$, " ") THEN
  4.     phrase$ = LEFT$(res$, INSTR(res$, " ") - 1)
  5.     PRINT phrase$; " is the word."
  6.  
« Last Edit: June 13, 2018, 11:56:21 am by FellippeHeitor »

Offline astrosteve

  • Newbie
  • Posts: 3
    • View Profile
.... 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!

FellippeHeitor

  • Guest
My pleasure!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Welcome to the forum, astrosteve!