INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Wrinkly trying to get back into Fortran!

Wrinkly trying to get back into Fortran!

(OP)
Hi. It has been over 35 years since I wrote my last Fortran program and that was in Fortran IV and on punched cards! I have just installed gfortran (Fortran 95) on my Linux box and have written my first simple program:

program f2c2f
! convert degrees C to F and F to C
implicit none
real :: x, c, f
print*, 'Enter a number:'
read*, x
c = (x - 32) * 5 / 9
f = x * 9 / 5 + 32
2010 format(f6.2, ' deg F = ', f6.2, ' deg C.')
2020 format(f6.2, ' deg C = ', f6.2, ' deg F.')
print 2010, x, c
print 2020, x, f
end program f2c2f

It works fine but there's just one thing I would like to change if possible. I would like the prompt on the same line as the entry of the number. I thought it was possible to add a format to the Read statement and I have tried in various ways to do this without success. All the online guides seem to gloss over formats on read statements and I'm not sure if they can contain literals.

Can any kind soul help please?

RE: Wrinkly trying to get back into Fortran!

like this

CODE

program f2c2f
 ! convert degrees C to F and F to C
  implicit none
  real :: x, c, f
  !print*, 'Enter a number:'
  write(*,'(A)',advance='no') 'Enter a number:'
  read*, x
  c = (x - 32) * 5 / 9
  f = x * 9 / 5 + 32
  2010 format(f6.2, ' deg F = ', f6.2, ' deg C.')
  2020 format(f6.2, ' deg C = ', f6.2, ' deg F.')
  print 2010, x, c
  print 2020, x, f
end program f2c2f 

RE: Wrinkly trying to get back into Fortran!

(OP)
Cool. Many thanks mikrom

RE: Wrinkly trying to get back into Fortran!

you are welcome

RE: Wrinkly trying to get back into Fortran!

An alternative solution is to slightly modify the print statement:

print '(a$)', 'Enter a number: '

The $ at the end of the format string will direct screen-printings to continue on the same line, not the beginning of next line.
A space is added after the colon to make the screen-printings less compact.
(I noted that an x at the end of the format had no effect; i.e. '(ax$)' did not add an extra space.)

I compile with g77.

RE: Wrinkly trying to get back into Fortran!

Thanks, I tried it with gfortran and it works.

RE: Wrinkly trying to get back into Fortran!

(OP)
Thanks 7man

RE: Wrinkly trying to get back into Fortran!

What the $ does at the end of the format-string must be that it prevents the addition of a linefeed (LF) character (or CRLF).

RE: Wrinkly trying to get back into Fortran!

(OP)
Yes, I think I read in that post that advance="no" was f90/95

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close