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

Username:   Password: 
 RegisterRegister   
 TURING ~ unexplainable error - HELP
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Memememe




PostPosted: Thu Mar 03, 2016 10:42 pm   Post subject: TURING ~ unexplainable error - HELP

What is it you are trying to achieve?
A short task ive been given is to make a weight grouping program. I must create a program that puts people under 60kg as a lightweight, 60-80 as medium weight, and over 80 as a heavyweight.


What is the problem you are having?
For the most part it works fine. but as soon as it reaches 100 or more, it will only put it as a lightweight.
So far, we can only use the variables string, integer, and real, so nothing other than that if you suggest a change in the main coding


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


Turing:


var kg:string
var cnt:int:=0
loop
cnt:=cnt+1
put "What is your weight? (kg)"
get kg
if kg < "60" then
put "You are a : Light weight"
put ""
put ""
elsif kg > "80" then
put "Your are a : Heavy weight"
put ""
put ""
else
put "You are a : Medium weight"
put ""
put ""
end if
exit when cnt=10
end loop



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




PostPosted: Thu Mar 03, 2016 11:05 pm   Post subject: Re: TURING ~ unexplainable error - HELP

Your problem is that the less than operator (this thing < ) compares numbers in the usual way you would expect, but compares strings lexicographically.
What this means is that for two strings it first compares the first characters, then the second and so on until one character is "larger" than the other (the ordering of characters is this). Notice that the characters for '0', '1', and so forth up to '9' are in the order you would expect.

Basically your program looks at the strings "100" and "60" and checks if '1' is less than '6'. Since the character '1' is "less than" the character '6', the string "100" is deemed to be less than the string "60".

If you change your kg variable to an int type, then get will take the text your user inputs and convert it to a number if possible (there will be an error if the user input non-numerical characters). Then you can compare that to the number 60 (not the string "60") and < will perform the usual numerical comparison.
Memememe




PostPosted: Thu Mar 03, 2016 11:24 pm   Post subject: Re: TURING ~ unexplainable error - HELP

Dreadnought @ Thu Mar 03, 2016 wrote:
Your problem is that the less than operator (this thing < ) compares numbers in the usual way you would expect, but compares strings lexicographically.
What this means is that for two strings it first compares the first characters, then the second and so on until one character is "larger" than the other (the ordering of characters is this). Notice that the characters for '0', '1', and so forth up to '9' are in the order you would expect.

Basically your program looks at the strings "100" and "60" and checks if '1' is less than '6'. Since the character '1' is "less than" the character '6', the string "100" is deemed to be less than the string "60".

If you change your kg variable to an int type, then get will take the text your user inputs and convert it to a number if possible (there will be an error if the user input non-numerical characters). Then you can compare that to the number 60 (not the string "60") and < will perform the usual numerical comparison.

Ah alrighty. Thankyou, it's working now aha
I used the same code as a base for the other ones i made, I figure that a string variable would work the same for this one.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: