DAU-Fragen

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
Danilo
-= Anfänger =-
Beiträge: 2284
Registriert: 29.08.2004 03:07

Re: DAU-Fragen

Beitrag von Danilo »

pbdau hat geschrieben:Ich habe an einem Beispiel gesehen, dass man über die
Konsole per input() Strings Zeichen eingeben kann.
Wie kann ich nur Zahlen eingeben (Variablen?) und z.B.
prüfen, ob die Zahl maximal zweistellig ist?
Wenn Du möchtest kannst Du mein WaitKey dafür nehmen.

Das Original:

Code: Alles auswählen

; "Console WaitKey.pb"
;
; by Danilo, 03.12.2003
;
Procedure.l WaitKey()
  ; waits until the user presses a key
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      key = asc
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure



OpenConsole()

PrintN(""):Print("Input? ")

For a = 1 To 10 ; get 10 chars with WaitChar
  key = WaitKey()
  If key=13
    Break
  ElseIf key > 31
    Print(Chr(key))
  EndIf
Next a

PrintN(""):Print("Number? ")

For a = 1 To 10
  key = WaitKey2("0123456789."+Chr(13))
  If key=13 ; return
    Break
  EndIf
  Print(Chr(key))
Next a
Und da siehst Du ja schon wie man Eingaben limitieren kann.
3-stellige Zahl:

Code: Alles auswählen

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure

OpenConsole()

PrintN(""):Print("Bitte geben sie eine 3-stellige Nummer ein: ")

For a = 1 To 3
  key = WaitKey2("0123456789"+Chr(13))
  If key=13 ; return
    Break
  EndIf
  Print(Chr(key))
  num$+Chr(key)
Next a

PrintN(""):PrintN(""):PrintN("Ihre Nummer: "+num$)
PrintN("<return>")
WaitKey2(Chr(13))

Debug Val(num$)
Einfache Ja/Nein-Abfrage (j/n):

Code: Alles auswählen

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure

OpenConsole()

PrintN(""):Print("Partition 'c:\' wirklich formatieren ? (j/n) ")

key = WaitKey2("jJnN") : PrintN(""):PrintN("")

If key='j' Or key='J'
  Print("Formatierung.")
  For a = 1 To 10
    Print("."):Delay(500)
  Next a
  PrintN(" erfolgreich.")
Else
  PrintN("Formatierung wird nicht durchgefhrt.")
EndIf

PrintN("<return>")
WaitKey2(Chr(13))
Für den Anfang kann das ganz hilfreich sein.

Wenn Du bei der Eingabe der Zahlen noch Backspace
unterstützen willst (zum korrigieren), dann solltest Du
Dir vielleicht ein kleines TUI dafür bauen.

Hier noch ein kleines Beispiel für die Eingabe einer maximal
6-stelligen Zahl, wobei korrigieren per Backspace erlaubt ist:

Code: Alles auswählen

Procedure.l WaitKey2(string$)
  ; waits until the users presses a key
  ; specified in string$
  Repeat
    asc = Asc(Inkey())
    If asc > 0 And asc < 127
      If FindString(string$,Chr(asc),1)
        key = asc
      EndIf
    EndIf
    Delay(10)
  Until key
  ProcedureReturn key
EndProcedure

OpenConsole():ClearConsole()

For a = 1 To 6: Line$+Chr(196): Next a
PrintN(Space(44)+Chr(218)+Line$+Chr(191))
PrintN("Bitte geben sie eine 6-stellige Nummer ein: "+Chr(179)+"      "+Chr(179))
PrintN(Space(44)+Chr(192)+Line$+Chr(217))
ConsoleLocate(50,1)

Repeat
  key = WaitKey2("0123456789"+Chr(13)+Chr(8))
  If key=13 ; return
    Break
  ElseIf key=8 ; backspace
    num$=Left(num$,Len(num$)-1)
  Else
    num$+Chr(key)
  EndIf
  ConsoleLocate(45,1)
  Print(RSet(num$,6," "))
  ConsoleLocate(50,1)
Until Len(num$)=6

PrintN(""):PrintN(""):PrintN("<return>")
WaitKey2(Chr(13))

Debug Val(num$)
Man kann also mit WaitKey() und WaitKey2() einiges recht
einfach machen...
cya,
...Danilo
"Ein Genie besteht zu 10% aus Inspiration und zu 90% aus Transpiration" - Max Planck
pbdau
Beiträge: 9
Registriert: 21.10.2004 11:01
Wohnort: München

Danke.

Beitrag von pbdau »

:D
Danke Leute, ich werds am Wochenende mal Testen. Wollte erst mal mit der Konsole anfangen um mich dann erst an die grafische Benutzeroberfläche zu wagen.
Die Lösung von TurnundTaxis war die, die ich genau NICHT wollte.
Antworten