Qbasicnews.com

Full Version: Newbie Needs Help! Problems!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Im really new at this and i was trying to write a program using an array of the months of the year
CLS
DIM months$(12)
months$(1) = "January"
month$(2) = "February"
month$(3) = "March"
PRINT months$(1); month$(2); month$(3)

this is a short example and when i run the program it reads
JanuaryFebruaryMarch
how do i put a space between them?
WIth PRINT you can use both variables, or literals, separated by ; (so they appear one next to another) or , (so they are tabbed).

Literals are numbers such as 5, 65, 22.21 or 4534e9, or strings, i.e. text that has to be enclosed between quotes ", so the interpreter knows where it begins and where it ends.

Code:
PRINT " "

will print a space. Hence

Code:
PRINT months$(1); " "; months$(2); " "; months$(3)

will print a space between each couple of months.

More Info @ http://qbasicnews.com/qboho/qckprint.shtml
Or, you can save some repetitions by using something like this"
Code:
FOR I = 1 TO 12
  PRINT MONTH$(I);" ";
NEXT I
which will print out the twelve months that you have defined in your month$(1) = "Januray", etc.

Note that, if you had named your months, such as January, thus:
month$(1) ="January " (notice the space at the end).
then, your original printout would have encluded a space separating every two months...

And on and on...
Quote:Or, you can save some repetitions by using something like this"
Code:
FOR I = 1 TO 12
  PRINT MONTH$(I);" ";
NEXT I
......
Since the trailing semicolon on the PRINT line is expecting more data to be appended to the print line, it will not print out the final line until you flush it out with a ending PRINT statement. Like this:
Code:
FOR I = 1 TO 12
  PRINT MONTH$(I);" ";
NEXT I
PRINT
Subsequent screen I/O or terminating the program will also flush the suspended PRINT line.
*****
Or if you'd rather not have the added space at the end of the line, and/or you'd like to keep your PRINTing logic contained inside your loop, you can

Code:
dim Month(1 to 12) as string

Month(1) = "January"
Month(2) = "February"
    '' ...
Month(12) = "December"

dim m as integer
for m=1 to 12
    if( m > 1 ) then print " " ;
    print Month(m) ;
    if( m = 12 ) then print
next

sleep : end

check for the month each iteration.
Quote:Or if you'd rather not have the added space at the end of the line, and/or you'd like to keep your PRINTing logic contained inside your loop, you can

Code:
dim Month(1 to 12) as string

Month(1) = "January"
Month(2) = "February"
    '' ...
Month(12) = "December"

dim m as integer
for m=1 to 12
    if( m > 1 ) then print " " ;
    print Month(m) ;
    if( m = 12 ) then print
next

sleep : end

check for the month each iteration.
It may make sense, but IMHO it's never wise to add additional IF's into a program that can perform the same thing without them.
*****
Quote:It may make sense, but IMHO it's never wise to add additional IF's into a program that can perform the same thing without them.
*****
So, you'd recommend something like this?

Code:
...
print Month(1) ;

dim m as integer
for m=2 to 12
    print " " ; Month(m) ;
next

print
   ...
Ultimately, you're right; adding additional branches in program flow is generally ill-advised. But of course so is using non-zero array bounds, although in this particular case doing so does add a bit of clarity.
Nerds! Big Grin
yeah.. I dont think anyone is going to notice a space on the end of that >_>
Okay, there are several ways you can optimize this.

Place the names of the months into some DATA statements, like so(though you could put them into a file):

Code:
DATA "January","February","March","April","May"
DATA "June","July","August","September","October","November"
DATA "December"

Then you read them into your array:

Code:
DIM Months(12) as STRING

FOR I = 1 TO 12
READ Months(I)
NEXT

Then, to print them out, do this:

Code:
FOR I = 1 TO 12
PRINT Months(I);" "
NEXT

Just put the code toghether, and it should work. You should see all 12 months printed out, even though it might wrap to the next line. If it does that, consider using abbreviations.

EDIT: Instead of putting DIM Month$(12), you can use DIM Months as STRING, so you don't have to use the dollar signs at the end of the variable name.
Pages: 1 2