Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Error when trying to use the Str.Trim command to count comment lines
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Danyn




PostPosted: Sat Dec 14, 2013 9:42 am   Post subject: Error when trying to use the Str.Trim command to count comment lines

What is it you are trying to achieve?
I have to make a program that counts how many lines contain the keyword that they're looking for as well as counting the total lines and comment lines


What is the problem you are having?
I get an error when using the Str.Trim command


Describe what you have tried to solve this problem
I've tried setting it as a variable and then using the command but I get the same issue

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:


%%%%%%%%%%Functions and Procedures%%%%%%%%%%
fcn KeywordCheck (keyword : string) : boolean
    var stream1 : int
    var data : string
    var flag : boolean := false
    open : stream1, "turingcmds.txt", get
    loop
        exit when eof (stream1)
        get : stream1, data : *
        flag := true
        exit when keyword = data
        flag := false
    end loop
    close : stream1
    result flag
end KeywordCheck

%%%%%%%%%%Variables%%%%%%%%%%
var stream1 : int
var text, fileName, keyword : string
var keywordCheck : boolean := false
var keywordCounter, lineCounter, commentCounter : int := 0

%%%%%%%%%%%Main Program%%%%%%%%%%
put "Please enter the name of the file including the extension."
loop
    get fileName
    exit when File.Exists (fileName) = true
    cls
    put "The file does not exist. Please enter the file again."
end loop

%Asks user for keyword
put "Please enter the keyword you are looking for."
loop
    get keyword
    keywordCheck := KeywordCheck (keyword)
    exit when keywordCheck = true
    cls
    put "Please enter a valid keyword"
end loop

%Open file for get
open : stream1, fileName, get

loop
    exit when eof (stream1)
    get : stream1, text : *
    if index (text, keyword) not= 0 then
        keywordCounter += 1
    elsif Str.Trim (text) (1) = "%" then
        commentCounter += 1
    end if
    lineCounter += 1
end loop
%Close the file
close : stream1

put "Your program contains ", lineCounter, " lines."
put keywordCounter, " lines contain the ", keyword, " keyword."



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sat Dec 14, 2013 10:41 am   Post subject: RE:Error when trying to use the Str.Trim command to count comment lines

In the future, you need to specify what kind of error you're getting.

In this case, I'm assuming it's "Substring index is greater than length of string", which means that Str.Trim(text) is returning an empty string, with length = 0, so trying to get the character at (1) results in an error.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: