'Incorrect output with '(23A, 1X, F4.1, 2X, 1A)' format
I am writing my first FORTRAN program since 1989 (using FORTRAN77) and this is my introduction to a modern FORTRAN. I am having trouble with formatting write statements. Specifically, after reading a REAL value and a CHARACTER, I then use a formatted WRITE statement to allow the user to review the input. I can use an unformatted WRITE statement and everything is fine, however, if I use a simple formatting statement, the resulting output is garbage. The following is my MWE:
! MWE program testing formatted write of a real number
!
! bash cmd line gfortran-9 -o MWE_formated_write MWE_formated_write.f95
! build environment:
! Kubuntu 21.04 / kernel 5.11.0-34-generic
! GNU Fortran (Ubuntu 9.3.0-23ubuntu2) 9.3.0
! laptop w/ 4x Intel i5-5200U @ 2.2 GHz w/ 8 GiB ram
!
program MWE_formated_write
!
implicit none
real :: temp_input ! input temperature
character(len=1) :: temp_input_units ! input temperature unit, [F]/[C]/[R]/[K]
!
! Get input temperature and temperature units
!
write(*,*) "Enter the temperature and units F/C/R/K: "
read(*,*) temp_input, temp_input_units
!
! verify the input temperature and temperature units
!
write(*, *) " You entered (raw):", temp_input, temp_input_units
write(*, '(23A, 1X, F4.1, 2X, 1A)') "You entered (formated):", temp_input, temp_input_units
!
stop
end program MWE_formated_write
and a capture of the output:
joseph@bohr:~/Projects$ gfortran-9 -o MWE_formated_write MWE_formated_write.f95
joseph@bohr:~/Projects$ ./MWE_formated_write
Enter the temperature and units F/C/R/K:
95 F
You entered (raw): 95.0000000 F
You entered (formated):�BF
I expected the second line to output "You entered (formated): 95.1 F".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
