Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 strint with Arrays?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat Mar 05, 2005 6:06 pm   Post subject: (No 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.
Flikerator




PostPosted: Sat Mar 05, 2005 6:14 pm   Post subject: (No 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?
Cervantes




PostPosted: Sat Mar 05, 2005 6:37 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: