Array error , please help.

New to FreeBASIC? Post your questions here.
gatorkea
Posts: 8
Joined: Feb 19, 2015 22:40

Array error , please help.

Postby gatorkea » Apr 01, 2015 13:18

I am getting this error message in the editor,
"FbTemp.bas(53) error 71: Array not dimensioned, before '(' in 'If namStr(i) = oneNam Then'"

namStr is Dim'd so im not sure why its not working, any help is appreciated. thanks.




'Phone number lookup , contains names and phone numbers

Dim f As Integer
Dim people(1 To 3) As String
Dim phoneNum(1 To 3) As String
Dim fileName As String
Dim namStr As String
Dim count As Integer = 1
Dim pnum As Integer
Dim i As Integer
Dim oneNam As String
Dim found As integer = -1

f = FreeFile

Open "project_3.txt" For Output As #f 'opens file for output to text file


For index As Integer = 1 To 3 'ask's user to input names and numbers to text file
Input "Enter the name: ",people(3)
Input "Enter the number: ",phoneNum(3)
Write #f, people(3), phoneNum(3) '
Next

Close #f

Input "What is the File's name?", filename

If(Open (filename For Input As #f)) Then 'opens file for input
Print "ERROR: opening file "
Sleep
End -1
EndIf '


Do Until Eof( f ) 'outputs all names and numbers from array onto the screen
Input #f , namStr, pnum
Print " Name: ", namStr & " Number: ",pnum
Sleep
Print
Loop '

Close #f

Print
Print
Print
Input "Who are you looking for?", oneNam


For i As Integer =1 To count-1 'linear search
If namStr(i) = oneNam Then
found = i
EndIf
Next

If found = -1 Then
Print "name not found!"
Else
Print "Name found at position "; found
EndIf
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Re: Array error , please help.

Postby Merick » Apr 01, 2015 13:29

Because "Dim namStr As String" is not an array
gatorkea
Posts: 8
Joined: Feb 19, 2015 22:40

Re: Array error , please help.

Postby gatorkea » Apr 01, 2015 14:07

well that was embarrasing :P
gatorkea
Posts: 8
Joined: Feb 19, 2015 22:40

Re: Array error , please help.

Postby gatorkea » Apr 01, 2015 14:27

well the program runs now, but when it get to the part where it ask's "Who are you looking for?" the program just closes , no clue why. It works in another program I wrote that is similar
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array error , please help.

Postby fxm » Apr 01, 2015 14:31

Sleep does not clear the keyboard buffer and any keys pressed during a call to Sleep are retained and can be read using Inkey, Getkey or Input.

First solution, empty the keyboard buffer after the Sleep(s) with:
While Inkey <> ""
Wend

Code: Select all

Dim count As Integer = 3

Dim f As Integer
Dim people(1 To count) As String
Dim phoneNum(1 To count) As String
Dim fileName As String
Dim namStr(1 To count) As String
Dim pnum(1 To count) As Integer
Dim i As Integer
Dim oneNam As String
Dim found As integer = -1

f = FreeFile

Open "project_3.txt" For Output As #f 'opens file for output to text file
For index As Integer = 1 To count 'ask's user to input names and numbers to text file
  Input "Enter the name: ",people(index)
  Input "Enter the number: ",phoneNum(index)
  Write #f, people(index), phoneNum(index) '
Next
Close #f

Input "What is the File's name?", filename
If(Open (filename For Input As #f)) Then 'opens file for input
  Print "ERROR: opening file "
  Sleep
  End -1
End If '

i = 1
Do Until Eof( f ) 'outputs all names and numbers from array onto the screen
  Input #f , namStr(i), pnum(i)
  Print " Name: ", namStr(i) & " Number: ",pnum(i)
  Sleep
  i = i + 1
  Print
Loop
While Inkey <> ""
Wend

Close #f

Print
Print
Print
Input "Who are you looking for?", oneNam

For i As Integer =1 To count 'linear search
  If namStr(i) = oneNam Then
  found = i
  End If
Next

If found = -1 Then
  Print "name not found!"
Else
  Print "Name found at position "; found
End If

Second solution, use Getkey (which removes the key from the buffer) instead of Sleep.

Code: Select all

Dim count As Integer = 3

Dim f As Integer
Dim people(1 To count) As String
Dim phoneNum(1 To count) As String
Dim fileName As String
Dim namStr(1 To count) As String
Dim pnum(1 To count) As Integer
Dim i As Integer
Dim oneNam As String
Dim found As integer = -1

f = FreeFile

Open "project_3.txt" For Output As #f 'opens file for output to text file
For index As Integer = 1 To count 'ask's user to input names and numbers to text file
  Input "Enter the name: ",people(index)
  Input "Enter the number: ",phoneNum(index)
  Write #f, people(index), phoneNum(index) '
Next
Close #f

Input "What is the File's name?", filename
If(Open (filename For Input As #f)) Then 'opens file for input
  Print "ERROR: opening file "
  Sleep
  End -1
End If '

i = 1
Do Until Eof( f ) 'outputs all names and numbers from array onto the screen
  Input #f , namStr(i), pnum(i)
  Print " Name: ", namStr(i) & " Number: ",pnum(i)
  Getkey
  i = i + 1
  Print
Loop

Close #f

Print
Print
Print
Input "Who are you looking for?", oneNam

For i As Integer =1 To count 'linear search
  If namStr(i) = oneNam Then
  found = i
  End If
Next

If found = -1 Then
  Print "name not found!"
Else
  Print "Name found at position "; found
End If
speedfixer
Posts: 391
Joined: Nov 28, 2012 1:27
Location: California

Re: Array error , please help.

Postby speedfixer » Apr 01, 2015 19:20

@gatorkea:

After the other syntax was cleaned up, your program still failed after the 'if (open ...) statement.
THAT statement, in that form, was causing the problem.
In Linux, I get a segfault. fxm's code is good.


@fxm:

why?

The examples on the wiki do not suggest any need for syntax other than:

If (Open filename For Input As #f) Then 'opens file for input

This produces:

error 6: Expected '(', found 'filename' in 'If (Open filename For Input As #f) <> 0 Then 'opens file for input'


david@CursedLU64:/rdkl/FB/examples/scratchpad$ fbc --version
FreeBASIC Compiler - Version 1.02.0 (02-06-2015), built for linux-x86 (32bit)


David
dodicat
Posts: 6156
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array error , please help.

Postby dodicat » Apr 01, 2015 19:32

Typed that so many times for the error.
In fact, every time I typed it I got it wrong.
I remind myself now that OPEN is always outside the brackets -- OPEN to the world, and not imprisoned in the brackets.
gatorkea
Posts: 8
Joined: Feb 19, 2015 22:40

Re: Array error , please help.

Postby gatorkea » Apr 01, 2015 21:36

quick question, in order for the program to display the persons phone number from the search... do I need to sort the array?
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array error , please help.

Postby fxm » Apr 01, 2015 21:48

Remark about the syntax of OPEN when it is used as a function

The documentation specifies one syntax using commas to separate different fields, but syntax without commas works also and I think most people use it also (without commas)!

What is it really the truth?
gatorkea
Posts: 8
Joined: Feb 19, 2015 22:40

Re: Array error , please help.

Postby gatorkea » Apr 02, 2015 20:24

Ok, heres another variation of the program, I am working on. Could anyone shed some light on my issue with Sub perimeters? I know its the reason my program doesnt do what I want it to do. It just always returns 'name not found' , You dont have to modify my code, I just want to understand how to do this. thanks

[code=vb file=Untitled.bas]'ReadIntoArray.bas
'reading strings into an array and then searching for a name

'declare variables
Dim file As Integer ' our file handle
Dim filename As String ' the actual name of the input
Dim namStr(1 To 100) As String 'made it bigger than it needs to be
Dim numStr(1 To 100) As String
Dim count As Integer = 1 ' the number of names in the array
Dim oneNam As String 'one name
Dim i As Integer 'use as index to the array
Dim found As integer = -1 ' a flag variable holding where you found
' the name, -1 means not found

Declare Sub searchName(oneNam As String , namStr() As String , found As Integer, i As Integer)

'mainline

Input "What is the file's name?" ,filename

file = FreeFile

'attempt to open that file
If( Open(filename For Input As #file) ) Then
Print "ERROR: could not open file for input...ending program in 3 seconds"
Sleep 3000
End 1
EndIf

'we succeeded in opening the file, so we continue...

'input all the data from the file into the array
Do Until Eof( file )
Input #file, namStr(count),numStr(count)
count = count + 1
Loop


'close the file
Close #file

'no matter how many names, print them all
For i As Integer = 1 To count-1
Print i; ": "; namStr(i); " " & numStr(i)
Next

'searching for a name's position
Print
Print
Print
Input "Who are you looking for?", oneNam

'linear search
searchName(oneNam, namStr(), found , i )

If found = -1 Then
Print "name not found!"
Else
Print "Name found at position "; found ; " " & numStr(found)
EndIf


Sleep
End

Sub searchName(oneNam As String , namStr() As String , found As Integer, i As Integer)
Dim count As Integer = 1
For i As Integer =1 To count-1
If namStr(i) = oneNam Then
found = i
EndIf
Next
End Sub
[/code]
dodicat
Posts: 6156
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Array error , please help.

Postby dodicat » Apr 03, 2015 0:29

In your sub searchname make parameter:
byref found As Integer
Do this in your declaration and actual sub.

Byref means that the sub will change the value of found to i if successful.
If you don't put byref, the parameter will default to byval, and the sub will only change found internally, which is no good to you.

In the sub the search should be from:
For i As Integer =1 To ubound(namStr)

You previously had:
For i As Integer =1 To count-1 which was from 1 to 0
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array error , please help.

Postby fxm » Apr 03, 2015 7:06

About the integer variable(s) 'i' of your programm, and its usage in your code above

1) In the -lang fb and -lang deprecated dialects only

You must also understand that when you declare a variable as iterator in a FOR instruction, this variable is local to the FOR...NEXT block, and remains unreachable from outside this block:

Code: Select all

For i As Integer = 1 To 5
  Print i
Next i

Print
Print i

Sleep
Compiler output:
.....\FBIDETEMP.bas(6) error 41: Variable not declared, i in 'Print i'

If you declare in the main module a variable with the same name and even of the same type, this variable (global variable to the module) is independent of the local variable (different memory address), and there is no interaction between these two variables:

Code: Select all

Dim i As integer = 33

Print i
Print

For i As Integer = 1 To 5
  Print i
Next i
Print

Print i

Sleep

Code: Select all

 33

 1
 2
 3
 4
 5

 33

General behavior (extract from /wiki/KeyPgDim):
In the -lang fb and -lang deprecated dialects, variables defined inside compound block statements (For..Next, While..Wend, Do..Loop, If..Then, Scope..End Scope) have local scopes, and are visible only within these blocks.


2) In the -lang qb and -lang fblite dialects

In the -lang qb and -lang fblite dialects, variables defined inside compound block statements (For..Next, While..Wend, Do..Loop, If..Then, Scope..End Scope) have global scope (not only local), and are also visible from outside these blocks:

Code: Select all

#lang "qb"

For i As Integer = 1 To 5
  Print i
Next i
Print

Print i

Sleep

Code: Select all

 1
 2
 3
 4
 5

 6
Last edited by fxm on Apr 03, 2015 11:13, edited 1 time in total.
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array error , please help.

Postby fxm » Apr 03, 2015 10:45

fxm wrote:Remark about the syntax of OPEN when it is used as a function

The documentation specifies one syntax using commas to separate different fields, but syntax without commas works also and I think most people use it also (without commas)!

What is it really the truth?

dkl / counting_pine, what is your opinion on that?
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Array error , please help.

Postby dkl » Apr 03, 2015 13:57

Both versions are supported, and I think that's ok.
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Array error , please help.

Postby fxm » Apr 03, 2015 16:33

Consequently, the relevant pages of documentation should be updated, for example simply by putting such commas in squared brackets.
Last edited by fxm on Apr 04, 2015 8:19, edited 2 times in total.

Return to “Beginners”

Who is online

Users browsing this forum: Google [Bot] and 2 guests