1

So, I want to write some code so that the user cannot type in a city that is not in the city list, the city_list array is a one dimensional array of size 5 which values are A,B,C,D and E. So what I did was this

character, dimension(5) :: city_list 

do i=1,5
    city_list(i)= achar(i+64)         !To give the array a,b,c values to its first five components
end do

character :: City1, ...

do while(ANY(City_list == City1) )
    READ*, City1
    PRINT*, "IT'S GOT TO BE A,B,C,D,E"
end do

So I used the ANY function that I saw in another post, but I can' get to use it well, it doesn't seem to do what I asked it to do

sergih123
  • 77
  • 1
  • 1
  • 9
  • 3
    It's not clear what you want your program to do, neither in your description nor your code. Answer these questions: (i) What is the first thing you want your code to do ? (ii) What should happen if the user enters the name of one of the cities ? (iii) What should happen if the user enters something other than the name of a city ? And no, don't tell me the answers to these questions, implement them in your program. Incidentally, for real help, post real code. – High Performance Mark Dec 16 '18 at 19:21
  • Oh hi Mark (:P), I would've posted the actual code but it's not in english (at least the names of the variables, althought that may be something you don't care about) and I thought it wasn't necessary for what I thought I was asking, I may not have expressed my self correctly, sorry. I just want the do while loop to keep looping untill what you enter as "city1" is a string that is on the city_list array, so I was looking for a function that would compare all the values in the array to "city1" that's why I used ANY (which I saw in another post), is any really the thing I should do here? – sergih123 Dec 16 '18 at 19:49
  • 1
    welcome, we really ned the exact code ([mcve]). Variables do not have to be English, or you can rename them for us. The most important thing is to see what you are doing. Do note that you appear to be confused in what is a Fortran string and what is a character array. **These are very different.** ANY only works for character arrays, for strings use SCAN. See https://stackoverflow.com/questions/33415590/difference-between-character10-a-and-character-a10 – Vladimir F Героям слава Dec 16 '18 at 20:30

3 Answers3

3

Another approach may be to use an infinite do-loop with conditional exit (to make the code a bit simpler...)

program main
    implicit none
    character :: city_list( 5 ), city1
    city_list = ["A", "B", "C", "D", "E"]

    do
        print*, "Introduce a city that is on the list"
        read *, city1
        if ( any( city1 == city_list ) ) exit
    enddo

    print *, "Your city1 is ", city1
end
roygvib
  • 7,218
  • 2
  • 19
  • 36
2

So I figured it out,

do while(is_in_list .eqv. .FALSE.)
    print*, "Introduce a city that is on the list"
    READ*, Ciud1
    do i=1, 5
        if(city_list(i) == city1) then      
            is_in_list = .TRUE.
            PRINT*, "It's in the list"
        end if
    end do
end do

That way the console will keep on asking to introduce a city that is in city_list. Once you introduce one that is in city_list it will keep going with the code.

We can tidy this code up a little bit and use the ANY function:

  is_in_list = .FALSE.

  DO WHILE(.NOT.is_in_list)
     PRINT*, "Introduce a city that is on the list"
     READ*, city1
     is_in_list = ANY(city_list==city1)
     IF (is_in_list) PRINT*, "It's in the list"     
  END DO

Here:

  • set the test variable to .FALSE. before entering the DO loop;
  • comparing a logical to either .TRUE. or .FALSE. makes us look like script-kiddies, real programmers know that the logical itself has one of those values;
  • we don't need to write a loop to compare each element in the array with the value entered, using the ANY function leaves that kind of grunt work to the compiler;
  • since ANY returns a logical we can assign its result directly to the test variable.
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
sergih123
  • 77
  • 1
  • 1
  • 9
0

!For modern Fortran you can use FINDLOC function:

program TestFindLocinStringArray
    implicit none
    character:: city_list(5), city1
    integer::ip(1)
    city_list = ["A", "B", "C", "D", "E"]
        print*, "Introduce a city that is on the list"
        read *, city1
        ip=findloc(city_list,city1)
        if(ip(1)>0)then
         type*,"city1 is on No#",ip
         exit
        else
         type*, "Your city1 is NOT in the list!"
        end if
    enddo
end program
MathArt
  • 159
  • 7