Input Box problem

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 51
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Input Box problem

Post by Löwenherz »

Hello all, I found some days ago an old freebasic example but it doesn't Run properly perhaps Somebody can Help, thanks

Code: Select all

#Include "windows.bi"

' Declare external functions
''Declare function GetStdHandle( byval nStdHandle as ulong ) as ulong
''Declare function ReadConsoleA( byval hConsoleInput as ulong, byval lpBuffer as any ptr, byval nNumberOfCharsToRead as ulong, byref lpNumberOfCharsRead as ulong, byval lpReserved as any ptr ) as long

' Define constants
''Const STD_INPUT_HANDLE = -10
''Const STD_OUTPUT_HANDLE = -11
''Const STD_ERROR_HANDLE = -12
const BUFSIZE As integer = 128

' Define input function
function InputBox( prompt as string ) as string
    dim as ulong bytes_read,vp
    dim as any ptr input_handle
    dim as integer Ptr buffer ' any ?
    dim as string input_text
    
    ' Get handle to console input
    input_handle = GetStdHandle( STD_INPUT_HANDLE )
    
    ' Allocate memory for input buffer
    buffer = allocate( BUFSIZE * SIZEOF( INTEGER )) ' Char ? 
    If (buffer =  0) Then
    	Print "error"
    	End -1
    EndIf
    ' Print prompt
    print prompt;
    
    ' Read input from console
    '------------------------------ problem zone ?---------------//
    
    ReadConsoleA( input_handle, buffer, BUFSIZE, varptr(bytes_read),0 ) ' 
    
    '------------------------------ problem zone ?---------------//
    
    ' Convert buffer to string
    input_text = ltrim(Str(buffer) ) ' *buffer
    ''input_text = str(buffer)
    
    ' Free memory
    DeAllocate(buffer)
    
    ' Return input text
    return input_text
End function

' Main program
dim as string myname

' Get user input
myname = InputBox( "Enter your name: " )


' Print input
Print ("Hello, "); myname; "!"

sleep
End 0
Edited new
fxm
Moderator
Posts: 12177
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Input Box problem

Post by fxm »

Code: Select all

#Include "windows.bi"

' Declare external functions
''Declare function GetStdHandle( byval nStdHandle as ulong ) as ulong
''Declare function ReadConsoleA( byval hConsoleInput as ulong, byval lpBuffer as any ptr, byval nNumberOfCharsToRead as ulong, byref lpNumberOfCharsRead as ulong, byval lpReserved as any ptr ) as long

' Define constants
''Const STD_INPUT_HANDLE = -10
''Const STD_OUTPUT_HANDLE = -11
''Const STD_ERROR_HANDLE = -12
const BUFSIZE As integer = 128

' Define input function
function InputBox( prompt as string ) as string
    dim as ulong bytes_read,vp
    dim as any ptr input_handle
    dim as zstring Ptr buffer ' <==========
    dim as string input_text
    
    ' Get handle to console input
    input_handle = GetStdHandle( STD_INPUT_HANDLE )
    
    ' Allocate memory for input buffer
    buffer = allocate( BUFSIZE * SIZEOF( Zstring )) ' <==========
    If (buffer =  0) Then
    	Print "error"
    	End -1
    EndIf
    ' Print prompt
    print prompt;
    
    ' Read input from console
    '------------------------------ problem zone ?---------------//
    
    ReadConsoleA( input_handle, buffer, BUFSIZE, varptr(bytes_read),0 ) ' 
    
    '------------------------------ problem zone ?---------------//
    
    ' Convert buffer to string
    input_text = ltrim(*buffer) ' <==========
    ''input_text = str(buffer)
    
    ' Free memory
    DeAllocate(buffer)
    
    ' Return input text
    return input_text
End function

' Main program
dim as string myname

' Get user input
myname = InputBox( "Enter your name: " )


' Print input
Print ("Hello, "); Left(myname, Instr(myname, Chr(&h0D)) - 1); "!" ' <========== to stop before the carriage return in the myname string

sleep
End 0

But why make it complicated when you can make it simpler:

Code: Select all

Function InputBox(prompt As String) As String
    Dim As String input_text
    Print prompt;
    Input "", input_text
    Return input_text
End Function

Dim As String myname

myname = InputBox("Enter your name: ")
Print "Hello, "; myname; "!"

Sleep
Lothar Schirm
Posts: 443
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Input Box problem

Post by Lothar Schirm »

Löwenherz, what is the advantage of your code? Why not use Input directly:

Code: Select all

Dim As String myname
Input "Enter your name: ", myname"
Print "Hello "; myname
Sleep
Löwenherz
Posts: 51
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Input Box problem

Post by Löwenherz »

Thank you fxm for corrections and Lothar in general I'm interested in programming and what's behind it and I enjoy learning about it :) freebasic is an interesting and very good Basic language
Post Reply