Computer Science Canada TURING ~ unexplainable error - HELP |
Author: | Memememe [ 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)
Please specify what version of Turing you are using Turing 4.1.1 |
Author: | Dreadnought [ 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. |
Author: | Memememe [ 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. |