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

Username:   Password: 
 RegisterRegister   
 Database
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jekate




PostPosted: Fri Jun 22, 2007 5:57 pm   Post subject: Database

Hello everyone.

I'm having some problems with a database program I'm writing. Here is what I have so far:

code:
var users, choice : int
users := 0
var ip_array : array 0 .. users of string

loop
    put "Enter a choice:"
    put "1 - Enter new user."
    put "2 - Print list of users."
    put "3 - Exit."
    get choice
    if choice = 1 then
        cls
        put "Enter the IP address:"
        get ip_array (users)
        users += 1
    elsif choice = 2 then
        cls
        put "There are ",users," many user(s) in the database."
        for i : 0 .. users
            put ip_array (i)
        end for
    elsif choice = 3 then
        exit
    end if
end loop


Now what I am trying to do is whenever I choose to add a new user, the array size gets bigger so I can keep adding users. The program comes to a halt when it tries to print what I've entered. I'm not sure what's wrong. I get an error "Array subscript is out of range." I'm not sure what that means. Thanks for any help.
Sponsor
Sponsor
Sponsor
sponsor
Saad




PostPosted: Fri Jun 22, 2007 6:08 pm   Post subject: Re: Database

because you declared a static array you cannot change the size of the array and the number that your trying to access is out of its bounds. I would recomend reading about flexible arrays as this will provide the solution

or just replace
code:
var ip_array : array 0 .. users of string

with
code:
var ip_array :flexible array 0 .. users of string


code:
        get ip_array (users)
        users += 1

with
code:

        get ip_array (users)
        users += 1
        new ip_array,users
Jekate




PostPosted: Sat Jun 23, 2007 7:43 am   Post subject: Re: Database

Alright, thanks for the help. But I am still receiving an error when it tries to print it. I can add more then 1 entry into the database, and it does print them, but when after it is finished printing is when the error occurs. This is what I have now:

EDIT: Ok, I tried something and it seems to be working. I added the -1 to the
code:
for i : 0 .. (users - 1)


And I've added a bit more.

code:
setscreen ("nocursor")

var users, choice : int
users := 0
var ch : string (1)
var ip_array : flexible array 0 .. users of string
var name_array : flexible array 0 .. users of string

loop
    put "Enter a choice:"
    put "1 - Enter new user."
    put "2 - Print list of users."
    put "3 - Number of users in database."
    put "4 - Exit."
    get choice
    if choice = 1 then
        cls
        put "Enter the name:"
        get name_array (users)
        put "Enter the IP address:"
        get ip_array (users)
        users += 1
        new name_array, users
        new ip_array, users
    elsif choice = 2 then
        cls
        for i : 0 .. (users - 1)
            put name_array (i), " - ", ip_array (i)
        end for
        getch (ch)
        cls
    elsif choice = 3 then
        cls
        put "There are ", users, " many user(s) in the database."
        getch (ch)
        cls
    elsif choice = 4 then
        exit
    end if
end loop


What I eventually want to be able to have the program do is be able to press a number, and it save the current list to a file. Also, for the program to be able to load the file, and be able to print it, as if I just entered it into the program. I also would like it so that when I add more users to the list, that it add it do the file when I save it, and not overwrite the current users in the save file. I'm not sure if that's possible but if so, that's what I'm gearing towards.

Oh, and a search function. If it's possible to be able to search for either names or IP's already entered in the database, to be able to search for them and bring up that info.
DIIST




PostPosted: Sat Jun 23, 2007 9:31 am   Post subject: Re: Database

Just use upper(ARRAY) - lower(ARRAY) commands to keep track of the number of elements in the array. As for you database you should try implementing a linklist if this is one of your own projects, not something due for school. It will be very easy with a linked list to add and delete users and even print them.
Jekate




PostPosted: Sat Jun 23, 2007 11:13 am   Post subject: Re: Database

Okay. You're going to have to explain the upper(ARRAY) and lower(ARRAY) as well as the linked list things. I'm not sure what they are. And yes, this is something I'm doing for myself. I'm done school.
DIIST




PostPosted: Sat Jun 23, 2007 11:22 am   Post subject: Re: Database

Jekate @ June 23rd 2007 wrote:
Okay. You're going to have to explain the upper(ARRAY) and lower(ARRAY) as well as the linked list things. I'm not sure what they are. And yes, this is something I'm doing for myself. I'm done school.




Turing:

var some_Array:array -2..2 of int

put lower (some_Array)
put upper (some_Array)

put "There are " , upper(some_Array) - lower (some_Array) + 1," elements in the array"


As for the linked list there should be a tutorial somewhere. It should be under a pointer lesson.
Wink
Jekate




PostPosted: Sat Jun 23, 2007 1:59 pm   Post subject: Re: Database

Ok, I'm not sure exactly how to implement the upper(array) and lower(array) yet. Are you trying to say that I can increase the size of the array with the upper(array) command? What you showed me just shows me that the upper(array) and lower(array) shows me what the limits of the array are, which I understand. But I'm not sure if I can edit these values. If so, could you explain how.

I'm still looking into the linked lists. I found a tutorial here about them, but it's going to be a bit before I understand those.

Thanks for the help, and here is what I've added so far. I can't load for some reason.
code:
setscreen ("nocursor")

var users, choice, data : int
users := 0
var ch : string (1)
var ip_array : flexible array 1 .. users of string
var name_array : flexible array 1 .. users of string

loop
    put "Enter a choice:"
    put "1 - Enter new user."
    put "2 - Print list of users."
    put "3 - Number of users in database."
    put "4 - Save list to file."
    put "5 - Load list from file."
    put "6 - Exit."
    get choice
    cls
    if choice = 1 then
        users += 1
        new name_array, users
        new ip_array, users
        put "Enter the name:"
        get name_array (users)
        put "Enter the IP address:"
        get ip_array (users)
        cls
    elsif choice = 2 then
        cls
        for i : 1 .. (users)
            put name_array (i), " - ", ip_array (i)
        end for
        getch (ch)
        cls
    elsif choice = 3 then
        cls
        put "There are ", users, " many user(s) in the database."
        getch (ch)
        cls
    elsif choice = 4 then
        open : data, "Database.txt", put
        put : data, users
        for e : 1 .. (users)
            put : data, name_array (e), " ", ip_array (e)
        end for
        close : data
        cls
    elsif choice = 5 then
        open : data, "Database.txt", get
        get : data, users
        for u : 1 .. (users)
            get : data, name_array (u), ip_array (u)
        end for
        close : data
        cls
    elsif choice = 6 then
        exit
    end if
end loop
Jekate




PostPosted: Sat Jun 23, 2007 4:55 pm   Post subject: Re: Database

EDIT:

Ok, I'm done. Program is finished unless I think of something later I want to add. Thanks for your help.

code:
var users, choice, data : int
users := 0
var ch : string (1)
var search : string
var ip_array : flexible array 1 .. users of string
var name_array : flexible array 1 .. users of string

loop
    put "Enter a choice:"
    put "1 - Enter new user."
    put "2 - Print list of users."
    put "3 - Number of users in database."
    put "4 - Save list to file."
    put "5 - Load list from file."
    put "6 - Search for an existing user."
    put "7 - Exit."
    get choice
    cls
    if choice = 1 then
        users += 1
        new name_array, users
        new ip_array, users
        put "Enter the name:"
        get name_array (users)
        put "Enter the IP address:"
        get ip_array (users)
        cls
    elsif choice = 2 then
        cls
        for i : 1 .. users
            put name_array (i), " - ", ip_array (i)
        end for
        getch (ch)
        cls
    elsif choice = 3 then
        put "There are ", users, " many user(s) in the database."
        getch (ch)
        cls
    elsif choice = 4 then
        open : data, "Database.txt", put
        put : data, users
        for i : 1 .. users
            put : data, name_array (i), " ", ip_array (i)
        end for
        close : data
        cls
    elsif choice = 5 then
        open : data, "Database.txt", get
        get : data, users
        for i : 1 .. users
            new name_array, users
            new ip_array, users
            get : data, name_array (i), ip_array (i)
        end for
        close : data
        cls
    elsif choice = 6 then
        put "Enter a name or IP you wish to search for."
        get search
        cls
        for i : 1 .. users
            if search = name_array (i) or search = ip_array (i) then
                put "Match found."
                put "IP or name searched for: ", search
                put "Matching IP or name:"
                put name_array (i), " - ", ip_array (i)
            end if
        end for
        getch (ch)
        cls
    elsif choice = 7 then
        exit
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Jekate




PostPosted: Sun Jun 24, 2007 12:10 am   Post subject: Re: Database

I'm wondering if I can make my job easier by making the program query a server and retrieve a list of IP's and names. Is that even possible with turing?
Andy




PostPosted: Sun Jun 24, 2007 2:56 am   Post subject: RE:Database

ya search up turing + php on the forums
Jekate




PostPosted: Sun Jun 24, 2007 9:37 am   Post subject: Re: Database

Alright, I've been looking it up on forums, but not exactly sure what I need to be looking for from the search.
d2bb




PostPosted: Mon Jul 09, 2007 10:08 pm   Post subject: Re: Database

Jekate @ Sun Jun 24, 2007 9:37 am wrote:
Alright, I've been looking it up on forums, but not exactly sure what I need to be looking for from the search.





this program sound lot like a "virus". Smile




also, should have ending code of


else choice not = 1-8... then
put "that was not a choice."





just tips Wink
Jekate




PostPosted: Sun Jun 15, 2008 10:35 pm   Post subject: Re: Database

Alright. Bringing back an old topic, yes I know.

Anyways, I'm bored and want to expand on this program. I'm going to be looking in linked lists as stated before.

Also, when I enter anything else but an integer for the choice, I get an error, how do I stop this from happening? Is there a variable that contains both strings, integers, floats, etc. that I can use?

When I search for text, or an IP, I have to enter the exact string for the program to find it. How can I make it so if I search for a 3 letter string such as 'hey', that it will find anything with the string 'hey' in the word?
Tony




PostPosted: Sun Jun 15, 2008 10:42 pm   Post subject: Re: Database

Jekate @ Sun Jun 15, 2008 10:35 pm wrote:
Also, when I enter anything else but an integer for the choice, I get an error, how do I stop this from happening? Is there a variable that contains both strings, integers, floats, etc. that I can use?

A string will accept anything as a string. You can then typecast it into other types with strint(), strreal(). Make sure to check if that would actually work, with intstrok(), etc, else you might get run-time errors.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
[Gandalf]




PostPosted: Mon Jun 16, 2008 12:10 am   Post subject: RE:Database

And to find a certain substring within a string, you can use the index() function:
code:
put index ("fdaheyfdaf", "hey")

This will output 4. If "hey" was not in the main string, it would return 0.
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 3  [ 31 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: