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

Printing on screen

Status
Not open for further replies.

fanta2

Technical User
Apr 10, 2005
78
CA
I was printing on the screen like:

Do i=1, 100
print *, 'values are ', i
Enddo
This prints rows of values like:
values are 1
values are 2
values are 3
.
.
I want the values to change in one place like

values are 1 (for the next i the value of 1 would change to 2 .... not another line)
Any idea how this can be done in fortran 90 thanks!
 
Code:
program pp
   character*14 bsp
   do i = 1,14
      bsp(i:i) = char(8)
   end do
   do i = 1, 99
      write(*,"(a14,a11,i3)",advance="no") bsp,"values are ", i
      do j = 1, 100000000
      end do         
   enddo      
   write(*,"(a14,a11,i3)",advance="no") bsp,"values are ", i
end
 
Hi both

Once upon a time in the past it was simply done like this (with the '+' line control):

Code:
       program pp
       do i = 1,100
          write(*,'(a,a11,i3)') '+','Values are ', i
       enddo
       end
 
Yes, that was in the FIV/F66, F77 days where the first character was carriage control.

1 was a new page
0 or space was a newline
+ was stay on the same line

Remember seeing printouts that were over an inch thick with one number on every page without the leading 1. There were also printouts with everything on the same line (one very black line) where they'd put leading + on all numbers.
 
Thanks xwb and gullipe - I want to make the F77 functionality of '+' in F90.

Salgerman - thanks for the idea though it didn't work. It just prints the last value - in that code the result is "values are 100". It doesn't show the intermediate values. The j loop is supposed to delay the process so that the values are visible right? Any idea??
 
fanta2:

The code posted works for me.
did you mess with it at all? or
did you first try it 'as-is'?
Because, in order for it to work, the lengths needs to work out, e.g., the 14 needs to be 14 (or some other number) everywhere and the other two numbers (11 and 3) need to add up to 14...if you know what I mean.

If for some reason the code 'as-is' does not work for you, try puting a 'flush' command after the 'write' ...to make sure the queue is empty after every time and not until it is full.

 
Oh, something else.

Try to put something in the empty loop (like a division operation or sin/cos calculation) to increase the delay...could it be that your computer is just waaaay too fast and you don't see the intermediate values?
 
I have used CVF in Windows XP to compile the code as is. It takes a while without printing anything and then finally prints 'values are 100'. I have used Flush(6) just after the first write line and it didn't help.
 
Is your write statement also writing to unit 6? or
did you leave it as "write(*,..." ?

Just to be consistent, I would "write(6,..." everywhere (not just the first one) and make sure that it is still writing to standard output...otherwise, maybe the standard channels are not 5 and 6, as we believe, anymore...maybe they are something else under CVF?

Please confirm.

 
program pp
use dflib
character*14 bsp
do i = 1,14
bsp(i:i) = char(8)
end do
do i = 1, 99
write(6,"(a14,a11,i3)",advance="no") bsp,"values are ", i
CALL FLUSH (6)
do j = 1, 10000000
!aa = cos(real(j))
end do
enddo

write(*,"(a14,a11,i3)",advance="no") bsp,"values are ", i
end

!I have used the same unit 6.
 

Did you forget to "write(6,..." in the last statement?

Other than that...I am sorry; but it looks like there is nothing else I can do...the code works for me using g95 in cygwin, Windows XP.


 
Thanks salgerman! Sure I havenot changed the last * but didn't help too. Generally, there is no effect in using * or 6.

Again thanks anyway the isue might be in the compiler then.
 
fanta2 said:
It just prints the last value - in that code the result is "values are 100". It doesn't show the intermediate values.
what salgeman posted works excellent for me with g95 and gfortran. But whe I use the optimization option -O in gfortran I get the behaviour as you described:
Code:
$ gfortran pp.f95 -o pp -O

$ pp
values are 100
So I thing your compiler has the optimization option on.
The problem is only in the waiting loop
Code:
do j = 1, 100000000      
end do
try to replace it with something else - CVF should provide a subroutine SLEEP.
 
fanta2,
Maybe this will work for you using the subroutine SLEEP
Code:
[COLOR=#a020f0]program[/color] pp
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: i
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]14[/color] bsp
  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]14[/color]
    bsp(i:i) [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]char[/color]([COLOR=#ff00ff]8[/color])
  [COLOR=#804040][b]end do[/b][/color]   
  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]99[/color]      
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]"(a14,a11,i3)"[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]"no"[/color]) bsp,[COLOR=#ff00ff]"values are "[/color], i
    [COLOR=#008080]call[/color] sleep([COLOR=#ff00ff]1[/color])    
  [COLOR=#804040][b]enddo[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]"(a14,a11,i3)"[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]"no"[/color]) bsp,[COLOR=#ff00ff]"values are "[/color], i
[COLOR=#a020f0]end[/color]
 
Thanks mikrom. Sleep didn't help it too.
 
Then it seems to be your compiler specific problem.
 
Salgerman et al,

I did mess around a little with the code of yours, as it did not work for me either (Compaq Visual Fortran + Windows XP). Seems to be a problem with the advance = 'no' clause. If I change it to advance = 'yes' (or no such clause at all) I get a printout row by row as expected with a suitable delay in between. But with advance = 'no' I did not get any printout at all. Maybe this is a bug in this compiler ??

It looks like the cursor got stuck in its position at the beginning of the line. I added a write statement before the loop without the advance = 'no' clause, just testing if it gets superceded once the cursor is away from the wall, but no success, even when I suppressed the return at the end of this first output

The best I got so far is

Code:
write(*,'(a14,a11,i3, $)') bsp,"values are ", i

whereby '$' suppresses the carriage return to the next line, but I could not find a way to return the cursor to the beginning of the line and overwrite the old line by the next output. Thus I got all the output in a single row, one after the other. Maybe someone knows of a way to return the cursor? BTW, this $-editor seems to be a Compaq feature, but other compilers may have similar special features.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Took me some time, but this works doing console output in a windows environment (compaq compiler, windows XP):

Code:
	program pp   

	use dfwin						! win 32 API routine definitions
	implicit none					! allways !

	integer hOut					! handle to output console 
	integer i						! loop counter 

	logical lrslt

	type (T_COORD) tCoord			! coorrdiantes on console
	type (T_CONSOLE_SCREEN_BUFFER_INFO) tCSBI	! data structure for console


	hOut = GetStdHandle (STD_OUTPUT_HANDLE)		! get the handle to the output console
	lrslt = GetConsoleScreenBufferInfo (hOut, tCSBI)	! retrieve info on the current console, 

	tCoord.X = tCSBI.dwCursorPosition.X					! current cursor position
	tCoord.Y = tCSBI.dwCursorPosition.Y

	do i = 1, 99 
		lrslt = SetConsoleCursorPosition (hOut, tCOORD)	! reset the cursor to the position it had when output started
		write(*,*)"values are ", i						! write to console      
		call sleep (100)								! wait some time to see the output
  	end do            

	stop
	end

Hope this helps.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top