
-----------------------------------
supaphreek
Wed Jun 02, 2010 11:47 am

Array to string
-----------------------------------
What is it you are trying to achieve?
Im trying to use getchar and convert it to a string


What is the problem you are having?
idk how

Describe what you have tried to solve this problem
Tried the code below...


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)



setscreen ("nocursor")

var a : char
var chars : array char of boolean
var c : array 1 .. 100 of string

for b : 1 .. 100
a := getchar
put a..
c (b) := a
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
exit
end if
end for
put ""
put c




Please specify what version of Turing you are using
4.1.1

-----------------------------------
chrisbrown
Wed Jun 02, 2010 12:14 pm

RE:Array to string
-----------------------------------
Although you can't "put" an array, you can use a for loop to output each character sequentially, or you can append each character to a string:
var s : string := ""    % must be initialized
var c : char
c := getchar   % say user inputs 'x'
s += c
put s        % outputs: x
c := getchar    % user inputs y
s += c
put s      % outputs: xy

-----------------------------------
supaphreek
Wed Jun 02, 2010 1:44 pm

RE:Array to string
-----------------------------------
Well, im trying to make it as a username... so i need the array to be 1 word in a string. how would i do that?

Thx alot tho, i relle appreciate ur help!

-----------------------------------
chrisbrown
Wed Jun 02, 2010 1:53 pm

Re: RE:Array to string
-----------------------------------
i need the array to be 1 word in a string.
I'm not sure what you mean... do you want each element in the array to contain one character? Or each element to contain one username?

-----------------------------------
supaphreek
Wed Jun 02, 2010 2:05 pm

RE:Array to string
-----------------------------------
well, i need to be able to put everything in the array into 1 string....

-----------------------------------
chrisbrown
Wed Jun 02, 2010 2:51 pm

RE:Array to string
-----------------------------------
Ah ok. Unless it's a requirement, you dont need the array at all, you can just append to a string using the technique in the code i provided.

If it is required, my example will still help. After all characters have been read into the array, append each character to a string using a for loop.

Again, if required: Turing will make the conversion for you but you should change c to an array of char instead of string for clarity's sake.
