Computer Science Canada

strint with Arrays?

Author:  Flikerator [ Sat Mar 05, 2005 5:25 pm ]
Post subject:  strint with Arrays?

I want to convert my array of string to an array of int. Can I do that?

code:

var file : int
var numstring : array 1 .. 6 of string
var nums : array 1 .. 6 of int

open : file, "DATA1.txt", get

for i : 1 .. 6
    get : file, numstring (i)
end for

for i : 1 .. 6

end for


Convert where the second for statement is, I put it in there incase its needed. You can take it out if its not needed.

Author:  Cervantes [ Sat Mar 05, 2005 6:06 pm ]
Post subject: 

Sure you can do that. Smile
code:

var numstring : array 1 .. 6 of string
var nums : array 1 .. 6 of int

for i : 1 .. 6
    numstring (i) := intstr (Rand.Int (-100, 100))
end for
for i : 1 .. 6
    nums (i) := strint (numstring (i))
end for
for i : 1 .. 6
    put nums (i)
end for


However, let's improve on that a bit, using the strintok() function.
code:

var numstring : array 1 .. 30 of string
var nums : array 1 .. 30 of int

for i : 1 .. 30
    numstring (i) := chr (Rand.Int (40, 60))
end for
for i : 1 .. 30
    if strintok (numstring (i)) then
        nums (i) := strint (numstring (i))
    else
        nums (i) := -1
    end if
end for
for i : 1 .. 30
    put nums (i)
end for

This just prevents the program from crashing.

Author:  Flikerator [ Sat Mar 05, 2005 6:14 pm ]
Post subject: 

code:
var file : int
var numstring : array 1 .. 6 of string
var nums : array 1 .. 6 of int

open : file, "DATA1.txt", get

for i : 1 .. 6
    get : file, numstring (i)
end for

for i : 1 .. 6
    nums (i) := strint (numstring (i))
end for



Overflow in result of strint

Because the value is "12345678987654321"

Is there anything I can do?

Author:  Cervantes [ Sat Mar 05, 2005 6:37 pm ]
Post subject: 

Use real variables, instead of integers.
That, or you could break the number up into different parts. But that's just too much extra work. Wink


: