2

So I'm just getting started learning Fortran because apparently it's still used for a lot of scientific computing. One of the things that I already hate about it is that compared to C++, strings are a nightmare. At the moment, I'm just trying to find a simple way to read in a string provided by the user and then spit it back out without all the trailing whitespace.

Here's the code I have right now, which according to what I've read should work under later Fortran standards, i.e. 2003/2008 (though I could easily have made errors of which I'm unaware). I'm trying to compile it on the MinGW version of gfortran that came with Code::Blocks 13.12.

program tstring
implicit none
character(100) io_name
character(len=:), allocatable :: final_name

print *, "What is your name, O master?"
read *, io_name
final_name = trim(io_name)

print *, "Excellent, master ", final_name, "!"
end program

It compiles just fine, but still has a massive amount of whitespace between final_name and the "!". The whitespace is somewhat dependent on the number of characters I give to io_name, but not in a particularly logical manner (15 characters gives more whitespace than 30, for example). Particularly bewildering to me is that if I give certain numbers of characters to io_name (between about 17 and 22) then instead of printing out the name, the program runs into a segfault.

Perhaps the most difficult part of this for me is that good Fortran documentation is very hard to find, especially for the 2003 and later standards. So if anybody could point me to some good documentation, I'd really appreciate it!

6
  • I copied your code and compiled with gfortran4.8.2 (on Linux x86_64) and it works fine; but if I compiled with gfortran4.7.2, it gives no characters before "!". So it seems we need version >=4.8. As for Intel Fortran (ifort), it works at least for >= 13.0 (which I have at hand). Commented Jun 25, 2015 at 22:18
  • I wish I could do that, but one of the companies I want to work for specifically asks for experience programming in Python and Fortran. Of the two, Fortran is proving far more difficult. Commented Jun 25, 2015 at 22:21
  • @roygvib So you're saying that v4.8.2 gives extra whitespace that v4.7.2 does not? Commented Jun 25, 2015 at 22:23
  • No, no, I mean v4.8.2 works no white space before "!" (more precisely, no white space before and after what we have input from stdin) so works as expected. On the other hand, v4.7.2 gives something like "master !" with no input text before "!" (so totally wrong result). Commented Jun 25, 2015 at 22:25
  • 1
    @RichardFitzhugh If your target company does not specifically need Fortran >=2003 (particularly its OOP features), I really believe that Fortran95 is more than sufficient for most scientific computation in practice (e.g., at least used in these packages en.wikipedia.org/wiki/…). If limited to F95, it should be very fast to get to become fluent (indeed, I feel it is much easier to learn than Python or C++ because the syntax is quite limited). In my case I like it because of its compact syntax for multi-dim arrays (like Numpy) :) Commented Jun 25, 2015 at 23:17

1 Answer 1

4

Get a more recent compiler. Gfortran 4.8 (AFAIK the oldest supported version) should work just fine with your code.

Strings are not really any nightmare, just stop thinking in C++ and trying to translate that in Fortran. It is quite possible to write tokenizers, parsers and DSL interpreters in Fortran.

Regarding the resources, it is off-topic here and there are numerous books and tutorials out there. Search for Fortran Resources at http://fortranwiki.org/fortran/show/HomePage

Sign up to request clarification or add additional context in comments.

1 Comment

I would just like to say that after installing the most recent version of gfortran (v 5.0 I believe), everything suddenly works much better. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.