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!

Read(5,*) Character - negative number problem

Status
Not open for further replies.

Ploper001

Technical User
Oct 5, 2006
286
GB
Hi, I have the following code:


READ(5,*) StartNumberInput
IF(StartNumberInput == 'X' .OR. StartNumberInput == 'x') THEN
RootNumber = 4
ELSE
READ(StartNumberInput,*) Initialx0(2)
x0 = Initialx0(2)
END IF


(StartNumberInput is a character/string, Initialx0 and x0 are Real Numbers)

When I break after the first line, it seems that when I've typed in a negative number such as "-1", StartNumberInput just becomes "-". This in turn makes x0 "-0.00" which is not what I entered.

I have very limited Fortran experience, could anyone help please?
 
Hi, try this:

program testX0

character*12 StartNumberInput
real*8 Initialx0(2)
real*8 x0

READ(5,'(a)') StartNumberInput
IF((StartNumberInput.eq.'X') .OR. (StartNumberInput.eq.'x')) THEN
RootNumber = 4
ELSE
READ(StartNumberInput,'(f10.0)') Initialx0(2)
x0 = Initialx0(2)
END IF

write(5,*) x0 ! Test

end
 
??

What do you want your program to do ??
What I can make of it is, that you want to read from an input channel that you define by typing in a number. But why would you want to read from different channels ? I would understand from different files, but different channels ?? Today it's console, next time from lineprinter, next from card puncher (these things existed and were adressed by their default channelnumbers in the past way back in the golden days of yore)??

If that is what you want to do, StartNumberInput has to be an integer, otherwise your second read won't work.

And even if you make it an integer, I would not wonder if you receive an error as to the channel not being connected, as these devices are not defaulted on each and every compiler in use today.

Refer to data input, especially 'open' in your compiler's docs and redefine what it is that you want to do once you know what the options might be.


Norbert
 
I understand Norbert's conern, it is not clear what you want your program to do. But I do not quite agree with him regarding the "read channel" (sorry Norbert!). You can use the READ statement to read from internal character strings, for instance:

character*20 string
integer*4 int

string = '12345'
read(string,'(i10)') int

and also write into them:

int = 100
write(string,'(i10)') int

Best regards
GulliPe

 
Sorry about that - I want the user to input a real value basically. HOWEVER, I also want them to be able to type "X" to exit the program.

Thus, I have to read the input as a character and then, if it isn't "X", convert it to a real.


Thanks for the help guys, it seems specifying a length for my character/string "StartNumberInput", like in gullipe's first post did the trick (before I just declared it as a CHARACTER). Thanks! :)
 
@ gullipe

no need to be sorry, here I learned a new one (still not perfect in knowing everything).


@ ploper:

much more simple still: define your StartNumberInput as integer (real only works as far as I know because your compiler automatically transforms it to integer - as far as I know, but see above ;-)) and read it as integer. Then any non - integer input (x, or any other literal) would yield an error. This one you can check and act accordingly


Code:
integer StartNumberInput,status

read(5,'(i5)',stat=status)StartNumberInput

if (status.ne.0)then

     !now for your actions if the user typed 'x'

endif

! and here if he used an integer
 
I knew there would be a better way of doing it, it's just that I started learning Fortran in September, and we haven't done anything like error handling yet - I've been learning VB.NET since 2004 and I am able to do far more advanced things with it than I can with Fortran 95 at the moment... lol :)
 
Sorry, sorry, sorry

I did misspell the statement. must be


read(5,'(i5)',iostat=status)
instead of
read(5,'(i5)',stat=status)


In many statements this construct is 'stat' but with input and output it is 'iostat'.

Just noticed this when I produced an error myself.

Norbert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top