Sorry for an elementary question. I am programming for 50 years, but it is my first experience with Gambas. How can I ask user to enter a number and get their input on the same line of the terminal?
In Basic it would be
Input "Invitation phrase",#filenumber, variable. In Gambas it seems there is no "invitation phrase". Is there any way to achieve the same result?
a prompt for input in c.l. application
- BruceSteers
- Posts: 1342
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: a prompt for input in c.l. application
for a terminal applications look at File.In and File.Out and the File.In.ReadLine() Method.ail_gu wrote: ↑Monday 20th November 2023 2:39pm Sorry for an elementary question. I am programming for 50 years, but it is my first experience with Gambas. How can I ask user to enter a number and get their input on the same line of the terminal?
In Basic it would be
Input "Invitation phrase",#filenumber, variable. In Gambas it seems there is no "invitation phrase". Is there any way to achieve the same result?
File.In and File.Out point to stdin and stdout respectively.
Something like this should work...
Public Sub Main()
Dim sTxt As String
Print "type something below.."
sTxt = File.In.ReadLine() ' read a line from stdin
Print "the text typed was: " & sTxt
End
I am not sure how to not put in a linefeed with the output text, if i used
Print "question: "; then the end semicolon should not make it insert a linefeed but it makes the text not show at all :-\
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1342
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: a prompt for input in c.l. application
Okay i got the answer for single line method now...BruceSteers wrote: ↑Monday 20th November 2023 4:03pm
I am not sure how to not put in a linefeed with the output text, if i used
Print "question: "; then the end semicolon should not make it insert a linefeed but it makes the text not show at all :-\
Dim sTxt As String
' print a question to stdout with no linefeed as line ends with ;
Print "This Question: ";
' Flushing stdout is required to display the text (this is what i was missing)
Flush #File.Out
' read answer from stdin
sTxt = File.In.ReadLine()
Print "You answered " & sTxt
Note: some gambas native commands like Read , Write, Flush, etc require the hash # prefix with their arguments to denote a file pointer.
hence the
Flush #File.Out
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: a prompt for input in c.l. application
In addition to "File.In.ReadLine()" you can also use "Input":
Public Sub Main()
Dim s As String
Write "type something here: "
Flush
Input s
Print "the text typed was: "; s
End
I am a European.
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.