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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I accept a carriage return as input???

Status
Not open for further replies.

MigrantFirmWorker

Programmer
Apr 9, 2003
54
US
I wish to query the user for input, but use a default value if the user enters a carriage return without text. Unfortunately the program will not proceed until the user enters text.

write(6,'(a,$)') 'Enter file (default: dummy.inp): '
read(5,*) inputFile
if(trim(inputFile) == '') inputFile='dummy.inp'

Is there a way to work around this limitation?
 
Try using an explicit format
Code:
      read(5,1000) inputFile
1000  format(a80)

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Thanx. Unfortunately, the program still stops on that read statement until a character is entered.
 
C TRY THE FOLLOWING :
CHARACTER * 80 InputFile
C write (6 ..... as before, but
READ(5,'(A)') InputFile
C or FORMAT specification as CaKiwi's
C but A instead of A80
 
It works on the old Microsoft Fortran. An idea to try
READ 3, IFILE
3 FORMAT(A)
 
I agree.

I used it a lot on M$ Fortran, and before that on General Automation FORTRAN IV.

It seemed to work ok then.

rgds
Zeit.
 
Here is a short program that does what you want, except that it simply displays the file name rather than doing anything with it. I used the common f90 extensions do while, enddo, and exit. The trim function was not available on my version of g77.

implicit none
character*(*) default
parameter (default = 'DefaultValue')
character*20 name
integer n

write(*,'(a,$)') 'Enter file name: '
read(*,'(a)') name
n = len(name)
do while (n .gt. 0)
if (name(n:n) .eq. ' ') then
n = n - 1
else
exit
endif
enddo
if (n .eq. 0) then
name = default
n = len(default)
endif
write(*,*) name(1:n)
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top