Records, arrays and redirection .. all in one huge mess!
Author |
Message |
Vlax
|
Posted: Thu May 25, 2006 9:20 pm Post subject: Records, arrays and redirection .. all in one huge mess! |
|
|
Well, basically Im trying to read a little phonebook i made in note pad ... the problem is i cant use my record in my array and read the three variables..(or atleast i dont know how). I've followed instructions .. and nothing really helped except for some stuff on records .. here is what I have so far: code: | %Records - Phonebook program
%Siamak Kiani
%Reads phone numbers and allows the user to search for people by number or name
var sn : int
type Entry :
record
fName : string
lName : string
number : string
end record
var phoneBook : array 1 .. 15 of Entry Entry
open : sn, "list.d", get
for i : 1 .. 15
get : sn, fName.Entry (i)
put fName.Entry (i)
get : sn, lName.Entry (i)
put lName.Entry (i)
get : sn, number.Entry (i)
put number.Entry (i)
end for
|
Im aware that this portion doesnt work:
Quote: code: | open : sn, "list.d", get
for i : 1 .. 15
get : sn, fName.Entry (i)
put fName.Entry (i)
get : sn, lName.Entry (i)
put lName.Entry (i)
get : sn, number.Entry (i)
put number.Entry (i)
end for
|
can someone help me debug it so i can use Entry(i) and store 3 elements(fName, lName, number) into it?
Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Thu May 25, 2006 9:32 pm Post subject: (No subject) |
|
|
well, your main problem is that you don't have an array of entries, you have an array of the variables that the type "Entry" contains (If that made any sense to you. ) Basically, the type "Entry" is a custom variable declaration, much like "boolean" The correct syntax would be:
code: | phonebook(i).fName
phonebook(i).lName
%etc. |
the reason being is phonebook is your variable type, and you have it declared as an array, therefore, you need to access element (i) of phonebook. Phonebook has more than one field, those being fName, lName, etc. so you need to tell turing which field you want to access.
Read the tutorial on this stuff in the Tutorials section! |
|
|
|
|
![](images/spacer.gif) |
Vlax
|
Posted: Thu May 25, 2006 9:34 pm Post subject: (No subject) |
|
|
you may have just given me the knowledge to finish this ISU ... Thanks! |
|
|
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Thu May 25, 2006 9:39 pm Post subject: (No subject) |
|
|
No problem, 'swhat i'm here for. Thank you for using code tags and posting in the right section. (I assume that means you read the rules before posting. You're probably the first of your kind ) |
|
|
|
|
![](images/spacer.gif) |
Vlax
|
Posted: Mon May 29, 2006 11:34 am Post subject: (No subject) |
|
|
Heres the finished product, but u need a data file...
code: |
%Records - Phonebook program
%Siamak Kiani
%Reads phone numbers and allows the user to search for people by number or name
var target : string
var sorted : boolean
var last : int := 15
var found : boolean := false
var choice : string (1)
var sn, location : int
var firsthalf, secondhalf, midpoint : int
firsthalf := 1
secondhalf := 15
type Entry :
record
fName : string
lName : string
number : string
end record
var phoneBook : array 1 .. 15 of Entry
open : sn, "list.d", get
for i : 1 .. 15
get : sn, phoneBook (i).fName
get : sn, phoneBook (i).lName
get : sn, phoneBook (i).number
end for
for i : 1 .. 15
sorted := true
for j : 1 .. 14
if phoneBook (j).lName > phoneBook (j + 1).lName then
sorted := false
const temp := phoneBook (j)
phoneBook (j) := phoneBook (j + 1)
phoneBook (j + 1) := temp
end if
end for
exit when sorted
end for
for i : 1 .. 15
put phoneBook (i).fName, " " ..
put phoneBook (i).lName, " " ..
put phoneBook (i).number
end for
proc lNamesearch
put "Input a last name."
get target
loop
midpoint := (firsthalf + secondhalf) div 2
if phoneBook (midpoint).lName = target then
exit
elsif phoneBook (midpoint).lName > target then
secondhalf := midpoint - 1
elsif phoneBook (midpoint).lName < target then
firsthalf := midpoint + 1
end if
if firsthalf > secondhalf
then
put "Not found."
exit
end if
end loop
put "The person is:"
put phoneBook (midpoint).fName, " " ..
put phoneBook (midpoint).lName, " " ..
put phoneBook (midpoint).number
end lNamesearch
proc numbersearch
put "Input a number."
get target
for i : 1 .. 15
if phoneBook (i).number = target
then
found := true
location := i
exit
end if
end for
if found then
put "This is the person you are looking for:"
put phoneBook (location).fName, " " ..
put phoneBook (location).lName, " " ..
put phoneBook (location).number
else
put "Not on this list, sorry!"
end if
end numbersearch
put "Would you like to search by last name or phone number?"
put "Input 1 for last name search."
put "Input 2 for number search."
get choice
if choice = "1" then
lNamesearch
elsif choice = "2" then
numbersearch
else
end if
|
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon May 29, 2006 12:32 pm Post subject: (No subject) |
|
|
Functions are your friends. Imagine:
code: | for i : 1 .. 15
get : sn, phoneBook (i).fName
get : sn, phoneBook (i).lName
get : sn, phoneBook (i).number
end for |
As:
code: | function get_entry_from_file (file : int) : Entry
var temp : Entry
get : file, temp.fName
get : file, temp.lName
get : file, temp.number
result temp
end get_entry_from_file
for i : 1 .. 15
phoneBook (i) := get_entry_from_file (sn)
end for |
Oh, and I would strongly advise you not to use magic numbers (15) in your code (if Turing allows you to get around this). Instead use constants. That way, if you wish to change the size of the array, you need only change one constant, rather than making changes throughout your code. |
|
|
|
|
![](images/spacer.gif) |
|
|