Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with one function
#1
The function expects a double, but should return a string. Is this possible?
It is about replacing the Saxon point with a comma in a German edition. The program works, and I want to wrap that in a function. But the function doesn't want to.

"Illegal string-number conversion" - where? Maybe someone knows where the error is? Thanks!

This is OK:
Code: (Select All)

'Punkt in der Zahlenausgabe durch Komma ersetzen - 28. Aug. 2023
'Aus: SB S.100

Option _Explicit

Dim As Double zahl
Dim As Integer punkt_position
Dim As String zk_zahl

Print "Eingegebene Zahl mit Komma ausgeben"

Print
Input "Zahl: ", zahl

'In Zeichenkette umwandeln
zk_zahl = Str$(zahl)

'Position des Punktes ermitteln
punkt_position = InStr(zk_zahl, ".")

'Punkt durch Komma ersetzen - Mid$-Anweisung
If punkt_position <> 0 Then
  Mid$(zk_zahl, punkt_position) = ","
End If

Print "Zahl nach deutscher Notation: "; zk_zahl

End

The function gives the error message (punktKomma = zk_zahl -- Line 50)
Code: (Select All)

'Punkt in der Zahlenausgabe durch Komma ersetzen - 28. Aug. 2023
'Aus: SB S.100

Option _Explicit

Declare Function punktKomma(dEingabe As Double) As String

Dim As Double zahl
Dim As Integer punkt_position
Dim As String zk_zahl

Print "Eingegebene Zahl mit Komma ausgeben"

Print
Input "Zahl: ", zahl

'In Zeichenkette umwandeln
zk_zahl = Str$(zahl)

'Position des Punktes ermitteln
punkt_position = InStr(zk_zahl, ".")

'Punkt durch Komma ersetzen - Mid$-Anweisung
If punkt_position <> 0 Then
  Mid$(zk_zahl, punkt_position) = ","
End If

Print "Zahl nach deutscher Notation: "; zk_zahl

Print

'Warum ueber Val(x) gehen? Der Ursprungswert ist doch da.
zahl = zahl * 3
Print Using "Eingabe * 3 = ####.##"; zahl

End

Function punktKomma (dEingabe As Double)

  Dim As Integer punkt_position
  Dim As String zk_zahl

  zk_zahl = Str$(dEingabe)
  punkt_position = InStr(zk_zahl, ".")

  If punkt_position <> 0 Then
    Mid$(zk_zahl, punkt_position) = ","
  End If
  punktKomma = zk_zahl
End Function
Reply
#2
You need to declare `punktKomma` with a `$`, so `punktKomma$`.

Note that in QB64 the `DECLARE` lines are unnecessary and ignored, so unfortunately there's no warning or error about how your declaration (which has `As String`) doesn't match the actual function definition (which just returns the default `Single`).
Reply
#3
Function punktKomma$ (dEingabe As Double)  <<-- All fixed.  See the $ there?

Matt musta beat me by seconds!
Reply
#4
Aaaah. . . with strings. I know it, and I forgot it.

[Image: banghead.gif]

That was it! Many thanks!
Reply




Users browsing this thread: 5 Guest(s)