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:
rav0
Veteran Member
*****

Avatar
i have an avatar

Posts: 1419
Reputation: 29
35 / Male / Flag
Joined: Aug 2003
O.P. (Quick)BASIC trouble (integers)
I'm doing a school assignment with QuickBASIC (does it make any difference that I'm using QuickBASIC and don't just say BASIC?).

I'm not exactly sure of the terminology, so if something I post doesn't make sense, please have me clarify it :).

When I'm printing integers, it always adds a space before and after it. How can I make it not do that?

Also, when printing an integer, how do I set the amount of decimal pplaces to output? It just uses the least amount neccessary.

It's a cost calculator, but with these two problems I'm having, prices end up like $ 238.5 rather than $238.50.

I hope somebody can help me.

This post was edited on 02-12-2006 at 08:14 AM by rav0.
| [Image: dorsh] |

(\ /)
(O.o)
(> <)

This is Bunny. Copy Bunny into your signature to help him on his way to world domination
02-12-2006 08:00 AM
Profile E-Mail PM Web Find Quote Report
Weyzza
Veteran Member
*****

Avatar
SoCal sunset > *

Posts: 1170
Reputation: 29
– / Male / –
Joined: May 2003
RE: (Quick)BASIC help
First off, I'm not familiar with qbasic.
Please forgive me if it doesn't work :p

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?

I believe there's a trim() function for eliminating the spaces.
That's usually available for most programming language.
quote:
Originally posted by rav0
Also, when printing an integer, how do I set the amount of decimal pplaces to output? It just uses the least amount neccessary.
I'm not sure if this is going to be working or not, but if I were you, I'll convert the value to string, check the number of digit after the decimal point, and concat a zero if necessary.
Registered 7854 days, 23 hours, 30 minutes, 17 seconds ago.
Happy Birthday, WDZ

02-12-2006 08:19 AM
Profile PM Find Quote Report
rav0
Veteran Member
*****

Avatar
i have an avatar

Posts: 1419
Reputation: 29
35 / Male / Flag
Joined: Aug 2003
O.P. RE: RE: (Quick)BASIC help
quote:
Originally posted by thekid
First off, I'm not familiar with qbasic.

Neither am I :).
quote:
Originally posted by thekid
Please forgive me if it doesn't work :p
Already done :p.
quote:
Originally posted by thekid
I believe there's a trim() function for eliminating the spaces.
That's usually available for most programming language.
According to Help, TRIM isn't there (though it might be named something else).
quote:
Originally posted by thekid
convert the value to string
I don't know how to do that :(.
| [Image: dorsh] |

(\ /)
(O.o)
(> <)

This is Bunny. Copy Bunny into your signature to help him on his way to world domination
02-12-2006 08:35 AM
Profile E-Mail PM Web Find Quote Report
Weyzza
Veteran Member
*****

Avatar
SoCal sunset > *

Posts: 1170
Reputation: 29
– / Male / –
Joined: May 2003
RE: (Quick)BASIC trouble (integers)
It's LTRIM$(astring$) to remove all spaces before the string, and RTRIM$(astring$) to remove all spaces after the string.

To convert an integer to string, use STR$(number%)

Source: http://www.qbcafe.net/english/ click Tutorials/QB Tutorial Part I

This post was edited on 02-12-2006 at 08:52 AM by Weyzza.
Registered 7854 days, 23 hours, 30 minutes, 17 seconds ago.
Happy Birthday, WDZ

02-12-2006 08:50 AM
Profile PM Find Quote Report
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
rav0
Veteran Member
*****

Avatar
i have an avatar

Posts: 1419
Reputation: 29
35 / Male / Flag
Joined: Aug 2003
O.P. RE: (Quick)BASIC trouble (integers)
Thanks a lot for your help, both of you.

It's submitted now.
| [Image: dorsh] |

(\ /)
(O.o)
(> <)

This is Bunny. Copy Bunny into your signature to help him on his way to world domination
02-14-2006 06:27 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


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