Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line Counter
#1
Hey Guys...Sorry I am asking so much questions its just that I am getting qbasic gradually and Im finding it abit hard to understand some things.
Code:
CLS
PRINT
PRINT "ENTER FILE NAME:";
INPUT FILEN$
CLS
OPEN FILEN$ FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, A$
IF A$ <> "" THEN LN = LN + 1
WEND
CLOSE
IF LN = 1 THEN PRINT "YOUR PROGRAM HAS ONLY 1 LINE OF CODE!"
PRINT "YOUR PROGRAM HAS"; LN; "LINES OF CODE"

I really dont get the middle part and how it works. Hope somone can explain...
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#2
Note, I changed "input #1" to "line input #1" so an entire line is read. I also changed the variable names for better readibility:

Code:
cls
print
print "ENTER FILE NAME:";
input fileName$

open fileName$ for input as #1
while not eof(1)                                        '' while the end hasn't been reached yet ...
    line input #1, tempLine                             ''     read a line from the file
    if tempLine <> "" then lineCount = lineCount + 1    ''     if the line is not empty, increase the line count
wend                                                    '' repeat
close

if lineCount = 1 then
    print "YOUR PROGRAM HAS ONLY 1 LINE OF CODE!"
    end
end if

print "YOUR PROGRAM HAS"; lineCount; "LINES OF CODE"
Don't be sorry - all questions are relevant. Ask if you need more help.
stylin:
Reply
#3
Hey Thanx for that But I still dont get this part...

Code:
if tempLine <> "" then lineCount = lineCount + 1

I dont get how this checks for words in a line!!
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#4
It checks if a line is empty. <> "" means "different from empty".
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
Personally, I would make a function to ignore comments, something like this:
[syntax="qbasic"]
'Remove leading and tailing spaces and making it all lowercase
templine=lcase$(ltrim$(rtrim$(tempLine)))
if tempLine<>"" then
'Checking if tempLine is a comment (beginning with ' or REM) but not a metacommand (beginning with '$)
if (mid$(tempLine,1,1)<>"'" or mid$(tempLine,2,1)="$") and mid$(tempLine,1,3)<>"rem") then
lineCount=lineCount+1
end if
end if[/syntax]
/post]
Reply
#6
Hybrid, incase nathan didn't clear it up...


When you learned math, you were most likely shown these signs: "<", ">", "=", etc. Well, the not-equal to sign that you have in math is not something that you can type easily. So to make a not-equal sign, you need to do this: "<>". So in our line of code:

Code:
if tempLine <> "" then
lineCount = lineCount + 1

It is saying "If templine does not equal anything, then add another line". So if a line in the file does not have anything, it doesn't count it as a line. Do you understand now?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#7
Doesnt help when you dont proofread your posts, Torahteen! :)

Quote:Hybrid, incase nathan didn't clear it up...


When you learned math, you were most likely shown these signs: "<", ">", "=", etc. Well, the not-equal to sign that you have in math is not something that you can type easily. So to make a not-equal sign, you need to do this: "<>". So in our line of code:

Code:
if tempLine <> "" then
lineCount = lineCount + 1


It is saying "If templine does not equal nothing, then add another line". So if a line in the file does not have anything, it doesn't count it as a line. Do you understand now?
Reply
#8
Code:
if tempLine <> "" then lineCount = lineCount + 1
I read it as: "if tempLine compares unequal to an empty string, increment lineCount."
stylin:
Reply
#9
Hey thanx guys for all your help. You all really friendly... Big Grin

But I still got one more problem..........

I get an Error when I want to run the program in QBASIC.

I Get "Type Mismatch"

LINE INPUT #1, tempLine

And it Highlights LINE...

Any Ideas?
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#10
That's cause Templine is ment to be a string, thus you either need a $ at the end of it: Templine$

Or,. somewhere before you use templine (e.g., better put at the top of the code), you can do this:

Code:
DIM templine AS STRING

Which will save you from adding $'s to the end of templine every time you use it..

:wink:
Kevin (x.t.r.GRAPHICS)

[Image: 11895-r.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)