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

Initialize character string

Status
Not open for further replies.

ognik

Programmer
Joined
Jan 18, 2015
Messages
5
Location
NZ
Hi - just learning FORTRAN, I want to print a line of '*', thought Id set up the line programmatically as follows:

character(len=n) :: stars
do cntr = 1, n
stars(cntr) = '*'
end do

However (I'm using silverleaf/plato) I get the following compile error "error 215 - Invalid expression on left hand side of assignment" - referring to the stars(cntr) = '*' line. It seems it doesn't like indexing the character string, but surely this is possible? Can anyone tell me how to achieve this with understanding?

Thanks
Ognik
 
Hi - sorry, newbie stuff, I fixed it with 'stars(cntr:1) = '*'

But when I try to print it as print *, stars I get a run-time error.

So the questions becomes how to initialise a string as n of a char, and then print it - keeping init and print simple 1-liners if possible?

Thanks
 
please, start you program with "implicit none"

I suspect that the variable cntr is not declared integer. In addition, I would like to see the declaration of "n"

Frantois Jacq
 
Hi, sorry, I had just copied the extracted bit, whole pgm is below. Also I am learning Fortan - so in addition to this slightly trivial point, I would appreciate any improvements you can advise me on
--------------------
program sinPluscos1
!Summary: Calc 4 & 5 point numerical deriv of sinX + cosX [f'(x)= CosX - sinX]
implicit none
integer, parameter :: dp = selected_real_kind(6, 99)
real(kind = dp) :: arg=1, h, exact, approx, diff, f0, f1, f2, g1
integer, parameter :: n=50
integer :: cntr
character(len=n) :: stars
!----- init -----
do cntr = 1, n
stars(cntr:1) = '*'
end do
!----- header ----
exact = cos(arg) - sin(arg)
print *, 'Numerical derivatives of sinX - CosX at x = 1, for varying h'
print *, 'Exact value = ', exact
print *, stars
print *, 'h 4-point 5 point'
print *, 'Enter value of h < 1.0 (GE 1.0 to stop)'
print *, stars
!---- header end ----
read *, h
do while (h.lt.1.00)
f0 = sin(arg)+cos(arg)
f1 = sin(arg+h)+cos(arg+h)
f2 = sin(arg+(2.0*h))+cos(arg+(2.0*h))
g1 = sin(arg-h)+cos(arg-h)
approx = ((-2.0*g1)-(3.0*f0)+(6.0*f1)-f2)/6*h
diff = exact - approx
print 20, h, approx, diff
read *, h
end do
20 format (8X, 3(A, 12X))
print *, stars
end program sinPluscos1
 
Taking into account your declaration, the right loop to initialize the string is :

Code:
do cntr = 1, n
   stars(cntr:cntr) = '*'
end do

They are other ways too. The simplest one :

Code:
stars=REPEAT('*',n)

Frantois Jacq
 
Awesome thanks Frantois. Is there a good but brief fortran95 language guide you would recomend?

Cheers
Alan
 
For a collection of Fortran-related resource links, including tutorials, go here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top