String length limit?
Author |
Message |
Fenrisulfr
![](http://compsci.ca/v3/uploads/user_avatars/177336184749bf265884756.jpg)
|
Posted: Mon Mar 23, 2009 2:57 pm Post subject: String length limit? |
|
|
Ok I know I have lota questions but... project due
when declare a variable as string, and get the variable, if the person is typing randomly and entered more than 255 characters the program will crash
Turing: | var name : string
get name:* |
so, I tried to put a bracket after string
Turing: | var name : string (80)
get name:* |
but, it still crashes if too many characters are entered. Is there a way to make sure that if too many characters are entered, only the first... lets say 80 characters will be read? (spaces included)
thanks! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
saltpro15
![](http://compsci.ca/v3/uploads/user_avatars/9776171634ced6278d14c2.png)
|
Posted: Mon Mar 23, 2009 3:01 pm Post subject: RE:String length limit? |
|
|
check if the length of the string is bigger than 80
you will need to look up the length command in the help file
then use an if loop to check if the length is bigger than 80, and if it is, do something |
|
|
|
|
![](images/spacer.gif) |
BigBear
|
Posted: Mon Mar 23, 2009 3:09 pm Post subject: RE:String length limit? |
|
|
OK the : * after get is saying get all characters
You could use :80 to get the first 80 characters this will prevent the length becoming more than 256 characters and crashing.
Besides your teacher should not care too much about this flaw in turing.
Also DemonWasp made a string class (like turings built in one) which accepts long strings.
http://compsci.ca/v3/viewtopic.php?t=20229 |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Mar 23, 2009 3:19 pm Post subject: RE:String length limit? |
|
|
Unfortunately, my String class doesn't have any method for getting a string from the console. It has the ability to read such a string from a file or output it to a file, but nothing with live user-input.
You could emulate the built-in functionality with a loop, my String class, Text.Locate and some creative handling of the backspace / arrow keys, but it may prove tricky.
Turing doesn't support strings (built-in strings, that is) beyond 255 characters. Why do you need to test for this or support this? You can probably prevent the crash by using the length of 255 on your get command, as it will cut off user-input after that many characters. |
|
|
|
|
![](images/spacer.gif) |
BigBear
|
Posted: Mon Mar 23, 2009 3:26 pm Post subject: RE:String length limit? |
|
|
If you hold down a key it will max eventually not sure how many characters.
As long as you have Turing: | get name : 255%or lower
| it will work |
|
|
|
|
![](images/spacer.gif) |
Fenrisulfr
![](http://compsci.ca/v3/uploads/user_avatars/177336184749bf265884756.jpg)
|
Posted: Mon Mar 23, 2009 9:18 pm Post subject: Re: String length limit? |
|
|
seems to work, but when I try to put it after getting it
Turing: |
put "Welcome, ", name, "! etc etc"
|
no matter how short my input for "name" is, for example Ted, the outout will always skip to the next line after name
so it becomes
Welcome, Ted
! etc etc
I don't suppose theres a way to make the string length as long as what the user inputted, but still only taking the first 80 or so with spaces included? |
|
|
|
|
![](images/spacer.gif) |
BigBear
|
Posted: Mon Mar 23, 2009 10:32 pm Post subject: RE:String length limit? |
|
|
You could use a loop with getch(ch) then add ch to word. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Tue Mar 24, 2009 1:54 am Post subject: RE:String length limit? |
|
|
It seems to me that the reason you have that newline in there is because the :80 forces get to take in the \n character too. If you loop over the characters in the string and output their ord values:
Turing: |
var name : string
get name : 80
put "a ", name, " b"
for i : 1 .. length ( name )
put ord ( name ( i ) )
end for
|
So for the input "Fenris", you get:
code: |
Fenris
a Fenris
b
70
101
110
114
105
115
10
|
Well, 70 happens to be in the upper-case characters in ASCII, and the 100-115 range is within lower-case characters. But what's that 10? Well, on Windows, newline is carriage (sp?) return, line feed - meaning values 10, then 13. Apparently it's putting one of those on your string!
You can use the Turing command substring to fix this problem. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Tue Mar 24, 2009 5:55 am Post subject: Re: String length limit? |
|
|
I know This may be kinda rude (sorry if your really offended...) but, are you just asking us to help you with every part of your project or o you just have a bad computer teacher.
even my computer teacher could help you with this. I just am asking this because you have alot of help topics for basic thing that your computer teacher would probably help you with if you asked for help. Like for the intstr he should have taught you this... Also I'm not saying there's anything wrong with you posting your topics for help thats good. Just wondering why your not learning these basic things from your teacher... |
|
|
|
|
![](images/spacer.gif) |
Fenrisulfr
![](http://compsci.ca/v3/uploads/user_avatars/177336184749bf265884756.jpg)
|
Posted: Tue Mar 24, 2009 6:10 am Post subject: Re: String length limit? |
|
|
Not offended lol even I know I asked way too many questions that may seem simple. The problem is whenever I ask my comp teacher, he tells me some random stuff that either makes no sense or doesnt work... had to do a bit of self learning
Oh and... thank you very much! the substring worked like a charm. Promise that'll be the last of my questions for a while since I'm handing in today |
|
|
|
|
![](images/spacer.gif) |
|
|