LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-04-2014, 10:54 AM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Rep: Reputation: Disabled
How to terminate a gfortran program?


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.
 
Old 05-04-2014, 11:18 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Quote:
Originally Posted by AlexBB View Post
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
 
Old 05-04-2014, 11:24 AM   #3
flshope
Member
 
Registered: Jul 2009
Location: Tennessee (United States)
Distribution: Debian 11.6, Ubuntu 22.04.2, 18.04.6, Android 11
Posts: 236
Blog Entries: 44

Rep: Reputation: 80
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.
 
Old 05-04-2014, 12:03 PM   #4
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
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:

Code:
601 FORMAT(4H A= ,I5,5H  B= ,I5,5H  C= ,I5,8H  AREA= ,F10.2,12HSQUARE
Code:
12HSQUARE
"SQUARE" is not separated from the format itself which I believe is 12H. When I tried to separate it and print the code like

Code:
12H SQUARE
it did not work. It would not give the result expected. It would say "not a number." Why?

If I leave it the way it is then the result looks like this, for example: 123.56SQUARE UNITS or something.

Thanks, - Alex

Last edited by AlexBB; 05-04-2014 at 12:13 PM.
 
Old 05-05-2014, 08:15 AM   #5
flshope
Member
 
Registered: Jul 2009
Location: Tennessee (United States)
Distribution: Debian 11.6, Ubuntu 22.04.2, 18.04.6, Android 11
Posts: 236
Blog Entries: 44

Rep: Reputation: 80
To deal with the '12HSQUARE' problem, I suggest the form of the code be:

Code:
  601 FORMAT(4H A= ,I5,5H  B= ,I5,5H  C= ,I5,8H  AREA= ,F10.2,13H SQUARE
     * UNITS)
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
 
Old 05-05-2014, 02:31 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Try a simple CTRL-d - aka, end of file.

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.
 
Old 05-06-2014, 06:27 AM   #7
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Thank you guys, flshope and jpollard. It is quite interesting. - Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the program to terminate after sertain length of time? hubabuba Programming 6 04-23-2006 11:06 PM
ps does not terminate Lotharster Linux - Newbie 1 02-18-2006 09:03 AM
Terminate Hung Program Ingla Linux - General 3 11-15-2005 08:13 AM
How to terminate this command slpwkr Programming 4 03-28-2005 11:33 AM
how to terminate processes feetyouwell Programming 8 09-18-2004 02:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration