Character reading error
Author |
Message |
Coldkick
|
Posted: Wed Jan 19, 2011 12:51 pm Post subject: Character reading error |
|
|
What is it you are trying to achieve?
I am trying to get my program to read the character on the second run of the loop.
What is the problem you are having?
The first character is read correctly, however the second and further are being read as blank and then run through a second time and is read correctly.
Describe what you have tried to solve this problem
A lot of stuff. Put debugging codes throughout the program. Switched character to string, it fixed the problem, but it would be better to use the char variable type IMO.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
var word : string := "cat"
var letter : char := ' '
var found : boolean := false
loop
found := false
put "before loop: ", found
get letter
put letter
for i : 1 .. length (word )
put i
if letter = word (i ) then
found := true
put "entered if part"
elsif i = length (word ) and found = false then
Draw.Oval (250, 250, 100, 100, black)
put "entered elsif part"
end if
end for
put "after loop: ", found
end loop
|
Please specify what version of Turing you are using
Turing 4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Wed Jan 19, 2011 1:29 pm Post subject: RE:Character reading error |
|
|
You're running into a subtle problem with the way Turing handles input into a char variable. Specifically, your second-and-later characters aren't coming as blank, they're coming in as "Carriage Return (CR)" and "Line Feed (LF)", the two characters that make up a "new line" in Windows. The code that reveals this is:
Turing: | put "'", letter, "'" |
If the input was somehow "blank", you would see:
but instead we see:
There are a few ways to fix this, depending on how you want it to work. The most correct way is to just ignore input that is either CR or LF (use the ord function for this: CR and LF have ord values of 10 and 13, so if ord ( letter ) = 10 or ord ( letter ) = 13 then you should skip further processing and go back to the top of the loop.
Yes, Turing can be a little thick about simple input problems. |
|
|
|
|
 |
Coldkick
|
Posted: Wed Jan 19, 2011 3:52 pm Post subject: RE:Character reading error |
|
|
Thanks a ton for the explanation. I was wondering if it was something like \n and I guess I was close. I was trying to help make my friends hangman game more efficient and that was the start of it. My comp sci teacher will love to see this too.  |
|
|
|
|
 |
TokenHerbz

|
Posted: Wed Jan 19, 2011 5:34 pm Post subject: RE:Character reading error |
|
|
turing actually supported a chomp type feature that removes the "enter" (/n) from input.
maybe
code: |
Str.Trim (str:string) : string
|
|
|
|
|
|
 |
TheGuardian001
|
Posted: Thu Jan 20, 2011 1:40 am Post subject: Re: Character reading error |
|
|
DemonWasp wrote: The most correct way is to just ignore input that is either CR or LF
Would the most correct way not be a function designed specifically for character input, such as getchar?
I suppose that does remove the standard "hit enter to confirm" functionality, but it works. |
|
|
|
|
 |
DemonWasp
|
Posted: Thu Jan 20, 2011 2:30 am Post subject: RE:Character reading error |
|
|
I suppose that ultimately depends on his specifications / requirements. In general though, whitespace is so frequently ignored I don't see why that isn't made simple in Turing. |
|
|
|
|
 |
Tony

|
Posted: Thu Jan 20, 2011 10:29 am Post subject: RE:Character reading error |
|
|
But \n is a valid ASCII character (and so is space and tab)... You might want that sometimes. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
DemonWasp
|
Posted: Thu Jan 20, 2011 10:38 am Post subject: RE:Character reading error |
|
|
Yes, you might want that sometimes. Generally, though, you want more or less the interface given by Java's Scanner class: next(), nextInt(), nextDouble(), etc. I would think that a language designed for new programmers would want the simplest option to be the default. Of course, you could also have a raw-get or whatever that captures whitespace, but that wouldn't be the usual use-case. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|