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

Username:   Password: 
 RegisterRegister   
 record-write
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Thuged_Out_G




PostPosted: Wed Dec 03, 2003 11:33 am   Post subject: record-write

i have to make a program that will resemble something a school would use...i have to use records to store the persons name, phone #, street, and street number.

all this is easy, but i need to write all the above info to a file, this i dont know how to do... also have to be able to delete, and search the record for a name, and display all inf for that name. heres what i got so far

code:

type studentData :
    record
        name : string
        phone : int
        add : string
        streetNum : int
    end record

var num, num1, phone, choice, streetNum : int
var name, add : string
put "How many students: " ..
get num

var student : array 1 .. num of studentData

proc add1
    for i : 1 .. num
        put "Enter students name: " ..
        get name
        put "Enter Students phone#: " ..
        get phone
        put "Enter students street number: " ..
        get streetNum
        put "Enter students street name: " ..
        get add
        student (i).name := name
        student (i).phone := phone
        student (i).streetNum := streetNum
        student (i).add := add
    end for
    put "Add successful"
end add1

proc remove
    put "Enter student number to remove: "
    get num
    for i : num .. num
        student (i).name := ""
        student (i).phone := 0
    end for
    put "Remove Successful"
end remove

proc search
    put "Enter name to find: " ..
    get name

end search

proc studentList
    put "Student list"
    for i : 1 .. num
        put i, ")name: ", student (i).name
        put " phone: ", student (i).phone
        put " Address: ", student(i).streetNum," ", student (i).add
       
    end for
end studentList

loop
    put "What would you like to do? "
    put "1) Add students"
    put "2) search for a student"
    put "3) Remove students"
    put "4) Show student list"
    get choice
    cls
    loop
        if choice = 1 then
            add1
        elsif choice = 2 then
            search
        elsif choice = 3 then
            remove
        elsif choice = 4 then
            studentList
        end if
        exit when choice > 0
    end loop
end loop
Sponsor
Sponsor
Sponsor
sponsor
McKenzie




PostPosted: Wed Dec 03, 2003 2:43 pm   Post subject: (No subject)

As most languages you can use the file in Text or Binary Mode.
Text
Easy to read and write in text editor (like Turing IDE). Can treat the file like it is the kb or screen (input or output)

Binary
Can read and write large data structures in one line.

I suggest using binary for your problem.

1. Open file for binary write

code:

var fileNo:int
open : fileNo, "payroll", write

2. Write the whole array in one line.
code:
write : fileNo, guys

3. Close your file
code:
close :fileNo


When you want to retrieve the info, open the file for read, rather than write, and use read rather than write.
Chimaera




PostPosted: Wed Dec 03, 2003 2:54 pm   Post subject: (No subject)

you'll have to use a bubble sort and save the information to a second array
Thuged_Out_G




PostPosted: Wed Dec 03, 2003 4:49 pm   Post subject: (No subject)

ok, ive got the writing to a file solved, i decided to go with "put"

but i still dont know how to delete records from the array, or how to search it

and i dont know what a "bubble sort" is either :S

here is an update of my code

code:

type studentData :
    record
        name : string
        phone : int
        add : string
    end record

var fileName : string := "put1.txt"
var fileNo : int
var win := Window.Open ("title:Student Records, position:top,center,notoolbar,graphics:500;500")
open : fileNo, fileName, put
var num, phone, choice : int
var name, add : string
put "How many students: " ..
get num

var student : array 1 .. num of studentData

proc add1
    for i : 1 .. num
        put "Enter students name: " ..
        get name
        put "Enter Students phone#: " ..
        get phone
        put "Enter student address: " ..
        get add : *
        student (i).name := name
        student (i).phone := phone
        student (i).add := add
        put : fileNo, "--------------------------------------------------------"
        put : fileNo, "Student Number: 0000", i
        put : fileNo, "name: ", student (i).name
        put : fileNo, "phone: ", student (i).phone
        put : fileNo, "address: ", student (i).add
        put : fileNo, "--------------------------------------------------------"
        put : fileNo, ""
    end for
    put "Add successful"
end add1

proc remove
    put "Enter student number to remove: "
    get num
    for i : num .. num
        student (i).name := ""
        student (i).phone := 0
    end for
    put "Remove Successful"
end remove

proc search
    put "Enter name to find: " ..
    get name

end search

proc studentList
    put "Student list"
    for i : 1 .. num
        put i, ")name: ", student (i).name
        put " phone: ", student (i).phone
        put " Address: ", student (i).add

    end for
end studentList

loop
    put "What would you like to do? "
    put "1) - Add students"
    put "2) - search for a student"
    put "3) - Remove students"
    put "4) - Show student list"
    put "5) - Exit"
    get choice
    cls
    loop
        if choice = 1 then
            add1
        elsif choice = 2 then
            search
        elsif choice = 3 then
            remove
        elsif choice = 4 then
            studentList
        elsif choice = 5 then
            loop
                Window.Close (win)
            end loop
        end if
        exit when choice > 0
    end loop
end loop
AsianSensation




PostPosted: Wed Dec 03, 2003 11:26 pm   Post subject: (No subject)

if you want to delete something from an array, just shift every single element of the array after it one space up.

ex. an array containing integers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

let's say you want to delete 4 from the array, then just shift everything after 4 one space up. so the array becomes: 1, 2, 3, 5, 6, 7, 8, 9, 10, blank. You can empty out the last element if you want, so you can add other stuffs later on.
Azzy




PostPosted: Thu Dec 04, 2003 8:10 am   Post subject: (No subject)

theres two problems with the code.for the get name you can only input your first name or the program will crash.you can solve that by putting a :* at the end of get name.
second of all.the program also crashes at the phone number if you input your area code.you should either fix it or if you can't just tell the user not to put it in.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: