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!

Exiting a loop

Status
Not open for further replies.

mjmnyr96

Programmer
Apr 25, 2006
7
US
Ok, this is gonna sound stupid but heres my problem.

I have an array or reals size 20. I want the user to enter as many values as they would like up to 20. If they want less, fine, but how do I go about exiting the loop? Say they entered their 5 values they wanted, and now want the statistics calculated on it.

I dont want to ask the user every time if they want to enter another number, as that gets annoying after a while. Is there a way around it, say if they just dont enter anything?

Thanks in advance.
 
Are you reading from the command line or from a file? It is easy from a file. Say the file channel is 99 and the array is called reals
Code:
      read (99, *, end = 20) (reals(i), i = 1, 20, 1)
20    continue
From the command line, you could choose an exit value. If the values cannot be negative then you could tell the user to type -1 to exit. Alternatively you could tell the user to type q and use a double read.
Code:
character*80 line
...
   i = 0
10 continue
   read (*, '(A)') line
   if (line(1:1) .eq. 'q') goto 20
   i = i + 1
   read (line, *) reals(i)
   goto 10
20 continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top