a prompt for input in c.l. application

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
ail_gu
Posts: 1
Joined: Monday 20th November 2023 2:28pm

a prompt for input in c.l. application

Post 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?
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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 :-\
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

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

Post 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
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply