Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Limit input characters

stevelong_rrx
Beginner
1,826 Views

I'm looking for a way to limit user keyboard input in a console application that runs in Linux and is compiled with Intel Fortran 2017.

At some juncture, input is collected from the user to provide a comment that is then saved to a database.

The relevant lines of code are:

PRINT *, 'Provide a brief description of your change: '
ACCEPT '(A50)', REVCOM

The REVCOM (revision comment) variable is defined as CHARACTER*50 and it gets inserted into a database field that is defined as VARCHAR(50).

If the user types more than 50 characters, the extra characters are simply discarded (without any "warning" given to the user).  I suppose I could make REVCOM allow more than 50 characters, then if LEN(REVCOM) > 50 tell the user it's too long and make them reenter the comment.  However, I'd rather not make them have to retype anything or count characters.  I'd like to know if there is a way to make the keyboard buffer simply stop accepting more input once 50 characters is reached, so that the user can easily tell when they've reached the maximum length.

0 Kudos
1 Solution
FortranFan
Honored Contributor III
1,712 Views

In @stevelong_rrx 's defense, it was mentioned in the first line of the original post the console app is question runs on Linux.

@stevelong_rrx , I am unaware of an immediate solution you can start using but can make a few long-winded suggestions:

  1. Take a look at the Fortran-lang site (https://fortran-lang.org/) and the resources there under terminal (and color) options, perhaps you might find something useful,
  2. Also, make an inquiry at the related Fortran Discourse site (https://fortran-lang.discourse.group/), perhaps at this thread: https://fortran-lang.discourse.group/t/toml-fortran-0-3-0/4108/2.  Chances are a couple of readers there going by the handles of @awvwgk and @urbanjost may have some options for you.
  3. And/or post an inquiry at the Fortran stdlib site (https://github.com/fortran-lang/stdlib) , especially on the Issues thread at https://github.com/fortran-lang/stdlib/pull/580.  You might receive some useful guidance there.

By the way, you may know `ACCEPT` is a non-standard extension to Fortran - you may consider simply replacing it with the standard READ statement which will be supported reliably by all the Fortran compilers.

Good luck,

 

 

View solution in original post

9 Replies
Steve_Lionel
Honored Contributor III
1,803 Views

Fortran has no such feature. If you are on Windows, you could design a form with a text field and that can be limited to 50 characters and do what you want. It's some more coding on your part to design the form, display it and get the values using the IFLOGM (Dialog) library. For more information, see Using Intel® Visual Fortran to Create and Build Windows*-Based Applications

There are also dialog examples in the Samples Bundle.

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,786 Views

You should be able to, either directly or via a C interop function, call the C Runtime Library function getch and selectively use the CRTL function putch. This should work in both Windows and Linux.

 

Jim Dempsey

stevelong_rrx
Beginner
1,705 Views

Thanks, Jim!  As a Fortran novice and with even less experience with C, I might find pulling this off a little more complicated than the task warrants, but I think I understand the concept (while len(revcom)<50, print the typed character to the screen, append the character to the revcom variable, and accept another character).

0 Kudos
andrew_4619
Honored Contributor III
1,775 Views

SetConsoleScreenBufferSize sets the number of lines and the line length of the console buffer. 

 

0 Kudos
stevelong_rrx
Beginner
1,747 Views

Is SetConsoleScreenBufferSize part of the Windows API?  I'm not find any documentation that shows how it would be used in a Linux application that runs in a terminal console.

0 Kudos
Steve_Lionel
Honored Contributor III
1,734 Views

Yes, that's a Windows API routine. You did not say you were using Linux before.

0 Kudos
FortranFan
Honored Contributor III
1,713 Views

In @stevelong_rrx 's defense, it was mentioned in the first line of the original post the console app is question runs on Linux.

@stevelong_rrx , I am unaware of an immediate solution you can start using but can make a few long-winded suggestions:

  1. Take a look at the Fortran-lang site (https://fortran-lang.org/) and the resources there under terminal (and color) options, perhaps you might find something useful,
  2. Also, make an inquiry at the related Fortran Discourse site (https://fortran-lang.discourse.group/), perhaps at this thread: https://fortran-lang.discourse.group/t/toml-fortran-0-3-0/4108/2.  Chances are a couple of readers there going by the handles of @awvwgk and @urbanjost may have some options for you.
  3. And/or post an inquiry at the Fortran stdlib site (https://github.com/fortran-lang/stdlib) , especially on the Issues thread at https://github.com/fortran-lang/stdlib/pull/580.  You might receive some useful guidance there.

By the way, you may know `ACCEPT` is a non-standard extension to Fortran - you may consider simply replacing it with the standard READ statement which will be supported reliably by all the Fortran compilers.

Good luck,

 

 

jimdempseyatthecove
Honored Contributor III
1,657 Views

I suggest that your input routine be constructed to emulate what users are familiar with.

Accept input up to Enter

Handle backspace (rubout)

Echo and insert characters .le. buffer length

Ignore characters in excess of input buffer (permissible to make sound)

If you want to be fancy, permit left and right arrows and character insertion.

 

Note, you should be able to locate (google) a ready made console input routine that does all this.

 

Jim Dempsey

0 Kudos
stevelong_rrx
Beginner
1,648 Views

Thanks for the various suggestions!

For now, I've gone with a very low-tech method that isn't foolproof, but was very easy to implement and is better than nothing!

      PRINT *, 'Provide brief description of your change: '
      PRINT *, '(Maximum comment length: 50 characters----------->|'
      ACCEPT '(A50)', REVCOM

This feature won't be used frequently, and if users ignore the on-screen indication of maximum length, and complain about lost characters, then I'll revisit adding a console input routine to the code.

0 Kudos
Reply