Number required for function error in qb64

70 Views Asked by At

I am writing a qb64 / QBasic software program to convert a numerical grade 0-100 to a letter grade (a,b,c,d,f). I have set it up so that strings/arrays are used to allow infinite grades to be entered. However when I try to calculate the letter grade and call it back to print with calcGrade!(MyList(Index%)) I receive the error "Number required for function" Can anyone help me with this?

ReDim MyList(0) As String
Dim Grade 
Dim Index%
_Title "Letter Grade Calculator"
Print "Enter your numerical grades. To finish enter (exit)"
Do While Grade$ <> "exit"
    Input "Enter Grade"; Grade$
    If Grade$ <> "exit" Then
        Index% = Index% + 1
        ReDim _Preserve MyList(Index%)
        MyList(Index%) = Grade$
    End If
Loop
Cls
For Index% = 1 To UBound(MyList)
    Print MyList(Index%);
     Print "Letter Grade:"; calcGrade!(MyList(Index%))
Next Index%
Function calcGrade! (numGrade)
      Select Case val(numGrade)
        Case is > 100
             calcGrade! = "A+"
       Case Is > 90
             calcGrade! = "A"
       Case Is > 80
             calcGrade! = "B"
      Case Is > 70
             calcGrade! = "C"
      Case Is > 60
             calcGrade! = "D"
       Case Is > 50
             calcGrade! = "F"
      Case Else
             calcGrade! = "ERROR: Invalid character"
     End Select
End Function
1

There are 1 best solutions below

0
Sep Roland On

However when I try to calculate the letter grade and call it back to print with calcGrade!(MyList(Index%)) I receive the error "Number required for function"

The error "Number required for function" that you receive for calcGrade!(MyList(Index%)) means that the calcGrade function is expecting to receive from you a numerical argument to be assigned to its numGrade parameter.

Function calcGrade! (numGrade)

Without any explicit declaration the numGrade parameter defaults to the single numerical format. However you are providing a string argument via MyList.

A further error is that the exclamation mark on the function's name, declares the function to return a single value. Therefore you cannot assign all of those "A+", "A", "B", "C", "D", "F", and "ERROR: Invalid character"string values to it.

The solution is to use the $ string suffix on calcGrade$ and numGrade$.

...
Cls
For Index% = 1 To UBound(MyList)
    Print MyList(Index%); " -> ";
    Print "Letter Grade:"; calcGrade$(MyList(Index%))
Next Index%
Function calcGrade$ (numGrade$)
    Select Case val(numGrade$)
      Case Is > 100
             calcGrade$ = "A+"
      Case Is > 90
             calcGrade$ = "A"
      Case Is > 80
             calcGrade$ = "B"
      Case Is > 70
             calcGrade$ = "C"
      Case Is > 60
             calcGrade$ = "D"
      Case Is > 50
             calcGrade$ = "F"
      Case Else
             calcGrade$ = "ERROR: Invalid character"
    End Select