Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrinkly trying to get back into Fortran!

Status
Not open for further replies.

gandalf458

IS-IT--Management
Jul 23, 2015
24
IT
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?
 
like this
Code:
program f2c2f
 ! convert degrees C to F and F to C
  implicit none
  real :: x, c, f
  !print*, 'Enter a number:'
  [highlight]write(*,'(A)',advance='no') 'Enter a number:'[/highlight]
  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
 
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.
 
Thanks, I tried it with gfortran and it works.
 
What the $ does at the end of the format-string must be that it prevents the addition of a linefeed (LF) character (or CRLF).
 
Yes, I think I read in that post that advance="no" was f90/95
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top