Computer Science Canada

Max string size

Author:  Mephi [ Sun Apr 27, 2003 2:50 pm ]
Post subject:  Max string size

how do you make it so that if a person puts in more than 256 characters or whatever the limit is it doesn't crash? can you make it so theres no limit?

Author:  Asok [ Sun Apr 27, 2003 2:52 pm ]
Post subject: 

code:
var str : string
put "enter the string"
get str : 6
put str


this for example will only get the first 6 characters in the string

Author:  nate [ Sun Apr 27, 2003 2:56 pm ]
Post subject:  this is how i do it

var text : string (20)

get text : 20

put text

This limits both the variable and the text you can enter. Right now it is 20 but you can change it to the 250 limit

-Nate

Author:  Mephi [ Sun Apr 27, 2003 3:01 pm ]
Post subject:  Umm...

hey nate:P (to both) they dont work.....if you put in a number larger than 255 its an error

Author:  Asok [ Sun Apr 27, 2003 3:09 pm ]
Post subject: 

mephi, are you trying to exceed the 255 cap or make it so that the user doesn't enter into the 255 cap?

Author:  Mephi [ Sun Apr 27, 2003 3:10 pm ]
Post subject:  both

well exceed mainly, but i guess just limiting is ok too....thx....as long as it duznt crash (my comp teachers really stingy, he keeps tryin to crash our programs ne way possible)

Author:  Mephi [ Sun Apr 27, 2003 3:27 pm ]
Post subject:  erm..

code:
var x :string
get x : 8
put x, "!"


how come if its less than 8 the ! goes on a different line? or is this just me?

Author:  Tony [ Sun Apr 27, 2003 3:28 pm ]
Post subject: 

I belive this issue was already addressed by the use of getch() in a loop

code:

var c:string(1) %for getch
var text:string := "" %store text here
var counter:int := 0 %counter

put "enter your string, 255 max"

loop
getch(c)
if c=chr(10) then %if enter is pressed
exit
end if
text := text + c
counter += 1

if counter =255 then %your string is already 255 long
put "I said 255 characters max"
exit
end if

end loop

put "your string is : ", text

Author:  Mephi [ Sun Apr 27, 2003 3:38 pm ]
Post subject:  nvm

nvm i found an easier way:
code:
var x : string
get x
if length (x) > 8 then
    x := x (1 .. 8)
end if
put x, "!"

just takes first 8 characters

Author:  Tony [ Sun Apr 27, 2003 3:42 pm ]
Post subject: 

well you can still type in 300 characters and your program will crash. Using getch will read input 1 key at a time, safeguarding your string variable

Author:  jamez [ Thu May 01, 2003 9:14 am ]
Post subject:  Re: nvm

Mephi wrote:
nvm i found an easier way:
code:
var x : string
get x
if length (x) > 8 then
    x := x (1 .. 8)
end if
put x, "!"

just takes first 8 characters



have 'get x' allows the user to input 1000s of characters


: