Fortran is primarily meant for batch numerical computing and as Steve Lionel points out, the language has no features to support this sort of interactive use. Let me make a few suggestions:
Wait indefinitely for user input.
This is the traditional method of getting user input and example code is available near the front of every Fortran textbook. The upside is that it's simple to implement; the downside is that the code effectively hangs until the user responds. Effectively, the code blocks, waiting on user input; there's no signalling or threading system in Fortran to proceed after a time interval has elapsed. On unix systems, I made use of SIGALRM and signal handlers in Perl to implement timeouts but that technique is specific to POSIXy systems and is an example of what can be done with OS libraries, as Steve Lionel noted.
I'm assuming this is the problem you are trying to work around.
Take all user input via an input file and/or command-line switches.
Assume the user knows how your software works and understands how to specify input via the command line or an input file. The Fortran Wiki shows a number of command-line processors which you could use directly. Otherwise, they are good example code for using (modern) Fortran 2003 intrinsics get_command_argument and command_argument_count.
For extended input, you will probably want to use some form of input file. Unfortunately, there's effectively no standard Fortran library which helps with this age-old problem; compare with (for example) Python's ability to read JSON with standard libraries which eliminates the need to invent Yet Another Broken, Proprietary, Unparsable, Garbagey Input File Format (YABPUGIFF). Here is a typical example. Welcome to user interface hell.
In my experience, inventing crappy input file formats and maintaining equally crappy parsers is about 75% of Fortran programming. There are third-party libraries to read and write INI, CSV, JSON, XML, etc. in Fortran. I suggest you investigate those before inventing your own crappy input format.
For programming in general, learn the operating system standard(s) for command line interfaces such as GNU long options and common options such as -v
and --verbose
for verbose output, -h
, --help
, and /?
for getting help, --usage
for showing usage information, etc.
Write the user interface in a different language.
A number of languages interoperate reasonably well with Fortran via the standard module ISO_C_BINDING
. I've had good results with Lua, but C and Python work well also.
If you don't need deep access to your code's internals, you can just read input via a wrapper program written in Python, etc., then create the appropriate command line or input file for your Fortran application, then issue a system()
or exec()
call to actually run your code. I use Python's subprocess
or multiprocessing
libraries to spawn and watch Fortran executables under both Windows or Linux and have used the Jinja2 template system for generating input for Fortran codes, though any of the popular web-oriented templating frameworks work just as well.
My biggest piece of advice is do not reinvent the wheel; others sorted this out decades ago, follow community standards even though they aren't perfect, and get on with writing interesting code.