PRINT Strings

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

PRINT Strings

Post by Pete »

Hey guys, answer this dude's question for me, will you?
Ref: Comments:
I use to be able to run the different lines seperatly in gwbasic, but
haven't been able to in qbasic. For instance the command "print L" would
give me the value of L. My qbasic does not give me that. Can you help me?
Dale Crook

``````````````````````````````````````````````````````````````````````

What I'm trying to do is print a column with different length strings,
and have the column justified on the right Dale Crook<
instead of the left. >Dale Crook

Code: Select all

          Darlene Crook<                                        >Darlene Crook
                Bob  Crook<                                       >Bob Crook
My program statement was L= len(name$). PRINT Tab (18 - L) name$. That
didn't give me the result I wanted. Thanks for your help.

Dale
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

This should do the trick:

Code: Select all

DECLARE SUB RightJustify (TextString AS STRING, ColumnWidth AS INTEGER)

DIM WorkName(1 TO 5) AS STRING

WorkName(1) = "Stephane Richard"
WorkName(2) = "Pete Berg"
WorkName(3) = "Alice Cooper"
WorkName(4) = "Terry Fox"
WorkName(5) = "Kristian Virtanen"

FOR Counter = 1 TO 5
    CALL RightJustify(WorkName(Counter), 30)
NEXT Counter

SLEEP

SUB RightJustify (TextString AS STRING, ColumnWidth AS INTEGER)

    Position% = ColumnWidth - LEN(TextString)
    LOCATE CSRLIN, Position%
    PRINT TextString

END SUB
Expected Output:

Code: Select all

             Stephane Richard
                    Pete Berg
                 Alice Cooper
                    Terry Fox
            Kristian Virtanen
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Also, to run lines seperatly look at the bottom of your QBASIC screen. There is an immediate box (if you don't see it, press F6 a few times) and then you can run lines immediatly (hence the title) there. You just have to type (or copy) the line there and press enter.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Not as elegant as Mystik's solution, but a little simpler:

Code: Select all

DIM WorkName(1 TO 5) AS STRING
WorkName(1) = "Stephane Richard"
WorkName(2) = "Pete Berg"
WorkName(3) = "Alice Cooper"
WorkName(4) = "Terry Fox"
WorkName(5) = "Kristian Virtanen"

REM Assuming that no name is greater than 30 characters.
FOR Counter = 1 TO 5
    PRINT SPACE$(30-LEN(WorkName(Counter))+WorkName(Counter)
NEXT Counter

SLEEP
Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni »

Even simpler:

Code: Select all

DIM WorkName(1 TO 5) AS STRING
WorkName(1) = "Stephane Richard"
WorkName(2) = "Pete Berg"
WorkName(3) = "Alice Cooper"
WorkName(4) = "Terry Fox"
WorkName(5) = "Kristian Virtanen"

REM Assuming that no name is greater than 30 characters.
a$=space$(30)

FOR i = 1 TO 5
    rset a$=""                         'in FB use rset a$, ""
    rset a$=Workname(i)         'in FB use rset a$,Workname(i)
    print a$
NEXT Counter
SLEEP 
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Antoni,

The instruction " rset a$="" " is not needed, so I removed it.

The instruction "NEXT Counter" should be "NEXT i".

I tested it and now it runs "even simpler".

I never used the RSET and LSET statements in QB. I thought they were only for use with FIELD statements.

Thanks, like they say, "you learn something every day."
*****
Post Reply