qb45advr.hlp (Topic list)
CONST Statement Programming Example
  QuickSCREEN      Details     Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
CONST Statement Programming Example
 
This example uses the CONST statement to declare symbolic constants
for the ASCII values of nonprinting characters such as tab and
line feed. Constants also make programs easier to modify.
 
  Tip: You must supply a text file when you run this example.
       Use a text file you have already created, create a file
       with a text editor, or specify the README.DOC text file.
 
' This program counts words, lines, and characters.
' A word is any sequence of nonblank characters.
DEFINT a-z
 
CONST BLANK = 32, ENDFILE = 26, CR = 13, LF = 10
CONST TABC = 9, YES = -1, NO = 0
 
CLS    ' Clear screen
' Get the file name from the command line.
FileName$=COMMAND$
IF FileName$="" THEN
   INPUT "Enter input file name: ",FileName$
   IF FileName$="" THEN END
END IF
 
OPEN FileName$ FOR INPUT AS #1
 
Words=0
Lines=0
Characters=0
 
' Set a flag to indicate you're not looking at a
' word, then get the first character from the file.
InaWord=NO
DO UNTIL EOF(1)
   C=ASC(INPUT$(1,#1))
   Characters=Characters+1
   IF C=BLANK or C=CR or C=LF or C=TABC THEN
 
' If the character is a blank, carriage return,
' line feed, or tab, you're not in a word.
      IF C=CR THEN Lines=Lines+1
      InaWord=NO
   ELSEIF InaWord=NO THEN
 
' The character is a printing character,
' so this is the start of a word.
' Count the word and set the flag.
      InaWord=YES
      Words=Words+1
   END IF
 
LOOP
PRINT Characters, Words, Lines
END