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!

IF statements and comparing character strings

Status
Not open for further replies.

Samohtvii

Programmer
Aug 2, 2012
21
AU
I have tried everything to get a simple bit of code to work but nothing has worked.
pseudocode:
get user answer (yes/no)
if answer = yes
print yes

I always get the same error "Cannot assign to a named constant"
i have tried
--
t = LGT(answer, 'yes')
IF(t)
--
if(answer .eq. 'yes)
--
if(LGT(answer, yes))
--
and any other combination.

Please help
Thanks
 
Comparisons in 'if' statements are made with '==', because '=' is used for assignments. Moreover, when comparing characters, the characters you are comparing to need to be given between quotes ('').

The following example works with my computer (gfortran) :

Code:
  program main
  implicit none
  character*3 :: answer

  answer = 'yes'

  if (answer == 'yes') then
    print*,'test successful'
  endif

  end program main

Hope it helps.
 
This works too
Code:
if (answer .eq. 'yes') then
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top