Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: New, again, with BASIC  (Read 28778 times)

0 Members and 1 Guest are viewing this topic.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
New, again, with BASIC
« on: January 26, 2013, 12:25:02 PM »
This is my first posting here. I did some BASIC programming back in the mid-1980s and now decided I wanted to get back into it again. This attached program was my first new attempt at it, using QB 1.1. I've since gotten into QB 4.5 and like it much better.

I'm running QB 4.5 on 32 bit Win XP. I start it using a .bat file on the desktop:

cd\basic 'The directory on the C: drive
qb         'QB's .exe file

I uploaded the file as a .txt document because the "Allowed file types: [are] doc, gif, jpg, mpg, pdf, png, txt, zip, log, bat, bmp. I saved BUDGET.BAS as a text file then opened it with Notepad and saved it as BUDGET.TXT.

Sure seems it would be simpler to upload it as BUDGET.BAS. Why the restriction?

I sure will appreciate any and all comments.  ???

[recovering disk space, attachment deleted by admin]
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #1 on: January 26, 2013, 04:22:19 PM »
One suggestion I have is to accept both Upper Case Y and Lower case y such as

INPUT "ENTER 'Y' for YES"; Enter$
IF Enter$ = "y" || Enter$="Y" THEN GOTO PrintIt
IF Enter$ <> "y" && Enter$ <> "Y" THEN GOTO RUSure

* Not sure if this is correct above for Basic with && (AND) || (OR) as for I am use to C++ syntax and cant test this here at work without QBasic. I started with GW-Basic long time ago, 1984-ish with DOS 2.11 but haven't  used it in years. When introduced to C and C++ in college in 1998, I made the switch to C++ as my choice language. But more recently I actually like using PERL for quick down and dirty console app programming.

Also unable to test your code but just browse through it in text editor right now. Do you need assistance with any of it?

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: New, again, with BASIC
« Reply #2 on: January 26, 2013, 04:55:10 PM »
One suggestion I have is to accept both Upper Case Y and Lower case y such as

INPUT "ENTER 'Y' for YES"; Enter$
IF Enter$ = "y" || Enter$="Y" THEN GOTO PrintIt
IF Enter$ <> "y" && Enter$ <> "Y" THEN GOTO RUSure

* Not sure if this is correct above for Basic with && (AND) || (OR) as for I am use to C++ syntax and cant test this here at work without QBasic
Basic and variants use AND and OR. Either way, a better approach would probably be a function. StrComp is a function found in more recent Versions of Visual Basic. It is not present in older QBASIC and QuickBASIC packages, but you can write a Version of it:

Code: [Select]
CONST FALSE = 0
CONST TRUE = NOT FALSE
DECLARE FUNCTION STRCOMP(str1 AS STRING, str2 AS STRING, COMPARETEXT AS INTEGER)
FUNCTION STRCOMP(str1 AS STRING, str2 AS STRING, COMPARETEXT AS INTEGER)
    IF COMPARETEXT THEN STRCOMP=(UCASE$(str1)=UCASE$(str2)) ELSE STRCOMP=(str1=str2)
END FUNCTION
Not tested, I'll admit. Speaking of Functions and subroutines, I notice a distinctive lack of them in the provided source listing, instead using GOTO for most of the flow control.

Aside from that, you might want to consider looking into more modern alternatives. If not Visual Basic, then something like FreeBASIC. FWIW, your provided source listing compiles and runs with the freeBASIC if the "-lang deprecated" flag is passed to the compiler. (it does provide a warning about a variable).

I was trying to dereference Null Pointers before it was cool.

Linux711



    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: New, again, with BASIC
« Reply #3 on: January 27, 2013, 11:17:14 AM »
I know this does not directly pertain to your question, but I think you should give QB64 a try. It is a modern remake of QB and is almost completely syntax compatible. If you are not just doing this for fun and actually want to gain some programming skills, you should choose a more modern programming language as BC suggested (maybe C#) because once you learn that one, most other programming languages will just fall into place; it wont take much learning time because of the similarities.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #4 on: January 27, 2013, 12:36:29 PM »
First - A big thank you to DaveLembke, BC_Programmer and Linux711 for your suggestions and help. I appreciate your interest.

Dave: I tried your suggestion for accepting both upper and lower case of a letter. As you suspected the && (AND) || (OR) will not work in QB 4.5. I modified it to a syntax that does work; see my attached file, BOTHCASE.txt. Thanks for the offer for additional assistance.

BC_Programmer I must admit I'd never heard of the "StrComp," function and the code you provided is a little above me at this point. You make a good point about functions and subroutines. I can see where that is better than using GOTO for most of the flow control.

Linux711 I'll add your suggestion to my list of apps to try. So now my list has:
1. QB64
2. Visual Basic
3. FreeBasic
4. QB64

I am doing this for fun but also want to gain some programming skills.  :D

[recovering disk space, attachment deleted by admin]
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

TheWaffle



    Hopeful
  • Thanked: 4
    • Yes
  • Computer: Specs
  • Experience: Beginner
  • OS: Linux variant
Re: New, again, with BASIC
« Reply #5 on: January 27, 2013, 01:37:38 PM »
I think it would be simpler to convert the ENTER$ variable to an uppercas before it goes to the if statement. I believe to do so you type:
 UCASE$(ENTER$)
In your code.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: New, again, with BASIC
« Reply #6 on: January 27, 2013, 02:38:36 PM »
Dave: I tried your suggestion for accepting both upper and lower case of a letter. As you suspected the && (AND) || (OR) will not work in QB 4.5. I modified it to a syntax that does work; see my attached file, BOTHCASE.txt. Thanks for the offer for additional assistance.

Original
Code: [Select]
CLS
PRINT "Would you like to print this information?"
PRINT
INPUT "ENTER 'Y' for YES"; Enter$
IF Enter$ = "y" THEN Enter$ = "Y": GOTO PrintIt
IF Enter$ = "y" THEN GOTO PrintIt
IF Enter$ <> "y" THEN IF Enter$ <> "Y" THEN GOTO RUSure
PrintIt:
PRINT : PRINT "Print it": END
RUSure:
PRINT : PRINT "Not print it": END

What about:
Code: [Select]
CLS
PRINT "Would you like to print this information?"
PRINT
INPUT "ENTER 'Y' for YES"; Enter$
Enter$ = UCASE$(Enter$)
IF Enter$ = "Y" THEN GOTO PrintIt
GOTO RUSure
PrintIt:
PRINT : PRINT "Print it": END
RUSure:
PRINT : PRINT "Not print it": END
Or, changing it to use structured programming, rather than "street BASIC" stuff (I think I'll describe that in a bit):

Code: [Select]
CLS
PRINT "Would you like to print this information?"
PRINT
INPUT "ENTER 'Y' for YES"; Enter$
Enter$ = UCASE$(Enter$)
IF Enter$ = "Y" THEN
    PRINT
    PRINT "Print it"
ELSE
    PRINT
    PRINT "Not print it"
END IF
END
That is essentially what you are "emulating" using the street BASIC goto. GOTO was the only control structure you could use in some early BASIC interpreters for Personal Computers. However, there hasn't been a good reason to limit yourself to using GOTO as the only method of flow control. It makes things more difficult to follow, and as programs get larger, it can become impossible to keep track of all the control flow. This applies equally to "GOSUB"; GOSUB works basically like GOTO, but you can make it return to where you used it with RETURN. For example:

Code: [Select]

CLS
PRINT "Would you like to print this information?"
PRINT
INPUT "ENTER 'Y' for YES"; Enter$
Enter$ = UCASE$(Enter$)
IF Enter$ = "Y" THEN GOSUB PRINTIT ELSE GOSUB NOPRINTIT
END
PRINTIT:
    PRINT
    PRINT "Print it"
    RETURN
NOPRINTIT:
    PRINT
    PRINT "Not print it"
    RETURN
END
This is functionally identical to the above. Naturally the appropriate logic would go within each, as I imagine you are implying with your own example.

Quote
BC_Programmer I must admit I'd never heard of the "StrComp," function
StrComp isn't in QuickBASIC, or most older BASIC dialects. I was thinking of Visual Basic.

Quote
and the code you provided is a little above me at this point.
My code was in error, actually.

The purpose is to create a new Function- STRCOMP, for comparing strings. Though perhaps a stumbling block for you is that you don't appear to be using Functions at all. Examples of common functions include LEFT$(), RIGHT$(), etc. StrComp() as present in languages that have it- unsurprisingly- compares the two strings you give it. if the first string is less than the second in the sort order, it returns -1; if they are equal, it returns 0. and if the second string is smaller, it returns 1.

Now, I quite recall my initial hurdles learning about Functions. The best way I found to consider them is more or less like a Math function. For example, math usually portrays math functions as f(x), which performs some function f on the value x. Most BASIC dialects include common Functions such as SIN, COS, and TAN.

A Function, then, usually has a few basic properties.

-A Name. This is pretty obvious. You need a name for a function so you can call it and use it.
-Arguments. Most functions take in inputs. for example, the trigonometric functions accept a number. This isn't required, though.
-A return value. This is what the function gives you back.

Now, in the case of StrComp, we have the following:

The Name will follow the standard "convention" of QuickBASIC, which is all uppercase function names. I think it looks ugly and makes the code look like it's yelling but when in Rome... Anyway, the name here will be STRCOMP.

Arguments. Our function will compare two strings. So we will need to accept two strings as arguments. We also want a argument to determine whether we will be case insensitive. We will accept an "INTEGER" number value. 0 will mean we don't want to compare text, and 1 will mean we do. ("Compare Text" meaning to be case insensitive)

QuickBASIC's rules regarding functions means we have to "DECLARE" it before we use it. This isn't 100% necessary but it's a good habit anyway. The line is essentially used to tell the compiler "I'm going to be using a Function, if you don't know what it does when you see it, don't worry, I'll tell you".


Code: [Select]
DECLARE FUNCTION STRCOMP(BYVAL str1 AS STRING,BYVAL str2 AS STRING, BYVAL COMPARETEXT AS INTEGER) AS INTEGER
the Function is declared with two String Arguments, and one INTEGER argument. the final section says the type of the Function's return value. in this case, INTEGER. BYVAL is worthy og explanation as well. This says that the Function will accept the parameters "By Value". In other words, when you call the function, the function get's a copy of what you sent it. This prevents the Function from changing those parameters, which can cause side effects later on.

Here is the implementation:

Code: [Select]
FUNCTION STRCOMP(BYVAL str1 AS STRING,BYVAL str2 AS STRING,BYVAL COMPARETEXT AS INTEGER) AS INTEGER
    IF COMPARETEXT THEN
        str1 = UCASE$(str1)
        str2 = UCASE$(str2)
    END IF
    iF str1<str2 THEN STRCOMP=-1
    IF str1=str2 THEN STRCOMP=0
    IF str1>str2 THEN STRCOMP=1

END FUNCTION
The first step we take is to check if the passed in COMPARETEXT argument is non-zero. This satisfies the IF condition, and we then change the two input strings to be the uppercase versions of themselves by assigning the result from the UCASE$() function on them. After that, we simply follow the convention. "return" values are set by assigning values to the name of the function. In this case, we set the appropriate values to STRCOMP inside the function. The last thing we assigned when the function exits will be used in place of the function call.

Some examples:

Code: [Select]
PRINT "StrComp test."
PRINT STR$(STRCOMP("False","FALSE",0))
PRINT STR$(STRCOMP("Turkey","Monkey",0))
PRINT STR$(STRCOMP("Animal","Weasel",0))
Quote
Linux711 I'll add your suggestion to my list of apps to try. So now my list has:
1. QB64
2. Visual Basic
3. FreeBasic
FWIW your budget program works fine in QB64 as well. QB64 is available here



I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #7 on: January 27, 2013, 03:01:28 PM »
Dear OP,
If you are comfortable with QB 4.5 just keep using it. You can make code that is both easy to read and efficient.

You can define procedures and functions in QB 4.5 as well as different types of variables. And the ability to handle binary files in QB if very good.

You don't need to learn another computer language unless you want to. Any language that does what you want is what you want. Age does not matter.

If someday QB will not run on a new OS, you just put it in a VM and keep working.

BYW, Fort ran is still around and does some very cool things.


Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #8 on: January 28, 2013, 07:42:03 AM »
Once again my thanks to BC_Programmer for all the help.

Geek-9pm: I appreciate your comments also.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
An update
« Reply #9 on: January 28, 2013, 01:05:09 PM »
Several folks had suggested QB64 as a next step. So when BC_Programmer told me that my "budget program works fine in QB64 as well. QB64 is available here" I clicked the "here" link, downloaded, and installed QB64.

It works fine and I like it but there is one thing I can't figure out how to change. I figured out how to get the editing window to full screen. But when I run the .bas program it goes to another window which is about 1/4 the size of my screen.

In QB45 the program runs in the same window in which the code is entered, while QB64 opens a popup window of about 1/4 the size of the full screen; at least that's what mine does.

I tried running QB64 with a desktop shortcut to qb64.exe and with the following desktop .bat file:

cd\qb64  'Directory on my C: drive where qb64.exe is located.
qb64.exe

What am I missing?
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Linux711



    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: New, again, with BASIC
« Reply #10 on: January 29, 2013, 11:14:34 AM »
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #11 on: January 30, 2013, 01:07:42 PM »
Thanks Linux711. I'll let you know how it works out. I haven't tried it yet in a program. It may be a few days though, since I have other stuff going.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #12 on: February 08, 2013, 01:02:51 PM »
OK, I tried QB64 and can readily see how much it is advanced over QB45. However, at least on my Win XP machine, it is cumbersome for me - I guess it's just too much for me to absorb at once.  :'(

I wrote the attached program in QB45 and do like the way it looks there - as a 2nd effort anyway. I ran this same code on QB64. It does run but: it opens in a window 1/4 the size of my monitor screen; the top section of the first screen has a blue background for the top half (as I intended) but black for the bottom half. The rest of the program runs the same as in QB45, just in the small window.

There are other items, which I don't need to get into now. My main goal now is composing efficient code.

Comments?

[recovering disk space, attachment deleted by admin]
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #13 on: February 08, 2013, 09:42:51 PM »
Comments?
Frankly I do not see any need to run this kind of program in 64 bit mode.
To the best of by knowledge there is no advantage to letting a small progrm have more memory space than  it could possibly use.

The code yon provided will compile to a EXE that takes less that a megabyte. And the data space is uses is trivial. Am I missing something here?

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: New, again, with BASIC
« Reply #14 on: February 08, 2013, 10:52:27 PM »
Comments?
Frankly I do not see any need to run this kind of program in 64 bit mode.
To the best of by knowledge there is no advantage to letting a small progrm have more memory space than  it could possibly use.

The code yon provided will compile to a EXE that takes less that a megabyte. And the data space is uses is trivial. Am I missing something here?

-QB64 is a 32-bit program, not a 64-bit one.
-Original QB's data usage is severely limited, as a result of not having access to a Virtual Memory manager. (Go ahead, try to find all the anagrams in a 200K+ Dictionary).
-64-bit Programs do not use more memory space. They have the capability to access more memory, though.

Also: QB64 appears to auto-update. Though that one could go either way. It is actively developed, however, which is a  plus.

As for efficiency: Switching to subroutines and functions will speed up the code significantly.

GOTO of course compiles to an Assembly JMP instruction. The issue with it is that it often wreaks havoc on the CPU prefetch cache, and basically every JMP will flush the prefetch queue. CALL and RET- which are used for functions and subroutines- work with the prefetcher logic. This wouldn't have a impact on your program- your program is currently fine, since most of it's time is spent waiting for user input.
I was trying to dereference Null Pointers before it was cool.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #15 on: February 09, 2013, 02:04:09 PM »
A few comments here:

I realize I heavily used GOTO; I had even more, until someone pointed that out. Each of my GOTOs now goes to a different weight range to display the results. Then, as each weight range completes, it goes to compute the ideal weight. The ideal weight for a male or a female of the same height is not the same, so I had to jump to different computations for each gender. The introduction is the only part that returns to where it left. This is the logic I used for how I compute the BMI and the ideal weight.

Here's how I implemented it, using a normal BMI as an example:

After the intro and asking for weight and height:    IF bmi < 24.9 THEN GOTO normal

normal:
    After displaying BMI: "Next we will determine your ideal weight."
    GOTO ideal  'Compute Ideal weight

ideal:
     After determining gender, go to appropiate section to compute (women: or men:)
     After determining ideal weight for either gender, GOTO terminate

The full code for this program is attached to my February 08, 2013 post.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #16 on: February 19, 2013, 08:17:49 AM »
I have started a website dedicated to QuickBasic 4.5. Have a peek at it.

http://quickbasic45.weebly.com/

John...
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Salmon Trout

  • Guest
Re: New, again, with BASIC
« Reply #17 on: February 19, 2013, 11:19:13 AM »
I see you include a link to a CH thread, which you call "my area on the forum". Isn't that a bit presumptuous?

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #18 on: February 19, 2013, 08:28:11 PM »
Maybe he means this thread.

Salmon Trout

  • Guest
Re: New, again, with BASIC
« Reply #19 on: February 20, 2013, 06:24:35 AM »
Maybe he means this thread.

Possibly, but it's stretching it a bit to call it "my area".

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #20 on: February 20, 2013, 07:59:04 AM »
Hey, a little slack please. I go by Newbe aka Greenhorn because I'm new here. At any rate, I changed "my area" to "my thread."
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #21 on: February 20, 2013, 10:10:29 AM »
Hey, a little slack please. I go by Newbe aka Greenhorn because I'm new here. At any rate, I changed "my area" to "my thread."

I see I've been upgraded to "Rookie."
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Another program
« Reply #22 on: March 15, 2013, 07:59:49 AM »
OK, here I am again with another endeavor. This time it's a program that asks a series of questions on mathematics and then calculates what percentage you got correct.

I originally wrote it in QB 45 and then tried it in QB 64; it worked just fine. I did have to add a _FULLSCREEN _STRETCH command at the beginning to get it full screen. (Linux 711 helped me on that.)

I'm hooked on QB 64 now that I have a better feel for it. The best part for me is that I can now work on a 64 bit machine with Win Vista and a 6.5" X 14" screen.

I know I could copy/paste the code here, but at 5.33 kb I feel that's it's too much. You can go to my website at quickbasic45.weebly.com to see a copy/paste of the code and/or download the .bas file. Or you can download a Notepad copy of the code right here.

Any comments?

[recovering disk space, attachment deleted by admin]
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #23 on: March 27, 2013, 01:12:52 PM »
Well here is my latest attempt, in QB 64. It's a 15 question trivia quiz, with (annoying?) sound. How about heading over to my website at http://quickbasic64.weebly.com/ and checking it out?
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #24 on: April 13, 2013, 02:33:35 PM »
Well here is my latest attempt, in QB 64. It's a 15 question trivia quiz, with (annoying?) sound. How about heading over to my website at http://quickbasic64.weebly.com/ and checking it out?

Here I am again. Remember my previous post about the annoying sound in the program? I've tried and tried to modify it so a user can turn the sounds off, but to no avail.

Any ideas or suggestions?

Here's a copy/paste of just the sound subroutines. You can download the entire program at my website at http://quickbasic64.weebly.com/

wrong:
IF sound$ = "n" THEN LOCATE 22, 22: PRINT "You have the sound turned off.";
RETURN
BEEP
BEEP
RETURN

correct:
LOCATE 14, 28: COLOR 18, 1: PRINT "That is correct!": COLOR 15, 1
IF sound$ = "n" THEN LOCATE 22, 22: PRINT "You have the sound turned off.";
RETURN
PLAY "mll64"
FOR x = 1 TO 50
    a$ = a$ + "v" + LTRIM$(STR$(x)) + "n" + LTRIM$(STR$(x))
NEXT
FOR x = 50 TO 1 STEP -1
    a$ = a$ + "v" + LTRIM$(STR$(x)) + "n" + LTRIM$(STR$(x))
NEXT
PLAY a$
a$ = ""
RETURN
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: New, again, with BASIC
« Reply #25 on: April 13, 2013, 06:47:00 PM »
You are using a one-line if statements. the return statements following your sound$="n" checks will always be run.
I was trying to dereference Null Pointers before it was cool.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #26 on: April 15, 2013, 03:42:11 AM »
BC_Programmer,
Thanks for the input. I've switched to a desktop and I seem to have non-BASIC programming problems with it. I'll go back to the laptop I wrote the program on and check it out. I'll get back to you.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #27 on: April 15, 2013, 10:37:22 AM »
wrong:
IF sound$ = "n" THEN LOCATE 22, 22: PRINT "You have the sound turned off.";
RETURN
BEEP
BEEP
RETURN

Using this shorter one as an example, I expect sound$ to be "n" when the user enters "n" at the "INPUT sound$" prompt. I initialize sound$ to "".
Each of the 15 questions calls the same wrong answer subroutine, which I'm trying to exit (return) if the user selects no sound. I use the same logic for correct answers.

What am I missing? How do I get away from the one-line if statements?

The lack of sound on this program seems to be a deficiency in this program (trivia2.bas) as well as on my desktop. If I run this program and my original version (trivia.bas) on my laptop, the sound works in both programs. I think I know what the desktop sound problem is though.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Salmon Trout

  • Guest
Re: New, again, with BASIC
« Reply #28 on: April 15, 2013, 12:28:52 PM »
wrong:
IF sound$ = "n" THEN LOCATE 22, 22: PRINT "You have the sound turned off.";
RETURN
BEEP
BEEP
RETURN

This above code is first going to run the single line IF test: if sound$ is "n", you PRINT the message, if sound$ is not "n" you don't PRINT the message, then execution continues at the next line (whatever the IF test result was) so you then RETURN, and the two BEEPs are never executed. It looks like you now need to find out about multiline IF statements,

Quote
What am I missing? How do I get away from the one-line if statements?

Here are a couple of examples that you may find illuminating:

IF sound$ = "n" THEN
    LOCATE 22, 22: PRINT "You have the sound turned off.";
    RETURN
END IF
BEEP
BEEP
RETURN


or (better)....

IF sound$ = "n" THEN
    LOCATE 22, 22: PRINT "You have the sound turned off.";
    RETURN
ELSE
    BEEP
    BEEP
    RETURN
END IF


Check here:

http://www.schoolfreeware.com/QBasic_Tutorial_7_-_If_Statements_-_QB64.html

and for overall guidance start here:

http://www.schoolfreeware.com/QBasic_Tutorials_-_QB64_Tutorials_-_Programming_And_Code_Examples.html



Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #29 on: April 15, 2013, 11:21:03 PM »
Quote
GOTO of course compiles to an Assembly JMP instruction. The issue with it is that it often wreaks havoc on the CPU prefetch cache, and basically every JMP will flush the prefetch queue. CALL and RET- which are used for functions and subroutines- work with the prefetcher logic. This wouldn't have a impact on your program- your program is currently fine, since most of it's time is spent waiting for user input.
Above posted earlier by BC_Programmer.
GOTO is almost never needed in modern programming.  Use of procedure and function is more a matter of style rather than t machine efficiency Using procedures with meaningful names help one to maintain disciple and sanity to a project. It can also reduce redundant code. In the rare case that a JMP instruction was really needed, the pre fetch  issue does not matter. If you have to JMP, just JMP.
Here is a reference.
http://en.wikipedia.org/wiki/Prefetch_input_queue

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
An Update
« Reply #30 on: April 16, 2013, 12:02:19 PM »
My special thanks to Salmon Trout and Geek-9pm for their fantastic help - including links to more web help. Getting away from the one-line IF-THEN statements seemed to be the key to my problems.

I even got the sound back - my desktop wasn't the problem at all.

I've decided to set this one aside for now and to move on to another project - haven't yet decided what.

Now you know why I say: "Half of knowledge is knowing where to find it."
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Here I go again - with another one
« Reply #31 on: April 30, 2013, 11:40:15 AM »
This program generates a number from two through twelve, simulating the roll of dice. You get to guess the number rolled; get it right and you can try again. Miss it ten times in a row and the game is over.

The program starts by popping up "ROLL THE DICE" one letter at a time but the way I did it seems inefficient. I wonder if there is a more efficient way, say using a DATA statement. The coding of the entire program is copy/pasted below but here is the part I'm wondering about:

LOCATE 5, 21: PRINT "Let's ..."
GOSUB pause: LOCATE 5, 31: PRINT "R " 'pause makes a one-second pause
GOSUB pause: LOCATE 5, 33: PRINT "O "
GOSUB pause: LOCATE 5, 35: PRINT "L "
GOSUB pause: LOCATE 5, 37: PRINT "L "
GOSUB pause: LOCATE 5, 40: PRINT "T "
GOSUB pause: LOCATE 5, 42: PRINT "H "
GOSUB pause: LOCATE 5, 44: PRINT "E "
GOSUB pause: LOCATE 5, 47: PRINT "D "
GOSUB pause: LOCATE 5, 49: PRINT "I "
GOSUB pause: LOCATE 5, 51: PRINT "C "
GOSUB pause: LOCATE 5, 53: PRINT "E "

You can download the program from my website at quickbasic64.weebly.com.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #32 on: April 30, 2013, 03:17:11 PM »
I have no idea of what your are doing. Why not just

PRINT "Roll the Dice"

Am, I missing something?

Salmon Trout

  • Guest
Re: New, again, with BASIC
« Reply #33 on: April 30, 2013, 03:24:04 PM »
I have no idea of what your are doing. Why not just

PRINT "Roll the Dice"

Am, I missing something?

He wants to print that phrase, character by character, with a pause after each one.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #34 on: April 30, 2013, 05:41:06 PM »
Thank you, Salmon Trout. That's exactly what I'm doing. I'm asking if there's a more efficient way to do it.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #35 on: April 30, 2013, 07:18:20 PM »
OK, create a procedure called SLOPRN that takes a string literal as the parameter.
It will can user MID$ to walk thru the letters on by one. After each print,  call the TIMER() to do a short delay.

You would invoke it like this.
LOCATE 5,3
CALL SLOPRN("Roll the Dice.")

Somebody else want to write the procedure? It will take me half and hour 'cause I am so slow.



BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: New, again, with BASIC
« Reply #36 on: April 30, 2013, 08:03:31 PM »
Something like:
Code: [Select]
SUB DELAYPRINT(LINE$, CHARDELAY!)
    DIM I AS INTEGER,STARTTIME AS SINGLE
    FOR I = 0 TO LEN(LNE$)
        PRINT MID$(LNE$,I,1);
        'delay
        STARTTIME=TIMER()
        DO WHILE(TIMER-STARTTIME<CHARDELAY!)
        'nothin.
        LOOP
    NEXT I
END SUB

'EXAMPLE:
DELAYPRINT "This is some text",1

I was trying to dereference Null Pointers before it was cool.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #37 on: April 30, 2013, 08:27:17 PM »
Yes, BC. That looks good.
By using  a SUB with parameters you can CALL  the same fragment of code over and over again. Just invoke with different parameters and c get various results. Because the SUB uses just local variables, the is no need to be afraid of it doing something bad on your global variables.. (Unless you let it.) The is.was one of the great improvements in QBASIC that set it apart from other BASIC packages of the era.
Remarkable, it even works in a 64 Windows 7. QBASIC still has a number of friends.

Of course, it one wants to do any visual effects, one ought to try and learn Visual Basic. Still  free for the express version. Early version still available.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Thank you all ...
« Reply #38 on: May 01, 2013, 08:48:01 AM »
for the help. :o
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #39 on: July 22, 2013, 12:11:38 PM »
Well here I am again with another endeavor. It's another exercise in trivia but with different questions. I gave up on the sound effects - for the present anyway. This time I tried to be more compact on my coding. For example, instead of repeating the same long request for input, I put the request into a subroutine. I also give the user the option of viewing the program in the smaller default QB64 window or in a full screen.

You can view a copy of the code and/or download the program from my website at quickbasic64.weebly.com.

As always I welcome your input.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #40 on: July 22, 2013, 12:22:50 PM »
What did you want to do with sound?
If you have the compiler, you can link your program to other libraries.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #41 on: July 23, 2013, 02:09:09 PM »
The only "sound" I've gotten into is beeps. It will be months before I try to get into more than that. I'm not ready to go beyond that. Thanks for the offer.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: New, again, with BASIC
« Reply #42 on: July 23, 2013, 10:42:31 PM »
QBASIC does more that beep or tweak.
Look here:
Quote
PLAY

PLAY "[string expression]"

Used to play notes and a score in QBasic on a computer equipped with a MIDI sound-card (all modern computer motherboards with built-in sound support MIDI). The tones are indicated by letters A through G. Accidentals are indicated with a "+" or "#" (for sharp) or "-" (for flat) immediately after the note letter. See this example:

PLAY "C C# C C#"

Whitespaces are ignored inside the string expression. There are also codes that sert the duration, octave and tempo. They are all case-insensitive. PLAY executes the commands or notes the order in which they appear in the string. Any indicators that change the properties are effective for the notes following that indicator.

Ln     Sets the duration (length) of the notes. The variable n does not indicate an actual duration
       amount but rather a note type; L1 - whole note, L2 - half note, L4 - quarter note, etc.
       (L8, L16, L32, L64, ...). By default, n = 4.
       For triplets and quintets, use L3, L6, L12, ... and L5, L10, L20, ... series respectively.
       The shorthand notation of length is also provided for a note. For example, "L4 CDE L8 FG L4 AB"
       can be shortened to "L4 CDE F8G8 AB". F and G play as eighth notes while others play as quarter notes.
On     Sets the current octave. Valid values for n are 0 through 6. An octave begins with C and ends with B.
       Remember that C- is equivalent to B.
< >    Changes the current octave respectively down or up one level.
Nn     Plays a specified note in the seven-octave range. Valid values are from 0 to 84. (0 is a pause.)
       Cannot use with sharp and flat. Cannot use with the shorthand notation neither.
MN     Stand for Music Normal. Note duration is 7/8ths of the length indicated by Ln. It is the default mode.
ML     Stand for Music Legato. Note duration is full length of that indicated by Ln.
MS     Stand for Music Staccato. Note duration is 3/4ths of the length indicated by Ln.
Pn     Causes a silence (pause) for the length of note indicated (same as Ln).
Tn     Sets the number of "L4"s per minute (tempo). Valid values are from 32 to 255. The default value is T120.
.      When placed after a note, it causes the duration of the note to be 3/2 of the set duration.
       This is how to get "dotted" notes. "L4 C#." would play C sharp as a dotted quarter note.
       It can be used for a pause as well.
MB MF  Stand for Music Background and Music Foreground. MB places a maximum of 32 notes in the music buffer
       and plays them while executing other statements. Works very well for games.
       MF switches the PLAY mode back to normal. Default is M
It helps if you have a basic knowledge of how to read music.

Newbe

    Topic Starter


    Rookie
  • Learning BASIC "again"
    • QuickBasic 6.4
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows Vista
Re: New, again, with BASIC
« Reply #43 on: September 20, 2013, 10:43:06 AM »
I have a website which relates to what I'm doing here.

Please check it out at:

http://quickbasic64.weebly.com/

Thank you.
'Learning.bas
CLS
PRINT "Half of knowledge is knowing where to find it."
SLEEP