Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having some trouble with Fortran

  1. Sep 10, 2013 #1
    I am sitting down and working my way through a Fortran 90/95 book for a research project I will be involved in at my university in a few weeks. However, I can't even seem to get the most basic program to work even when copying the template directly from the book. It always says invalid character at (1). For instance, my Hello World program was formatted as such:

    PROGRAM Hello_world
    WRITE(*,*) "Hello, World!"
    END program Hello_world

    Then, I saved it as hello.f90.

    When I go to try to compile it, it keeps giving me an an invalid program statement at (1) and several other invalid character at (1) errors.

    I am using the Terminal on Linux and VIM if that matters.
     
  2. jcsd
  3. Sep 10, 2013 #2

    Borek

    User Avatar

    Staff: Mentor

    For some reason it makes me think about UTF-8 with BOM. But I can be completely off.
     
  4. Sep 10, 2013 #3

    jedishrfu

    Staff: Mentor

  5. Sep 10, 2013 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You should be able to use "underscore" in Fortran 90 or 95 variable names etc - but there are some not-quite-standards-compliant compilers around (and some not-even-close-to-being-standards-compliant ones!)

    Another possibility is the difference between free-format and fixed-format source code. If you and the compiler disagree about which format you are using, nothing will work and the error messages you get might look weird..

    The two formats might be distinguished by different filename extensions like .f90 and .F90, or by using a compiler option on the command line. Check your compiler documentation. (If you are learning Fortran from scratch, use free format!)

    As for standards, there's an old programming joke: scientific programmers are happy to write in any language, provided it's called FORTRAN. (And conversely, commercial programmers don't care what their programming language is called, so long as the syntax is identical to COBOL.)
     
  6. Sep 10, 2013 #5
    Is there a copy of the reference manual on the web for the manufacturer's version of FORTRAN that you are using? I'm not asking for a generic FORTRAN book, but the manual for the exact version that you are using. With that it should be possible to track down exactly what the problem is and what is needed to fix this.
     
  7. Sep 11, 2013 #6
    I tried saving the file as .F90 now, and it still gives me the same errors.

    I will check the web for the reference manual.

    @Jedi -- I copied that program directly and it is still giving me same error.

    @Borek -- This is my first programming language, and I honestly have no idea what you just said haha.

    Thank you everyone for the help.

    Also, I am compiling with gfortran.
     
  8. Sep 11, 2013 #7

    phyzguy

    User Avatar
    Science Advisor

    I copied your program directly and it compiled and ran just fine. Tell us exactly what commands you are using to compile and run the program, and what operating system you are using.
     
  9. Sep 11, 2013 #8
    Well, I am running linux on one of my school's computers.

    I open vim and type the program directly as it is show above. I then save it to my desktop as hello.f90

    Then, in the terminal, I type compile it:

    gfortran hello.f90

    and also, at the advice of someone else, tried

    gfortran -o hello hello.f90

    but both of them gave me the same error.
     
  10. Sep 11, 2013 #9

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

  11. Sep 11, 2013 #10
    please post the output of the following command:
    Code (Text):

    gfortran --version
     
     
  12. Sep 11, 2013 #11

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That all looks OK. But you said...
    ... so your best option is find somebody who can sit down in front of you own computer and sort this out. We can keep guessing what the problem might be, but it you don't understand what the guesses mean, that might not help even if we guess right!

    Yeah, this probably seems frustrating and ridiculous - but we've all been there, at some time in the past.
     
  13. Sep 11, 2013 #12

    phyzguy

    User Avatar
    Science Advisor

    I'm not sure what the issue is. Like I said, I copied your program exactly and it compiled and ran fine on my Unix machine. My gfortran version is:

    GNU Fortran (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50)
    Copyright (C) 2007 Free Software Foundation, Inc.
     
  14. Sep 11, 2013 #13

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    That's pretty old. The current release is 4.9
     
  15. Sep 11, 2013 #14

    Borek

    User Avatar

    Staff: Mentor

    It had nothing to do with programming, more with a format of the text file.

    http://en.wikipedia.org/wiki/Utf-8#Byte_order_mark

    I believe vim can be configured to add BOM to the files. I can be wrong though.
     
  16. Sep 11, 2013 #15

    phyzguy

    User Avatar
    Science Advisor

    That's a good point. Why don't you try a different text editor - gedit or emacs and see if you have the same problem.
     
  17. Sep 12, 2013 #16
    Nevermind what this previously said.

    I tried changing the text editor and that worked brilliantly. Thank you everyone so much for all of your help.
     
    Last edited: Sep 12, 2013
  18. Sep 12, 2013 #17

    phyzguy

    User Avatar
    Science Advisor

    The .out file is the executable. Let's say the file is called a.out then to run it, just type:

    $./a.out

    To be clear, you don't type the $. This is the prompt supplied by the system.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Having some trouble with Fortran
Loading...