Computer Science Canada

Writing in Turing with Arrays

Author:  XeroX [ Sun May 21, 2006 1:39 pm ]
Post subject:  Writing in Turing with Arrays

Is it at all possible to write to a file with an array, as this leads to the error "Put stream number must be int type"

here is my code:
code:
%%%%%%%%%%%%%%%%%%%%%%
%Variable Declaration%
%%%%%%%%%%%%%%%%%%%%%%
var manynumbers : int
var temp1 : string
var temp2 : string
var stremout : int %a var that is an int is needed to open a file

%%%%%%%%%%%%%%%%%%%%%%
%Main Program%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
put "Welcome to the PhoneBook"
put "We will sort your numbers alphabetically and save them to a text file"

put "How many numbers would you like to enter?"
get manynumbers

var phonenumber : array 1 .. manynumbers of string
var phonename : array 1 .. manynumbers of string

for r : 1 .. manynumbers
    put "Please enter the name"
    get phonename (r)
    cls

    put "Please enter the number"
    put skip
    get phonenumber (r)
    cls
end for

%sorting the numbers by alpheabetical order

for i : 1 .. manynumbers
    for decreasing j : manynumbers .. i + 1
        if phonename (j - 1) > phonename (j) then

            temp1 := phonenumber (j - 1)
            temp2 := phonename (j - 1)

            phonenumber (j - 1) := phonenumber (j)
            phonename (j - 1) := phonename (j)

            phonenumber (j) := temp1
            phonename (j) := temp2
        end if
    end for
end for

for q : 1 .. manynumbers
    put phonename (q), "'s number is: ", phonenumber (q)
end for

open : stremout, "phonebook.txt", put %open a connection to send data

for p:1..manynumbers
    put : phonename(p) , "'s number is: ", phonenumber (p)
    exit when p=manynumbers
end for

close : stremout

Author:  XeroX [ Sun May 21, 2006 1:41 pm ]
Post subject: 

please ignore the exit when p=manynumbers

thanks.

Author:  jamonathin [ Sun May 21, 2006 2:29 pm ]
Post subject: 

The first part must be int (i.e something that represents your file)

code:

for p : 1 .. manynumbers
    put : stremout, "'s number is: ", phonenumber (p)
    exit when p = manynumbers
end for

Author:  XeroX [ Sun May 21, 2006 2:58 pm ]
Post subject: 

Thats great, Thanks!


: