Page 1 of 1

a prompt for input in c.l. application

Posted: Monday 20th November 2023 2:39pm
by ail_gu
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?

Re: a prompt for input in c.l. application

Posted: Monday 20th November 2023 4:03pm
by BruceSteers
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?
for a terminal applications look at File.In and File.Out and the File.In.ReadLine() Method.

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 :-\

Re: a prompt for input in c.l. application

Posted: Monday 20th November 2023 6:59pm
by BruceSteers
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 :-\
Okay i got the answer for single line method now...


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

Re: a prompt for input in c.l. application

Posted: Monday 20th November 2023 9:00pm
by vuott
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