Print characters on one line without newline...??? 
Author Message
 Print characters on one line without newline...???

         I'm writting this PrintX routine that is to print
the letter X n times on one line without any spaces and
newline.  My PrintX routine looks like this:

      SUBROUTINE PrintX(XCOUNT)
        INTEGER XCOUNT

        INTEGER i

        DO 10 i, XCOUNT
          PRINT 20
  10    CONTINUE
  20    FORMAT(1X, 'X')

        PRINT *

        RETURN
        END

correct: XXXXX...X
incorrect: X
           X
           X
           .
           .
           X

This routine only prints one 'X' on n lines and I want it to print
n X's on one line without any spaces in between and without a new-
line character.  Could someone explain what I might be doing wrong?
Thanks in advance to all that reply to this e-mail.

-Conrad



Sat, 06 May 1995 02:33:42 GMT  
 Print characters on one line without newline...???
a.cs.uiuc.edu denies any knowledge of ctaylor, and gets major indigestion

suggestion here.  Bandwidth apologies, etc.

In comp.lang.fortran you write:

Quote:
>         I'm writting this PrintX routine that is to print
>the letter X n times on one line without any spaces and
>newline.  My PrintX routine looks like this:
>      SUBROUTINE PrintX(XCOUNT)
>        INTEGER XCOUNT
>        INTEGER i
>        DO 10 i, XCOUNT
>          PRINT 20
>  10    CONTINUE
>  20    FORMAT(1X, 'X')
>        PRINT *

>        RETURN
>        END
>correct: XXXXX...X
>incorrect: X
>           X
>           X
>           .
>           .
>           X

I am only familiar with FORTRAN-77; under that I don't think there's a
standard-conforming method to do EXACTLY what you're asking for.  Many
implementers, though, have the formatting character "$" in their superset
--- you may well want to check on the Fortran for your machine.

What's wrong, though, with building a character string and outputting THAT.
Even better for the specimen code, though, would be using an implied
DO-loop.

      SUBROUTINE PrintX(XCOUNT)
      INTEGER XCOUNT
      INTEGER i
      if (XCOUNT .gt. 0) then
         print 10, ('X', i = 1, XCOUNT)
      else
         print 10
      end if
   10 format (1x, 80a1)
* I don't know if you intended the extra line or not:
*?    print 10
      return
      end
--
                                              --- Tim Rolfe




Sat, 06 May 1995 04:16:14 GMT  
 Print characters on one line without newline...???

Quote:
(Conrad W Taylor) writes:

>         I'm writting this PrintX routine that is to print
> the letter X n times on one line without any spaces and
> newline.

The following does what you want and is written in ANSI standard FORTRAN 77.

      program test
      call printx(15)
      call printx(80)
      end
      subroutine printx(xcount)
c
c  Print a space, then xcount X's with a newline at the end
c
      integer xcount,nd
      character*16 form1,form2
      write(form1,fmt='(a,i1,a)') '(a,i',nd(xcount),',a)'
      write(form2,fmt=form1) '(1x,',xcount,'(''X''))'
      write(6,form2)
      return
      end
      function nd(k)
c
c  Returns the number of digits in k (plus one for the
c  minus sign if k is negative)
c
      integer nd,k,l
      nd=1
      l=abs(k)
    1 if (l.lt.10) goto 2
      nd=nd+1
      l=l/10
      goto 1
    2 if (k.lt.0) nd=nd+1
      return
      end

OUTPUT:
 XXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I don't know an easier way to do this in standard FORTRAN, but someone will
probably show you.

Andy Mai



Sat, 06 May 1995 04:31:56 GMT  
 Print characters on one line without newline...???

:          I'm writting this PrintX routine that is to print
: the letter X n times on one line without any spaces and
: newline.

  On an HP9000:

        accept *, n
        write(*,'(A,$)') ('X',i=1,n)
        end


Quote:
> a.out

47

 where the above line has no CR at the end (the pld... is my prompt).
 Reminder:  '$' is not an F77 standard feature.



Sat, 06 May 1995 06:35:38 GMT  
 Print characters on one line without newline...???

|>         I'm writting this PrintX routine that is to print
|>the letter X n times on one line without any spaces and
|>newline.  My PrintX routine looks like this:
|> [...]
|>
|>This routine only prints one 'X' on n lines and I want it to print
|>n X's on one line without any spaces in between and without a new-
|>line character.  Could someone explain what I might be doing wrong?

I do not think this can be done using standard Fortran.
Many compilers, however, offer a FORMAT extension usually
called $ which allows this, and works as follows:

      SUBROUTINE PrintX(XCOUNT)
        INTEGER XCOUNT

        INTEGER i

        DO 10 i=1, XCOUNT
          PRINT 20
  10    CONTINUE
  20    FORMAT($, 'X')

        PRINT *

        RETURN
        END

You may have a hard time in doing this on IBM mainframes :-)
--
Furio Ercolessi
Materials Research Laboratory           |   Intl School for Advanced Studies
Univ. of Illinois at Urbana-Champaign   |   Trieste, Italy



Sat, 06 May 1995 03:49:53 GMT  
 Print characters on one line without newline...???

The following subroutine will do the job

        SUBROUTINE PrintX(XCOUNT)
        INTEGER XCOUNT

        INTEGER i

        PRINT *, ('X',i=1,XCOUNT)

        RETURN
        END

---------------------------------------------------           / \
 Department of Aeronautics                                    | |
 Imperial College of Science, Technology & Medicine   ________| |________
 Prince Consort Road, London SW7 2AY, U.K.           |________| |________|
 Room E355, Tel. : (071) 589 5111 Ext. 4028                   | |

---------------------------------------------------       |___\_/___|
 P. N. Asproulis



Fri, 12 May 1995 22:02:39 GMT  
 Print characters on one line without newline...???

Quote:

>The following subroutine will do the job

>        SUBROUTINE PrintX(XCOUNT)
>        INTEGER XCOUNT

>        INTEGER i

>        PRINT *, ('X',i=1,XCOUNT)

>        RETURN
>        END

No, it doesn't work !  On a Sun SPARC-2 with Fortran 1.4.1 and a main-program:
        call PrintX(10)
        print *, 'The end'
        end
I get this result:
% a.out
XXXXXXXXXX
The end

Ole H. Nielsen
Laboratory of Applied Physics, Building 307
Technical University of Denmark, DK-2800 Lyngby, Denmark

Telephone: (+45) 42 88 24 88 ext. 3187
Telefax:   (+45) 45 93 23 99



Sun, 14 May 1995 18:17:15 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. easy character printing on one line (no CR/LF)

2. print without newline?

3. print without newline

4. Printing without newlines..

5. echo, print and the newline character

6. PEP 259: Omit printing newline after newline

7. PEP 259: Omit printing newline after newline

8. Serial signals with/without end-of-line characters?

9. puts without end-of-line character

10. : Print a Character without a \n

11. How do I print without advancing a line?

12. Formatted output - print, write without line feed

 

 
Powered by phpBB® Forum Software