Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It must be an elementary question. I am trying to run some of simple gfortran programs and one of them requires human input of 3 numbers which it reads. It can do it indefinitely but when I want to stop it I type q and get an error message along with the program termination all right. What is the correct way to do it? Pressing ESC does not help. Thanks.
It must be an elementary question. I am trying to run some of simple gfortran programs and one of them requires human input of 3 numbers which it reads. It can do it indefinitely but when I want to stop it I type q and get an error message along with the program termination all right. What is the correct way to do it? Pressing ESC does not help. Thanks.
Do you have the source code for this program? If you do then have a look at it and see if you can figure it out or post it here and someone will be able to do it.
jdk
Perhaps the simplest way to terminate execution is to type ctrl-c, which simply kills the process.
What I usually do, though, is something like the following:
...
REAL X
...
READ(*,*)X
IF(X .EQ. 0.)STOP
...
where I have used '0.' as the stopping criterion assuming '0.' is never a valid input value. Of course, you would have to pick some other criterion if '0.' is a valid input for your code.
There are more elaboration ways to stop execution, such as displaying a question to the user to ask if she wants to run another case (e.g., enter 'y' or 'n'). I'll write some more code if you aren't sure how to code this. Let me know.
Yes, I do have the source code. I copied it from a well known website. Had to do a minor correction since it did not compile first. The code is here:
Code:
C AREA OF A TRIANGLE - HERON'S FORMULA
C INPUT - CARD READER UNIT 5, INTEGER INPUT, ONE BLANK CARD FOR END-OF-DATA
C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT
C INPUT ERROR DISPAY ERROR MESSAGE ON OUTPUT
501 FORMAT(3I5)
601 FORMAT(4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2,12HSQUARE
* UNITS)
602 FORMAT(10HNORMAL END)
603 FORMAT(23HINPUT ERROR, ZERO VALUE)
INTEGER A,B,C
10 READ(5,501) A,B,C
IF(A.EQ.0 .AND. B.EQ.0 .AND. C.EQ.0) GO TO 50
IF(A.EQ.0 .OR. B.EQ.0 .OR. C.EQ.0) GO TO 90
S = (A + B + C) / 2.0
AREA = SQRT( S * (S - A) * (S - B) * (S - C))
WRITE(6,601) A,B,C,AREA
GO TO 10
50 WRITE(6,602)
STOP
90 WRITE(6,603)
STOP
END
I also have a question here. In that FORMAT statement:
To terminate execution using the code as written, type
Code:
0,0,0
This works on my machine running Ubuntu and gfortran -- that is, I get normal termination without an error message.
I would observe that the code is mixing integer and real arithmetic, which is not a good practice.
If I were to write this code according to my own tastes, it would be as follows:
Code:
C AREA OF A TRIANGLE - HERON'S FORMULA
C INPUT - CARD READER UNIT 5, INTEGER INPUT, ONE BLANK CARD FOR END-OF-DATA
C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT
C INPUT ERROR DISPAY ERROR MESSAGE ON OUTPUT
C 501 FORMAT(3I5)
IMPLICIT NONE
601 FORMAT(' A= ',F10.2,' B= ',F10.2,' C= ',F10.2,' AREA= ',F10.2,
* ' SQUARE UNITS')
602 FORMAT('NORMAL END')
603 FORMAT('INPUT ERROR, ZERO VALUE')
REAL A,B,C,S,AREA
10 READ(*,*) A,B,C
IF(A.EQ.0. .AND. B.EQ.0. .AND. C.EQ.0.) GO TO 50
IF(A.EQ.0. .OR. B.EQ.0. .OR. C.EQ.0.) GO TO 90
S = (A + B + C) / 2.0
AREA = SQRT( S * (S - A) * (S - B) * (S - C))
WRITE(6,601) A,B,C,AREA
GO TO 10
50 WRITE(6,602)
STOP
90 WRITE(6,603)
STOP
END
My compiler statement is:
Code:
gfortran -o HERON.x HERON.f
assuming the source code file name is HERON.f.
Last edited by flshope; 05-05-2014 at 11:20 AM.
Reason: fix minor indiscretion in my coding
Interactive fortran read statements are/should be "read(unit,fmt,END=<label>)..." format. Thus an end of file causes the program to exit, or carry out other functions (like saving data) before exit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.