Computer Science Canada

Strings and Spaces

Author:  CentiZen [ Wed May 12, 2010 11:53 am ]
Post subject:  Strings and Spaces

I've been wrestling with this problem all day here. Ever time I try to use an if statement or a case statement in turing with a space in it, it dosen't work. For example, see the following code:

code:
var test : string
get test
if test = "Hello Computer" then
    put "Hello Michael"
elsif test = "HelloComputer" then
    put "HelloMichael"
else
    put "it didn't work!"
end if


So, typing "Hello Computer" should output "Hello Michael" but instead it outputs "it didn't work!". However, typing "HelloComputer" will (properly) output "HelloMichael".

A similar problem is encountered when using case statements. See the following code:

code:
var test : string
get test
case test of
    label "hello computer" : put "Hello Michael"
    label "hellocomputer" : put "HelloMichael"
end case


Like in the last one, typing "hellocomputer" outputs "HelloMichael", but typing "hello computer" causes the program to crash with the error Case selector is out of range

I've tried this code on both turing 4.1.1 and 4.1.1a versions, both have the same problem. Has anyone else encountered this? Is turing known to have this issue (google was no help)? Or is this an inherent problem in every programming language and I just haven't noticed it untill now?

Thanks for the help.

Michael

Author:  DemonWasp [ Wed May 12, 2010 12:07 pm ]
Post subject:  RE:Strings and Spaces

Try putting the following line before your if statement:
code:
put test


You'll probably notice that it says "Hello" and not "Hello Computer". This is because Turing accepts input token-by-token, with whitespace as a delimiter. It reads the first token (Hello) and not the second one (World).

To get the entire line, use:
code:
get test : *

Author:  Tony [ Wed May 12, 2010 12:51 pm ]
Post subject:  Re: Strings and Spaces

CentiZen @ Wed May 12, 2010 11:53 am wrote:
but typing "hello computer" causes the program to crash with the error Case selector is out of range

Just like you've had an "else" part in your if-statement; it might be a good idea to have a default catch-all clause to labels, in case someone types in something unexpected
code:

case test of
    label "hello computer" : put "Hello Michael"
    label "hellocomputer" : put "HelloMichael"
    label : put "unknown case for: ", test
end case


: