Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Initialize character string

Initialize character string

Initialize character string

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?


RE: Initialize character string

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?


RE: Initialize character string

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

RE: Initialize character string

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

RE: Initialize character string

Taking into account your declaration, the right loop to initialize the string is :

CODE --> fortran

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

They are other ways too. The simplest one :

CODE --> fortran


Frantois Jacq

RE: Initialize character string

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


RE: Initialize character string

For a collection of Fortran-related resource links, including tutorials, go here.

RE: Initialize character string

Thanks all smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close