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

Username:   Password: 
 RegisterRegister   
 Problem with files
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
s3arkay




PostPosted: Fri Jun 16, 2006 8:04 pm   Post subject: Problem with files

Here is my problem guys:

I made a program which lets you enter student names, marks, age, sex ect. My program has to add users, edit users, sort and search.

So when I go to create a newuser I have GUI.Quit in the procedure. when I take this out of the procedure the information does NOT get writen into the text file. It stays in while the program is running but when I close everything down it will not come back (never put into my file) So i keep the GUI.Quit in the procedure so it will write to the file.

But the problem is, my buttons only work once and I want to be able to click them more then once. Understand? Please help
Sponsor
Sponsor
Sponsor
sponsor
s3arkay




PostPosted: Fri Jun 16, 2006 8:05 pm   Post subject: (No subject)

code:
import GUI

%-----
var window : int := Window.Open ("position: middle;middle, title: Marks Manager Deluxe")

var ans : int
var pic : int
var key : string (1)
var NewUserButton, SortButton, DisplayButton, EditButton, SearchButton, ExitButton : int
var font1, font2, font3, font4 : int
var ser, review : string
var names : flexible array 1 .. 0 of string




%------------Declaring the fonts

font1 := Font.New ("Times New Roman:16")
font2 := Font.New ("comic sans ms:12")
font3 := Font.New ("times new roman:12")
font4 := Font.New ("Comic Sans ms:16")

%---Setting the records--------

type name :
    record
        first_name : string
        last_name : string
        age : string
        male_female : string (1)
        mark1 : string
        mark2 : string
        mark3 : string
        mark4 : string
        mark5 : string
        markavg : string
    end record

%---Opening screen--------------------------------

setscreen ("graphics:600;550")
pic := Pic.FileNew ("h:/introscreen.jpg")
Pic.Draw (pic, 1, 1, picCopy)
Font.Draw ("Press any key to continue", 200, 105, font2, black)
getch (key)
cls

%---Registering And login screen---------------------------
var num : int
var count : int := 1
setscreen ("graphics:685;550")
var file : int
open : file, "markssummative.txt", get
setscreen ("graphics:500;380")
pic := Pic.FileNew ("h:/choice.jpg")
Pic.Draw (pic, 1, 1, picCopy)

get : file, num
var clas : array 1 .. 35 of name
loop
    get : file, clas (count).first_name
    get : file, clas (count).last_name
    get : file, clas (count).age
    get : file, clas (count).male_female
    get : file, clas (count).mark1
    get : file, clas (count).mark2
    get : file, clas (count).mark3
    get : file, clas (count).mark4
    get : file, clas (count).mark5
    exit when count = num
    count := count + 1
end loop
close : file



%-------
proc EnterNewUser (id : int)
    var window1 : int := Window.Open ("position: middle;middle, title: Enter new user")
    setscreen ("graphics:685;550")
    pic := Pic.FileNew ("h:/markspage.jpg")
    Pic.Draw (pic, 1, 1, picCopy)
    drawfillbox (168, 95, 342, 120, white)
    Font.Draw ("Average:", 9, 105, font1, black)
    locate (9, 25)
    get clas (id).first_name : *
    locate (11, 25)
    get clas (id).last_name : *
    locate (13, 25)
    get clas (id).age : *
    locate (15, 25)
    get clas (id).male_female : *
    locate (17, 25)
    get clas (id).mark1 : *
    locate (19, 25)
    get clas (id).mark2 : *
    locate (21, 25)
    get clas (id).mark3 : *
    locate (24, 25)
    get clas (id).mark4 : *
    locate (26, 25)
    get clas (id).mark5 : *
    locate (28, 25)
    % clas (id).markavg := (clas (id).mark1 + clas (id).mark2 + clas (id).mark3 + clas (id).mark4 + clas (id).mark5) / 5
    % Font.Draw (clas (id).markavg, 100, 255, font3, black)
    GUI.Quit
    Window.Close (window1)
end EnterNewUser

proc NewUser
    num := num + 1
    EnterNewUser (num)
end NewUser

proc swap (var list : array 1 .. * of string, z, x : int)
    const temp := list (z)
    list (z) := list (x)
    list (x) := temp
end swap

proc bubsort (var list : array 1 .. * of string)
    for x : 1 .. upper (list) - 1
        const last := upper (list) - x + 1
        for q : 1 .. last - 1
            if list (q) > list (q + 1) then
                swap (list, q, q + 1)
            end if
        end for
    end for
end bubsort

proc sorting

    count := 0
    new names, 0 % purge names
    loop

        exit when count = num
        count += 1
        new names, count

        names (count) := clas (count).last_name + ", " + clas (count).first_name + ", " + clas (count).male_female + ", " + clas (count).age

    end loop

    bubsort (names)
    drawfillbox (127, 0, 1000, 1000, black)
    colourback (black)
    colour (white)

    Font.Draw ("           Names are sorted by last name.", 110, 350, font3, white)
    Font.Draw ("          (Last name, first name, sex, age)", 110, 330, font3, white)
    for a : 1 .. upper (names)
        locate (7 + a, 18)
        put names (a)
    end for
    Font.Draw ("Select a option from the list", 150, 15, font2, white)
end sorting


proc displaying
    var window2 : int := Window.Open ("position: middle;middle, title: Enter new user")
    var number : int := 1
    var row : int := 3
    setscreen ("graphics:600;400")
    drawfillbox (0, 370, 1000, 400, green)
    drawfillbox (0, 370, 1000, 100, blue)
    colourback (blue)
    colour (white)
    Font.Draw ("Press any key to continue", 185, 25, font2, red)
    Font.Draw ("Name                            Age     Sex      Mark1   Mark2   Mark3   Mark4   Mark5   Average  ", 5, 380, font3, black)
    for c : 1 .. num
        locate (row, 1)
        put number, " ", clas (c).first_name, " ", clas (c).last_name ..
        locate (row, 20)
        put " ", clas (c).age : 6, clas (c).male_female : 6, clas (c).mark1 : 6, clas (c).mark2 : 7, clas (c).mark3 : 7, clas (c).mark4 : 7, clas (c).mark5 : 4
        if c > 0 then
            row += 1
        end if
        if c > 0 then
            number += 1
        end if
    end for
    getch (key)
    Window.Close (window2)
end displaying

proc editing
    displaying
    drawfillbox (127, 0, 1000, 1000, black)
    Font.Draw ("Which number would you like to edit?", 132, 190, font4, white)
    Font.Draw ("Enter 0 for not editing anybody", 142, 160, font4, white)
    locate (16, 33)
    get ans
    if ans = 0 then
        Font.Draw ("Select a option from the list", 150, 75, font2, white)
    else
        EnterNewUser (ans)
    end if
end editing

proc searching
    var window3 : int := Window.Open ("position: middle;middle, title: Enter new user")
    setscreen ("graphics:600;400")
    pic := Pic.FileNew ("h:/qwerty.jpg")
    Pic.Draw (pic, 1, 1, picCopy)
    Font.Draw ("Enter the Last name you are searching for:", 5, 305, font4, blue)
    Font.Draw ("(case sensative)", 115, 290, font2, blue)
    locate (6, 55)
    get ser
    colourback (14)
    colour (2)
    drawfillbox (0, 100, 1000, 270, 14)
    for g : 1 .. num
        if ser = clas (g).last_name then
            locate (10, 1)
            put "Last name:  ", clas (g).last_name
            put "First name: ", clas (g).first_name
            put "Sex:        ", clas (g).male_female
            put "Mark1:      ", clas (g).mark1
            put "Mark2:      ", clas (g).mark2
            put "Mark3:      ", clas (g).mark3
            put "Mark4:      ", clas (g).mark4
            put "Mark5:      ", clas (g).mark5
        end if
    end for

    Font.Draw ("Press any key to continue", 130, 10, font3, black)
    Font.Draw ("If nothing shows up, no data entry was found", 100, 30, font3, black)
    getch (key)
    Window.Close (window3)
end searching

proc exiting
    setscreen ("graphics:600;500")
    pic := Pic.FileNew ("h:/closing.jpg")
    Pic.Draw (pic, 1, 1, picCopy)
    getch (key)
    Window.Close (window)
end exiting




setscreen ("graphics:500;380")
pic := Pic.FileNew ("h:/choice.jpg")
Pic.Draw (pic, 1, 1, picCopy)

NewUserButton := GUI.CreateButton (10, 260, 20, "Enter New User", NewUser)
SortButton := GUI.CreateButton (10, 220, 20, "Sort Data", sorting)
DisplayButton := GUI.CreateButton (10, 180, 20, "Display Data", displaying)
EditButton := GUI.CreateButton (10, 140, 20, "Edit a User", editing)
SearchButton := GUI.CreateButton (10, 100, 20, "Search", searching)
ExitButton := GUI.CreateButton (10, 30, 20, "Exit", exiting)

loop
    exit when GUI.ProcessEvent
end loop

open : file, "markssummative.txt", put
put : file, num
for c : 1 .. num
    put : file, clas (c).first_name
    put : file, clas (c).last_name
    put : file, clas (c).age
    put : file, clas (c).male_female
    put : file, clas (c).mark1
    put : file, clas (c).mark2
    put : file, clas (c).mark3
    put : file, clas (c).mark4
    put : file, clas (c).mark5
end for
close : file

getch (key)

Window.Close (window)


The h:/ are the pictures from my usb
Bored




PostPosted: Fri Jun 16, 2006 9:21 pm   Post subject: (No subject)

That's the problem, your call GUI.Quit and from then on your loop will call GUI.ProcessEvent and exit. The simple solution to this is to call GUI.ResetQuit. Take this
code:
loop
    exit when GUI.ProcessEvent
end loop

to
code:
loop
    loop
        exit when GUI.ProcessEvent
    end loop
    GUI.ResetQuit
end loop

This will undo what GUI.Quit does and will also reloop the code. Aswell you may want to take a look at this
code:
get : file, num
var clas : array 1 .. 35 of name
loop
    get : file, clas (count).first_name
    get : file, clas (count).last_name
    get : file, clas (count).age
    get : file, clas (count).male_female
    get : file, clas (count).mark1
    get : file, clas (count).mark2
    get : file, clas (count).mark3
    get : file, clas (count).mark4
    get : file, clas (count).mark5
    exit when count = num
    count := count + 1
end loop
close : file

First off you never want to hard code the length of the array, you want to use Flexible arrays which will allow you to add another element for each name in the file. Aswell you do not want to have the number of names at the top, what if someone changes that, or the program miscounts do to a logic error. You then cannot run the program proberply. Insetad you should look up eof() which returns true if the end of a file is reached. your could then replace, exit when
code:
count = num
with
code:
exit when eof (sn)

Third thing, if you are going to do it this way, why not have a for loop. You could instead have
code:
get : file, num
var clas : array 1 .. 35 of name
for i : 1..num
    get : file, clas (count).first_name
    get : file, clas (count).last_name
    get : file, clas (count).age
    get : file, clas (count).male_female
    get : file, clas (count).mark1
    get : file, clas (count).mark2
    get : file, clas (count).mark3
    get : file, clas (count).mark4
    get : file, clas (count).mark5
end for
close : file

But you shouldn't do that but instead look into flexible arrays and eof(). Oh yes one more thing, DO NOT use global variables. clas is a global variable, instead pass it to each procedure as a parameter. You can pass an entire array as a parameter, for example.
code:
proc PutArray (a : array 1..* of string)
    for i : 1..upper (a)
        put a (i)
    end for
end PutArray

var a : array 1..5 of string := init ("a", "ab", "abc", "abcd", "abcde")
PutArray (a)

Now you may be thinking, i want to edit my elements, well that's where the var command comes into play
code:
proc PutArray (a : array 1..* of string)
    for i : 1..upper (a)
        put a (i)
    end for
end PutArray
proc GetArray (var a : array 1..* of string)
    for i : 1..upper (a)
        get a (i) : *    end for
end GetArray

var a : array 1..5 of string
GetArray (a)
PutArray (a)
[/code]
s3arkay




PostPosted: Fri Jun 16, 2006 10:34 pm   Post subject: (No subject)

Dude.... Bored, I owe you big time. Thanks for all the help and you saved my life it seems lol... Thanks man, appreciate it
s3arkay




PostPosted: Sat Jun 17, 2006 8:32 am   Post subject: (No subject)

One problem Bored, my version of turing doesnt have ResetQuit.

i have 4.0.3
Bored




PostPosted: Sat Jun 17, 2006 9:18 pm   Post subject: (No subject)

Well then create one, it's rather simple. All GUI.Quit does is change the variable is call the procedure WidgetModule.Quit, the procedure WidgetModule.Quit is change the variable quit to true. So to reverse this we simply have to change it back to false. The first step is to locate your turing directory. This is most likely under "C:\Program Files\Turing". From there you would find the file gui, which is the code for the gui module. This is under "Turing\Support\lib". So once in there you must create the ResetQuit procedure. The code for this is simply
code:
proc ResetQuit
    WidgetModule.ResetQuit
end ResetQuit

Now we need to export it, so go to the top and you'll find a big list of procedrues being exported, I'd suggest you use CTRL - F to search for Quit and place ResetQuit beside it for neatness sake. Now that we have our GUI.ResetQuit, we need to create the WidgetModule.ResetQuit. To do this open WidgetModule.tu under "Turing\Support\lib\GUIClass". Do the same procedure as above but replace the line
code:
WidgetModule.ResetQuit

with
code:
quitting := false
. remember to export it, and voila your done.[/code]
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: