What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » (Quick)BASIC trouble (integers)

(Quick)BASIC trouble (integers)
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: RE: (Quick)BASIC help
quote:
Originally posted by thekid
I believe there's a trim() function for eliminating the spaces.
No there isn't...

quote:
Originally posted by thekid
Source: http://www.qbcafe.net/english/ click Tutorials/QB Tutorial Part I
No need for tutorials (which aren't even entirly accurate) for this, as Q(uick)Basic comes with a complete and easy help system including examples, etc.

All you need to do is press F1 when your cursor is over a command or function to pop up the comprehensive help about that command or function (including examples, related links, etc)

----------------------------------

@ Those who aren't familiar with Q(uick)Basic: Although the methods described may work, they are often not needed. Don't guess if you're not familiar with a specific language, it will only confuse the OP (and others) and often requires more post-corrections and other explainations than it would have been needed in the first place.

----------------------------------

quote:
Originally posted by rav0
When I'm printing integers, it always adds a space before and after it. How can I make it not do that?
This is so, by default, in many Basic variants (if not all!!!). The reason behind it is that Integers in Basic are always signed. They are either negative, 0, or positive. The 'space' is actually the placeholder of the sign of the Integer, which isn't 'printed' when the Integer is 0 or higher.

If you want to remove it, it depends on what method you use to print it. In Q(uick)Basic (and some other variants) you can format how numbers should be printed by using the PRINT USING command.

With this, you can remove the space, show the sign (even when +), add decimal places and fill them in, etc... This is also the method to set the amount of decimal places to output... eg:

PRINT USING "###.##"; 123.4
will output: 123.40

PRINT USING "+##.##"; 12.3
will output: +12.30

PRINT USING "##.##-"; 12.3
will output: 12.30 (thus without any leading spaces if the number is 0 or higher)

To know more about the PRINT USING command and its many format codes, set your cursor on the "print" or "using" part and press F1 (this goes for all Q(uick)Basic commands; use it ;)).

--

Another method to remove the leading space with positive numbers, is to convert the number to a string and then remove the space (if it is there; negative numbers don't have a leading space, see above).

To do this you can use the STR$() function. This function is also available in many other variants (eg: Visual Basic). And after you've converted the number to a string, you need the LTRIM$() function to remove any spaces on the left.

Do not use the LEFT$() function or the MID$() function to remove spaces as you'll never know if there is a leading space or not in the first place (eg: with negative numbers, there isn't one).

PRINT LTRIM$(STR$(12.5))
will output: 12.5 (thus without any spaces)

PRINT LTRIM$(STR$(-45))
will output: -45 (thus without any spaces)

PRINT MID$(STR$(12.5), 2)
will output: 12.5 (thus without any spaces)

PRINT MID$(STR$(-45), 2)
will output: 45 (because with the MID$(x ,2) function you show only a substring of the original string. In this case, everything from character position 2 and further; character position 1 was the sign, so it isn't shown. Hence why you shouldn't use this function)

!!! To know more about the STR$(), LTRIM$(), etc functions, set your cursor on the function name and press F1 (this goes for all Q(uick)Basic functions; use it ;)).

--

Note that there is not a TRIM$() function in Q(uick)Basic. This was only introduced in Visual Basic. To simulate the TRIM$() function in Q(uick)Basic you can combine the LTRIM$() and RTRIM$() function:
PRINT LTRIM$(RTRIM$("   Hello World   "))
will output "Hello World", thus without the leading and trailing spaces.

----------------------------------

PS: QBasic and QuickBasic are not the same languages! QBasic, which came with DOS 5 and 6 is a stripped down version of QuickBasic. QuickBasic is the complete package and includes a compiler. QuickBasic isn't free! (although you can't really buy it anymore now-a-days of course).

Stuff made in QuickBasic often doesn't work in Qbasic. Stuff made in Qbasic often does work (but not always) in QuickBasic.

- QuickBasic 1.0 and 2.0 are very rare and were not much used.
- QuickBasic 3.0 is still rare (now-a-days), though it was the first wide-spread and often used basic programming language. Programs compiled in QB3.0 (QB is the short for QuickBasic (not for QBasic!)) are extremely fast, faster than when compiled with higher version numbers.
- With QuickBasic 4.0, the QuickBasic language became very popular. (The compiler was slow though; much because of all the added commands and functions)
- QuickBasic 4.5 was the final and last version of the QuickBasic language and was extremely popular and was one of the most used programming languages in its days.

- Further updates by Microsoft introduced Power Basic (PB 6.0 and 7.0), which took QuickBasic to an even higher level.

----------------------------------

In response to the topic title (and just as a sidenote; though a bit important too): "(Quick)Basic"...

Not only is QBasic different from QuickBasic, ... "Basic" is a way too vague description of the language you're working in or requesting info about. "Basic" is an extremely wide and vague description of the langauge.

So the more correct topic would be: "QuickBasic" or if you must "Q(uick)Basic" (as QBasic and QuickBasic are compareable in many aspects though).

It is mandatory that you state which Basic variant you're working in. Though many commands work roughly the same across variants and versions, this isn't always so, and each variant or version has his own need-to-know things, do's and dont's, specific command and functions, etc, etc.

So all the above goes for QBasic and QuickBasic, but may not work in other variants!!!!

----------------------------------

;)

Happy programming in the ancient art of DOS programming ;)

PS: if you are very well experienced in QuickBasic, you can do stuff you never believed it could've been done. QuickBasic is/was, despite what some might say, an extremely powerfull language. Especially when combined with interrupts (aka the old "DOS API's") and assembly...

This post was edited on 02-12-2006 at 08:31 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
02-12-2006 08:04 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
(Quick)BASIC trouble (integers) - by rav0 on 02-12-2006 at 08:00 AM
RE: (Quick)BASIC help - by Weyzza on 02-12-2006 at 08:19 AM
RE: RE: (Quick)BASIC help - by rav0 on 02-12-2006 at 08:35 AM
RE: RE: (Quick)BASIC help - by CookieRevised on 02-12-2006 at 08:04 PM
RE: (Quick)BASIC trouble (integers) - by Weyzza on 02-12-2006 at 08:50 AM
RE: (Quick)BASIC trouble (integers) - by rav0 on 02-14-2006 at 06:27 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On