INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

Trouble with comparing character strings in Fortran

Trouble with comparing character strings in Fortran

Trouble with comparing character strings in Fortran

(OP)
Hello all

So I am working on some basic Fortran (FORTRAN) programs in order to learn the basics. I have some experience coding in C++ before this

Im trying to write some basic input control structures, ie

CODE --> FORTRAN

PROGRAM MENU
      implicit none
      character (LEN=5):: input
      write(*,*)'==Age Database=='
      do while(input/='Q'.or.input/='q')
        write(*,*)'==Main Menu=='
        write(*,*)'> '      
        read(*,*)input
        if(input=='P'.or.input=='p')
          write(*,*)'P command activated'
        end if
      end do
      stop
      end program MENU 

But when I go to compile it with gfortran (ahh, gfortran -o menu menu.f)

I get

Quote (gfortran)

menu.f:9.72:

if(input=='P'.or.input=='p')
1
Error: Cannot assign to a named constant at (1)
menu.f:11.11:

end if
1
Error: Expecting END DO statement at (1)

Which I dont understand, since I thought == was a comparison operator, not the assignment one. Is it absolutely necessary to declare another character string , assign 'P' or 'p' to it, and check it in that case?

RE: Trouble with comparing character strings in Fortran

You are missing the then

Also, since input has been declared as character(len=5), trim it before comparing. Unlike C, there is no string terminator. Fortran strings are space padded so it will be comparing 'P ' against 'P'.

RE: Trouble with comparing character strings in Fortran

(OP)
So is the then keyword required for all if statements, or just ones that compare strings?

How exacty do I "trim" the input variable?

RE: Trouble with comparing character strings in Fortran

Not always, depends on how you write the if statement.

CODE

! Then not required
if (condition) dosomething
! Then  required
if (condition) then
    dosomething
end if 
Trimming

CODE

if(trim(input)=='P'.or.trim(input)=='p') then
    ... 

RE: Trouble with comparing character strings in Fortran

"not equal" is working for me, I had to change the "do while" around:

CODE

PROGRAM MENU
      implicit none
      character (LEN=5):: input
      write(*,*)'==Age Database=='
      do ! while(input/='Q'.or.input/='q')
      
        write(*,*)'==Main Menu=='
        write(*,*)'> '      
        read(*,*)input
        if ( input=='P' .or. input=='p') then
          write(*,*)'P command activated'
        end if
        
        if ( input=='Q' .or. input=='q') exit
      end do
      stop
end program MENU 
No need for trim, by the way, in this case; but do learn about it.

RE: Trouble with comparing character strings in Fortran

Sorry...I meant to say that "not equal" it's not working for me

RE: Trouble with comparing character strings in Fortran

It is a logic error. Should be

CODE

do while (input /= 'Q' .and. input /= 'q') 

RE: Trouble with comparing character strings in Fortran

doh!

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!

Resources

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